www/index.html would like to use your current location - Ionic Framework

不羁岁月 提交于 2019-12-11 03:58:40

问题


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.


回答1:


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.




回答2:


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"/>


来源:https://stackoverflow.com/questions/32024240/www-index-html-would-like-to-use-your-current-location-ionic-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!