phonegap地理定位不起作用

时间:2014-12-16 11:25:11

标签: cordova geolocation

你好,来自phonegap的地理位置对我不起作用。这是我的代码。

function getGeo() {
    alert("begin getGeo");
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

function onSuccess(position) {
    alert("onsuccess");
    var test = '"lat":"' + (position.coords.latitude) + '", "long":"' + (
        position.coords.longitude);
    alert(test);
}

function onError(error) {
    alert('code: ' + error.code + '\n' + 'messagee: ' + error.message +
        '\n');
}

我收到警报"开始getGeo"而且没有任何反应。

在我的config.xml文件中,我添加了这一行。

<gap:plugin name="org.apache.cordova.geolocation" version="0.3.10" />

2 个答案:

答案 0 :(得分:0)

试试这个:

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

function onSuccess(position) {
    //Lat long will be fetched and stored in session variables
    //These variables will be used while storing data in local database 
    localStorage.setItem("lat", position.coords.latitude);
    localStorage.setItem("long", position.coords.longitude);
}
function onError(error) {
    if (error.code == error.TIMEOUT)
    {
        // Attempt to get GPS loc timed out after 5 seconds, 
        // try low accuracy location
        navigator.geolocation.getCurrentPosition(
               onSuccess, 
               errorCallback_lowAccuracy,
               {maximumAge:600000, timeout:10000, enableHighAccuracy: false});
        return;
    }
    alert("GeoLocation didn't worked, try to reboot your internet connection or your device, the app will close now.");
    navigator.app.exitApp();
}

function errorCallback_lowAccuracy(error) {
    alert("GeoLocation didn't worked, try to reboot your internet connection or your device, the app will close now.");
    navigator.app.exitApp();
}

答案 1 :(得分:0)

您忘记在代码前插入此文件。

https://github.com/apache/cordova-plugin-geolocation/blob/master/www/geolocation.js

此文件中的

getCurrentPosition:function(successCallback, errorCallback, options)函数。