www/index.html 想使用您当前的位置 - Ionic Framework [英] www/index.html would like to use your current location - Ionic Framework
问题描述
我搜索了与基于地理位置的警报问题相关的各种论坛和帖子;出于某种原因,在我的情况下没有任何技术起作用.
I searched various forums and posts related to GeoLocation based alert issue; For some reason no technique worked in my case.
在我的 Ionic 框架项目中添加了cordova-plugin-geolocation"插件.
Added "cordova-plugin-geolocation" plugin to my Ionic Framework Project.
在 $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
});
当我在 iOS/Android 中模拟/运行时,它会抛出
When I emulate / run in iOS / Android it throws
错误消息:/www/index.html 想使用您的位置
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
为 Location 创建了一个新的 Service,如下所示
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;
}
})
那么我们将服务称为
Location.fetch().then(function(pos){...})
在 $ionicPlatform.ready(..)
内或添加到按钮 ng-click
以获取位置.
within $ionicPlatform.ready(..)
or add to a button ng-click
to fetch location.
根本原因:我相信,当我们在任何控制器中直接调用 Location.fetch()
或 navigator.geolocation..
时,它会在最初加载控制器时执行.这可能会在 Cordova/Ionic 平台准备好之前被触发,因此我们会收到额外的/不需要的警报,指出 www/index.html 正在尝试使用位置作为我的问题状态.
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.
这篇关于www/index.html 想使用您当前的位置 - Ionic Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!