0

我使用 cordova-2.0.0 并希望定义用户位置的坐标。此外,我只想获得 gps 坐标,而不是网络。我该怎么做?为什么 gps-logo 不显示在状态栏中?

我的代码 index.html 脚本:

function onSuccess(position) {
        var element = document.getElementById('geolocation');
    element.innerHTML = 'Latitude: '+ position.coords.latitude + '<br />' +
                         'Longitude: '+ position.coords.longitude + '<br />';
    }

// onError Callback receives a PositionError object
    //
function onError(error) {
    var element = document.getElementById('geolocation');
    element.innerHTML = 'code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n';
}

// Wait for PhoneGap to load
    document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is ready
    function onDeviceReady() {
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
4

3 回答 3

1

哦,这很容易。选项 - enableHighAccuracy: true。及其工作:)

function onDeviceReady() {
                // Update every 3 seconds
                var options = { frequency: 3000, enableHighAccuracy: true };
                watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
            }

但是我如何使用cordova api定义提供者?例如:如果 gps 关闭,则应用程序会发出警报(“请打开 gps!”);

于 2012-08-08T06:03:02.043 回答
0

If you can't see the GPS logo in the Status-bar, it's because GPS doesn't start. Maybe one JS-file is not loaded ?

Cordova/phonegap is not native code, there are only three options you could put on watchPosition() function : maximumAge, timeout and enableHighAccuracy (to use GPS). You can just set enableHighAccuracy at true to use GPSProvider and not NetworkBaseProvider. Maybe you can analyse the error message in onError function to look at if a special message is sent when GPS is disabled.

Edit : In my test, no special message appaers, just a timeout message : "Position retrieval timed out." So I think you can't check if GPS is on or off in cordova...

In my opinion, Cordova/phonegap (and all the "crossplatforms" technologies) is not a good choice to develop an app using GPS and others managers like TelephonyManager.

Nothing can replace native code for that.

I have an other problem : GPS dont stop when I use clearWatch(): Here, my code :

var currentTracking = false;
var idwatch = null;

function switchTracking() {
    //when button is pressed to start GPS
    if (!currentTracking) {
        currentTracking = true;
        document.getElementById('switchTracking').value = "Stop GPS"
        tracking();
    }

    //when button is pressed to stop GPS
    else {
        navigator.geolocation.clearWatch(idwatch);
        currentTracking = false;
        document.getElementById('switchTracking').value = "Start GPS"
    }
}

function tracking() {
    if (currentTracking) {
        idwatch = navigator.geolocation.watchPosition(onSuccess, onError,{  timeout: 5000, enableHighAccuracy: true });
    }
    else{
        navigator.geolocation.clearWatch(idwatch);
    }
}

function onSuccess(position) {
   // alert("Gps");

    var lat = document.getElementById('lat');
    var long = document.getElementById('long');
    lat.innerHTML = position.coords.latitude;
    long.innerHTML = position.coords.longitude;
}

// onError Callback receives a PositionError object
//
function onError(error) {
    alert('code: ' + error.code + '\n' +
            'message: ' + error.message + '\n');
}
于 2013-11-06T16:57:05.377 回答
0

对于您更新的要求,您必须创建一个本机插件来检查 GPS 状态,并根据状态在您的 html 页面中发出警报。

访问此处获取有关在 android 上创建本机插件的教程。

于 2012-08-08T06:14:38.390 回答