cordova - www/index.html 想使用您当前的位置 - Ionic Framework

标签 cordova geolocation ionic-framework cordova-plugins ngcordova

我搜索了与基于 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/

相关文章:

javascript - 如何将两个数组缓冲区合并为一个?

testing - 用 Jest 进行 React Native 测试不可能模拟地理位置

java - 找不到 gradle 包装器

ios - 如何从方法中启动 ViewController?

javascript - 在用户交互之前调用 Ionic recognizePopup then() 函数

ios - WebSQL-从SD卡存储和检索数据库-PhoneGap iOS

javascript - Chrome 地理定位传感器破坏 CORS

HTML5 Geolocation API获取WiFi AC信息?

ionic-framework - Ionic 4 中的深层链接如何工作?

angular - Ionic 4 - 当我运行 "ionic server"时,出现 [错误] ng 意外关闭(退出代码 1)