I searched various forums and posts related to GeoLocation based alert issue; For some reason no technique worked in my case.
Added "cordova-plugin-geolocation" plugin to my Ionic Framework Project.
Added to my base Controller under $ionicPlatform.ready(...)
navigator.geolocation.getCurrentPosition(function(position) {
var coords = {};
coords.updated = new Date();
coords.latitude = position.coords.latitude.toFixed(6);
coords.longitude = position.coords.longitude.toFixed(6);
coords.altitude = position.coords.altitude + ' m';
coords.accuracy = position.coords.accuracy + ' m';
coords.altitudeAccuracy = position.coords.altitudeAccuracy + ' m';
coords.heading = position.coords.heading + '\u00b0';
coords.speed = position.coords.speed + ' m/s';
console.log('Fetched Location...' + geoText);
return position;
}, function(err) {
Logger.error(err.message);
}, {
timeout: 10000,
enableHighAccuracy: true
});
When I emulate / run in iOS / Android it throws
Error Message: /www/index.html would like to use Your Location
I would greatly appreciate if someone can help.
I resolved this issue by following this
Created a new Service for Location as follows
.service('LocationService', function($q) {
this.fetch = function() {
var deferred = $q.defer();
navigator.geolocation.getCurrentPosition(function(position) {
deferred.notify('Fetching Location...')
var coords = {};
coords.updated = new Date();
coords.latitude = position.coords.latitude.toFixed(6);
coords.longitude = position.coords.longitude.toFixed(6);
coords.altitude = position.coords.altitude + ' m';
coords.accuracy = position.coords.accuracy + ' m';
coords.altitudeAccuracy = position.coords.altitudeAccuracy + ' m';
coords.heading = position.coords.heading + '\u00b0';
coords.speed = position.coords.speed + ' m/s';
deferred.resolve(position);
}, function(err) {
deferred.reject(err);
}, {
timeout: 10000,
enableHighAccuracy: true
});
return deferred.promise;
}
})
Then we shall call services as
Location.fetch().then(function(pos){...})
within $ionicPlatform.ready(..)
or add to a button ng-click
to fetch location.
Root Cause:
What I believe is that when we call Location.fetch()
or navigator.geolocation..
directly in any controllers, its executed when the controllers are loaded initially. This might get fired before Cordova / Ionic platform is ready and hence we get the additional / unwanted alert mentioning that www/index.html is trying to use location as my problem states.
Add these lines in config.xml
i have fixed this problem.
<feature name="Geolocation">
<param name="ios-package" value="Geolocation" />
</feature>
<gap:plugin name="cordova-plugin-geolocation" source="npm"/>