getCurrentPosition does not work


#1

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);
    });

}

#2

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


#3

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.


#4

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/


#5

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.


#6

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


#7

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.


#8

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