geolocation in ionic 3 always returns the same latitude and longitude

0

It turns out that the geolocation of ionic3 in my cell phone after a while starts to return the same latitude and longitude. That is to say, during 30 minutes, the geolocation calculates the latitude and longitude correctly but after a time it starts to return the same latitude and longitude. Example: during the 30 minutes returns different latitudes and longitudes. 6.25 ...- 7591929 ...

After a time always the same latitude and longitude returns. That is, 6.25489, -75.129912. It does not matter in the place where it always returns 6.25489, -75.129912. I do not know if it's a cache.

Here in the provider method I have

import { Geolocation } from '@ionic-native/geolocation';

The declared variables

public laS: any = 0;
public loS: any = 0;

In the constructor I have

constructor
    (
    public http: Http,
    private geolocation: Geolocation
    ) 
    {
       console.log('Hello GeoProvider Provider');
    }

The method to get the latitude and longitude I have

lng() {
    var options = {
      enableHighAccuracy: true,
      maximumAge: 0,
      timeout: 5000
    };

      this.subscription = this.geolocation.getCurrentPosition(options).then((resp) => {
        //alert(resp.coords.latitude);
        //alert(resp.coords.longitude);
        this.laS = resp.coords.latitude;
        this.loS = resp.coords.longitude;
      }).catch((error) => {
        //console.log('Error getting location', error);
        alert("Error "+error);
      });

  }

The previous method tried removing options to see if it calculated a different latitude and longitude. Also placing at maximumAge: 0 and it does not work either. It always returns the same.

Then in the home.ts component I import the aforementioned provider

import { GeoProvider } from '../../providers/geo/geo';

In the constructor I have

constructor
  (
    public navCtrl: NavController,
    public geoProvider: GeoProvider
  ) 
  {}

And the method to call the function

lng()
  {
    this.geoProvider.lng();
  }

Finally in the home.html view I have

<div>
      <button ion-button color="primary"  (click) = "lng()">Cacular solo latitud y longitud</button>
      <div>
        latitud: {{ geoProvider.laS  }}
      </div>
      <div>
          Longitud: {{ geoProvider.loS  }}
      </div>
  </div>

I do not know where I'm making the error or if it's a problem with the geolocation plugin.

    
asked by santiago 02.11.2017 в 17:01
source

1 answer

0

It is very strange the error that is having, that map api works very well. I recommend that you use the Geolocation.watchPosition () method how they explain here , because the code that you are publishing, I think it is very good for it to be malfunctioning. and as I see here , they explain the difference between watchPosition () and getCurrentPosition () and say the property is better and more accurate watchPosition () .

    
answered by 02.11.2017 в 17:24