Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular geolocation

I'm stuck at geolocation. Actually it working fine but some problem appear when I using If else. When User click allow location it working but the problem appear when user click not allow, it not insert in else. This DEMO as reference.

Component

    ngOnInit() {
       this.getUserLocation();
    }

    getUserLocation() {
       if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(position => {
            this.lat = position.coords.latitude;
            this.lng = position.coords.longitude;
          });
    }else {
       console.log("User not allow")

    }
}
like image 466
javiens Avatar asked Jul 16 '26 03:07

javiens


1 Answers

@shayan ans is the correct approach to get your desire result. I will modify his approach a little bit to meet your expectations.

Try something like this

navigator.geolocation.getCurrentPosition(function(){
        alert('Location accessed')
},function(){
       alert('User not allowed')
},{timeout:10000})
like image 89
Jaydip Jadhav Avatar answered Jul 18 '26 16:07

Jaydip Jadhav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!