www / index.html想要使用您当前的位置 - Ionic Framework [英] www/index.html would like to use your current location - Ionic Framework

查看:532
本文介绍了www / index.html想要使用您当前的位置 - Ionic Framework的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了与基于GeoLocation的警报问题相关的各种论坛和帖子;出于某种原因,我的案例没有任何技术。



在我的Ionic Framework项目中添加了cordova-plugin-geolocation插件。

$ ionicPlatform.ready(...) $ b

  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;
},函数(err){
Logger.error(err.message);
},{
timeout:10000,
enableHighAccuracy:true
});

当我在iOS / Android中模拟/运行时,它会抛出


错误讯息:/www/index.html想使用您的位置


我会非常感谢,如果有人可以帮助。

解决方案

我通过以下解决这个问题



为位置创建一个新的服务如下

  .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.alti tude = position.coords.altitude +'m';
coords.accuracy = position.coords.accuracy +'m';
coords.altitudeAccuracy = position.coords.altitudeAccuracy +'m';
coords.heading = position.coords.heading +'\\\°';
coords.speed = position.coords.speed +'m / s';
deferred.resolve(position);
},函数(err){
deferred.reject(err);
},{
timeout:10000,
enableHighAccuracy:true
});
return deferred.promise;

))

然后我们将服务称为

  Location.fetch()。then(function(pos){...})

$ ionicPlatform.ready(..)中或添加到按钮 ng-click 取得位置。



根本原因:
我相信当我们在控制器加载时直接调用 Location.fetch() navigator.geolocation .. 原来。这可能会在Cordova / Ionic平台准备就绪之前被解雇,因此我们得到了额外/不需要的警报,提及www / index.html正在尝试使用位置作为我的问题状态。


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.

这篇关于www / index.html想要使用您当前的位置 - Ionic Framework的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆