【问题标题】:www/index.html would like to use your current location - Ionic Frameworkwww/index.html 想使用您当前的位置 - Ionic Framework
【发布时间】:2015-11-08 13:07:12
【问题描述】:

我搜索了与基于地理位置的警报问题相关的各种论坛和帖子;出于某种原因,在我的情况下没有任何技术起作用。

在我的 Ionic 框架项目中添加了“cordova-plugin-geolocation”插件。

添加到$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 中模拟/运行时,它会抛出

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

如果有人可以提供帮助,我将不胜感激。

【问题讨论】:

  • 您无法在设备模拟器中真正测试 cordova 插件。您是否尝试过构建并将其安装在实际设备上?或者您可以使用 intelxdk software.intel.com/en-us/intel-xdk 导入您的项目,然后使用调试选项卡将其推送到设备并拥有实时调试器,以便在真实设备上测试 cordova 插件并能够对其进行调试。
  • 是的@Jess,我在我的手机(Android 和 iOS)上试过。感谢您的回复。该插件实际上工作正常,我担心的是它会弹出“.../Index.html 希望使用您的位置消息”(除了应用程序级别的确认)。
  • 是的,我们刚开始在 ios 8.4.1 中遇到此问题,但在 android 上没有。你有没有设法找到解决办法?
  • @JessPatton,请查看我的答案以获取解决方案
  • 好的,我试试看。谢谢

标签: cordova geolocation ionic-framework cordova-plugins ngcordova


【解决方案1】:

我按照这个解决了这个问题

为 Location 创建了一个新的 Service,如下所示

.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 以获取位置。

根本原因: 我相信,当我们直接在任何控制器中调用Location.fetch()navigator.geolocation.. 时,它会在最初加载控制器时执行。这可能会在 Cordova / Ionic 平台准备好之前被触发,因此我们会收到额外的/不需要的警报,提到 www/index.html 正在尝试使用位置作为我的问题状态。

【讨论】:

  • 是的,这解决了 ios 上的问题。
  • 你能分享你的位置进口声明吗? “.service”是如何附加到位置的?
【解决方案2】:

在 config.xml 中添加这些行

我已经解决了这个问题。

<feature name="Geolocation">
    <param name="ios-package" value="Geolocation" />
</feature>
<gap:plugin name="cordova-plugin-geolocation" source="npm"/>

【讨论】:

  • config.xml 文件在哪里?
猜你喜欢
  • 2015-04-07
  • 2022-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 2021-08-21
  • 1970-01-01
相关资源
最近更新 更多