Android的地理位置使用PhoneGap的code:3错误 [英] android geolocation using phonegap code: 3 error

查看:263
本文介绍了Android的地理位置使用PhoneGap的code:3错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PhoneGap的API文档使用地理定位,但得到以下错误消息,

I'm trying to use geolocation using PhoneGap API doc but getting below error message,

警报 code:3 消息:超时过期

Alert code:3 message: Timeout expired

我的code是,

    <script>

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

var watchID = null;

// Cordova is ready
//
function onDeviceReady() {
    // Throw an error if no update is received every 30 seconds
    var options = { timeout: 10000 };
    watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}

// onSuccess Geolocation
//
function onSuccess(position) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: ' + position.coords.latitude + '<br />' +
                    'Longitude: ' + position.coords.longitude + '<br />' +
                    '<hr />' + element.innerHTML;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: ' + error.code + '\n' +
      'message: ' + error.message + '\n');
}

请咨询我,我缺少的是什么。

Please advice me what I am missing.

推荐答案

我相信这是一个比较普遍的问题,特别是与Android设备。一些流行的解决方案,我所遇到的包括

I believe this is a relatively common problem especially with Android devices. Some of the popular solution I have come across include

  1. 不要指定错误回调:对于一些非常奇怪的原因这个工程,如果没有启用GPS就会出现你的唯一的问题。我不会在生产应用程序,但告知。

  1. Do not specify the error callBack: For some really odd reason this works, your only problem will arise if GPS isn't enabled. I wouldn't advise this in a production app though.

指定enableHighAccuracy。这似乎是工作,但不是自己的,见#3以下

Specify enableHighAccuracy: true in your options. This seems to work but not on its own, see #3 below

在选择指定maximumAge:我用这一个,并将其设置非常高,也许1小时。它所做的是,即使没有GPS目前,科尔多瓦会给你最后一个小时内拍摄的最后一个已知坐标,所以这真的取决于你想要如何精确的坐标,是相对于时间。

Specify maximumAge in options: I use this one and set it very high to maybe 1hour. What it does is that even if there is no GPS currently, cordova will give you the last known coordinates captured within the last hour, so this really depends on how accurate you want your coordinates to be with respect to time.

我通常使用的#2,#3的组合,这个工程合理的PhoneGap的3.x的。

I usually use a combination of #2 and #3 and this works reasonably for Phonegap 3.x.

作为一个边,你可以包括一个选项,通过一个插件在你的错误回调设备的设置,启动一个意图,这样用户就可以使用自己的全球定位系统,如果禁用(喜欢这款手机在谷歌地图没有,当你使用它的导航)。

As an side, you could include an option to start an Intent via a plugin for the device's settings in your error callback so that the user can enable their GPS if disabled (Like google maps on the phone does when you use it for navigation).

我希望这有助于。

这篇关于Android的地理位置使用PhoneGap的code:3错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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