Reputation: 2061
I am learner in Ionic and present in my application I am trying to get user location using below code but it's not working properly.
I mean lat and longs are coming but very slowly location lat and longs changing when user move one place to another. Is there any better solution for getting user current location?
this.geolocation.getCurrentPosition().then((resp) => {
}).catch((error) => {
console.log('Error getting location', error);
});
let watch = this.geolocation.watchPosition();
watch.subscribe((data) => {
this.data = data;
console.log("----->Watch latitude" + data.coords.latitude);
console.log("-----> Watch logitude" + data.coords.longitude)
console.log("-----> Watch accuracy" + data.coords.accuracy)
});
Upvotes: 1
Views: 4948
Reputation: 1749
You can increase the accuracy by passing the enableHighAccuracy option:
this.geolocation.getCurrentPosition({ enableHighAccuracy: true })
or
this.geolocation.watchPosition({ enableHighAccuracy : true, timeout: 10000 })
You can also pass params for increasing the frequency the call is made such as timeout, this is explained in the README:
"enableHighAccuracy: Provides a hint that the application needs the best possible results. By default, the device attempts to retrieve a Position using network-based methods. Setting this property to true tells the framework to use more accurate methods, such as satellite positioning. (Boolean)
timeout: The maximum length of time (milliseconds) that is allowed to pass from the call to navigator.geolocation.getCurrentPosition or geolocation.watchPosition until the corresponding geolocationSuccess callback executes. If the geolocationSuccess callback is not invoked within this time, the geolocationError callback is passed a PositionError.TIMEOUT error code. (Note that when used in conjunction with geolocation.watchPosition, the geolocationError callback could be called on an interval every timeout milliseconds!) (Number)
maximumAge: Accept a cached position whose age is no greater than the specified time in milliseconds. (Number)"
https://github.com/apache/cordova-plugin-geolocation
Upvotes: 2