Cordova/Phonegap 地理位置

Cordova/Phonegap Geolocation

我正在编写一个 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>

我该如何解决?

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

当您设置 enableHighAccuracy: true 时,应用程序正在对 OS "give me a high accuracy position from the GPS hardware" 说。如果在 OS 设置中启用了 GPS,则 GPS 硬件可用,因此请求高精度位置将导致 OS 使用 GPS 硬件以检索高精度位置。但是,如果在 OS 设置中禁用了 GPS,则 OS 无法提供您的应用请求的高精度位置,因此会出现错误回调。

如果您设置 enableHighAccuracy: false,应用程序会告诉 OS "give me a location of any accuracy",因此 OS 将 return 使用单元格 [=] 的位置25=](或 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 });