【问题标题】:How to stop watchPosition() Position retrieval timed out error?如何停止 watchPosition() 位置检索超时错误?
【发布时间】:2013-12-06 17:42:27
【问题描述】:

我正在开发一个 phonegap 应用程序,它在其功能中使用设备的当前位置,但在 Iphone 上进行测试时,该应用程序每 60 秒会给出一条错误消息,如下所示:

我用来获取位置的源代码是:

function onDeviceReady() {

    var options = { maximumAge: 0, timeout: 10000,enableHighAccuracy: false };
    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);

    function onSuccess(position) {
        if (position.coords.latitude) {
            sessionStorage.setItem("appLatitude", position.coords.latitude);
            sessionStorage.setItem("appLongitude", position.coords.longitude);
        }
        //alert(sessionStorage.getItem("appLatitude") + "&" + sessionStorage.getItem("appLongitude"));
    }

    function onError(error) {
        z.appNotification.getAlert('code: ' + error.code + '\n' +
                'message: ' + error.message + '\n');
    }

}

如何阻止该错误消息的提示?

【问题讨论】:

    标签: android iphone cordova geolocation


    【解决方案1】:

    如果您只想获取位置一次使用:

    navigator.geolocation.getCurrentPosition(onSuccess, onError);
    

    如果您出于任何原因想停止观看,请使用:

    navigator.geolocation.clearWatch(watchID);
    

    【讨论】:

    • 每当位置发生变化并且由于该原因而出现问题时,该应用程序都需要获取设备的位置。每当手机位置发生变化时,应用都会显示超时错误。
    • 在 iOS cordova 应用程序上定期(例如每秒)调用navigator.geolocation.getCurrentPosition() 而不是使用watchPosition() 避免了“超时”错误。 (cordova-ios 5.1.0)
    猜你喜欢
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多