我搜索了与基于 GeoLocation 的警报问题相关的各种论坛和帖子;出于某种原因,没有任何技术适用于我的案例。
在我的 Ionic Framework 项目中添加了“cordova-plugin-geolocation”插件。
在 $ionicPlatform.ready(...)
下添加到我的基本 Controller
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
});
当我在 iOS/Android 中模拟/运行时抛出
Error Message: /www/index.html would like to use Your Location
如果有人能提供帮助,我将不胜感激。
最佳答案
我按照这个解决了这个问题
如下创建一个新的位置服务
.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;
}
})
然后我们将服务称为
Location.fetch().then(function(pos){...})
在 $ionicPlatform.ready(..)
中或添加到按钮 ng-click
以获取位置。
根本原因:
我相信,当我们在任何 Controller 中直接调用 Location.fetch()
或 navigator.geolocation..
时,它会在最初加载 Controller 时执行。这可能会在 Cordova/Ionic 平台准备就绪之前被触发,因此我们会收到额外的/不需要的警报,提到 www/index.html 正在尝试使用位置作为我的问题状态。
关于cordova - www/index.html 想使用您当前的位置 - Ionic Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32024240/