getCurrentPosition does not work

Hi All,

Please help. getCurrentPosition does not work in one of project, it is so strange issue.

I have a project which is using same way as following, that project can get user location, but in this project, it does not work, on the console, I only can see ’ get user location …’ as the first line output, after that there is no any output even error message.

I have install the plugin and add provider in app.module.ts. Any ideas where is wrong? thanks

ionViewDidLoad() {
 this.loadMap();

}

loadMap() {
console.log(‘get user location …’);
this.geolocation.getCurrentPosition().then((position) => {

        let latLng = {lat:position.coords.latitude, lng:position.coords.longitude};

        console.log('lat lng is '+latLng);
        let mapOptions = {
            center: latLng,
            zoom: 15
            
        }

        

    }, (err) => {
        console.log(err);
    });

}

Maybe it’s just taking a long time. I’ve been finding that my geolocation can take a couple minutes to fire.

It should also prompt the user to agree to share their location if they haven’t agreed for this site yet. You won’t get a result until after they’ve agreed.

You don’t need to use a plugin as modern browser’s support the Geolocation API: https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition

You might consider moving your Geolocation code into a Service:

  ...

  private options = {
    enableHighAccuracy: true,  // use GPS
    maximumAge        : 30000, // milliseconds e.g., 30000 === 30 seconds
    timeout           : 27000  // milliseconds
  };

  ...

    if (navigator.geolocation) {

      navigator.geolocation. getCurrentPosition((position: any) => {

        ...

      }, (error) => {
        this.logger.info('GeolocationService: getCurrentPosition() - error code: ' + error.code);

        //
        // try enableHighAccuracy: false  e.g., use IP
        //

      }, this.options);
    }

Which can be injected into your Page.

And, try creating a simple wrapper for console.log(): https://robferguson.org/blog/2017/09/09/a-simple-logging-service-for-angular-4/

1 Like

Thanks for your reply.

Yes, When I open the application on device, it prompt me to ‘allow xxxxxx’, I choosed ‘Yes’, but after that does not get any result , even did not fire getCurrentPosition operation at all.

Thanks for your reply, but I am just wondering why that plugin did not work anymore, it worked very well before.

Same issue here. had to increase the timeout to 10 seconds to get the geolocation.
different devices show different duration for getting the value. before I updated all plugin/packages versions, I got the location much faster.

1 Like

thannks.You are right,after setting timeout option it works.

1 Like

sir i tried ur code but getCurrentPosition not fire in some Android devices specially in the Oppo F11 pro devices
and i tested in 4 Different android devices but only not working in Oppo F11 pro tell me what i do.