Rag*_*arr 2 gps geolocation phonegap-plugins cordova
我正在写一个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 });
}
Run Code Online (Sandbox Code Playgroud)
当我使用GPS尝试这个插件时它工作得很好但是当我尝试没有GPS时我收到超时...我将超时更改为100000但不起作用.此外,在我的config.xml中,我添加了以下代码:
<feature name="Geolocation">
<param name="android-package" value="org.apache.cordova.GeoBroker" />
</feature>
Run Code Online (Sandbox Code Playgroud)
我该如何解决?
更新:根据您在下面的评论我改写了我的答案
当你设置时enableHighAccuracy: true,应用程序会对操作系统说"从GPS硬件给我一个高精度位置".如果在OS设置中启用GPS,则GPS硬件可用,因此请求高精度位置将使OS接合GPS硬件以检索高精度位置.但是,如果在OS设置中禁用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 });
Run Code Online (Sandbox Code Playgroud)