【问题标题】:Cordova/Phonegap Geolocation科尔多瓦/Phonegap 地理位置
【发布时间】:2015-07-01 16:09:32
【问题描述】:

我正在编写一个 Cordova/Phonegap 应用程序,我想使用 Geolocation 插件来获取经度和纬度。这是我的代码:

$scope.showCoord = function() {
        var onSuccess = function(position) {
            console.log("Latitude: "+position.coords.latitude);
            console.log("Longitude: "+position.coords.longitude);
        };

        function onError(error) {
            console.log("Code: "+error.code);
            console.log("Message: "+error.message);
        };

        navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: 3000, timeout: 5000, enableHighAccuracy: true });
    }

当我用 GPS 尝试这个插件时,它工作得很好,但是当我在没有 GPS 的情况下尝试它时,我收到超时...我将超时更改为 100000 但不起作用。此外,在我的 config.xml 中,我添加了以下代码:

<feature name="Geolocation">
        <param name="android-package" value="org.apache.cordova.GeoBroker" />
    </feature>

我该如何解决?

【问题讨论】:

    标签: cordova gps geolocation phonegap-plugins


    【解决方案1】:

    更新:根据您在下面的评论,我重写了我的答案

    当您设置enableHighAccuracy: true 时,应用程序对操作系统说“给我一个来自 GPS 硬件的高精度位置”。如果在操作系统设置中启用了 GPS,则 GPS 硬件可用,因此请求高精度位置将导致操作系统使用 GPS 硬件来检索高精度位置。但是,如果在操作系统设置中禁用了 GPS,则操作系统无法提供您的应用请求的高精度位置,因此会出现错误回调。

    如果您设置enableHighAccuracy: false,则应用程序对操作系统说“给我一个精确的位置”,因此操作系统将使用单元三角测量/Wifi(或 GPS,如果它当前被另一个应用程序激活)返回一个位置)。

    因此,为了同时满足高精度和低精度的位置,您可以先尝试高精度位置,如果失败,则请求低精度位置。例如:

    var maxAge = 3000, timeout = 5000;
    
    var onSuccess = function(position) {
        console.log("Latitude: "+position.coords.latitude);
        console.log("Longitude: "+position.coords.longitude);
    };
    
    function onError(error) {
        console.log("Code: "+error.code);
        console.log("Message: "+error.message);
    };
    
    navigator.geolocation.getCurrentPosition(onSuccess, function(error) {
        console.log("Failed to retrieve high accuracy position - trying to retrieve low accuracy");
        navigator.geolocation.getCurrentPosition(onSuccess, onError, { maximumAge: maxAge, timeout: timeout, enableHighAccuracy: false });
    }, { maximumAge: maxAge, timeout: timeout, enableHighAccuracy: true });
    

    【讨论】:

    • 对不起,我想在 GPS 处于活动状态时获取它,而当它关闭时,我想从网络接收坐标
    • 它对我不起作用...如果我将超时设置为 1 分钟,我总是会超时...我正在我的 android 设备上进行测试
    • 此外,如果我关闭连接,当我必须收到 POSITION_UNAVAILABLE 时,我会收到超时
    • 如何在没有 GPS 的情况下测试场景?在 Android 4 中,Location mode High Accuracy 将 GPS+Wifi+Cell 定位;使用 Wifi+Cell 位置节省电池电量
    • 我在我的设备上安装了我的应用程序,并在我的浏览器上安装了它,因此我可以关闭或打开我的 gps、wifi 但无法正常工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多