I am using ionic Geolocation
Library for getting user current location.
When i open the application every time it's asking for the permission. I have use the following code to get the current location from user.
let options = {timeout: 20000, enableHighAccuracy: true , maximumAge: 0};
this.geolocation.getCurrentPosition(options).then((resp) => {
})
Can you please help me why it asking for the permission every time.
Thanks
This can happen because the Ionic platform may not be fully ready before code requesting a user location gets called. See this Ionic forum discussion.
The Ionic-Native geolocation plugin, which uses native device location permissions & is only set once, AND the browser window in which the Ionic Cordova app run are BOTH asking for user permission.
In my case, and I presume yours, the alert that shows up on every launch references the browser 'container' and 'index.html', indicating it's the browser asking for permission. The app-specific alert only shows up once on the initial launch after installation.
I had the same problem w/a geolocation service call in a modal that appears right after launch. My solution was to:
Inject the Platform
from ionic-angular into the component
making the geolocation request, and then
Wrap my geolocation call in platform.ready().then()
, to ensure
it only ran after the platform was, y'know, "ready."
This removed the duplicate alerts, and it only asks once immediately after first launch.
import { Component, OnInit } from '@angular/core';
import { Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
constructor(private platform: Platform, private geo: Geolocation) {}
ngOnInit() {
this.setLocation();
}
setLocation() {
this.platform.ready().then(() => {
return this.geo.getCurrentPosition()
.then(pos => {//do stuff w/position});
});
}