android phonegap地理定位模拟器

时间:2012-02-09 16:52:22

标签: cordova

我尝试在phonegap平台上的android模拟器上使用geolocation获取坐标。但似乎行不通。代码应该是正确的,我在清单中添加权限,使用高精度标记,我该如何解决这个问题?谢谢!

<!DOCTYPE html>
<html>
  <head>
    <title>Device Properties Example</title>

    <script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
    <script type="text/javascript" charset="utf-8">

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

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

    // onSuccess Geolocation
    //
    function onSuccess(position) {
        var element = document.getElementById('geolocation');
        element.innerHTML = 'Latitude: '           + position.coords.latitude              + '<br />' +
                            'Longitude: '          + position.coords.longitude             + '<br />' +
                            'Altitude: '           + position.coords.altitude              + '<br />' +
                            'Accuracy: '           + position.coords.accuracy              + '<br />' +
                            'Altitude Accuracy: '  + position.coords.altitudeAccuracy      + '<br />' +
                            'Heading: '            + position.coords.heading               + '<br />' +
                            'Speed: '              + position.coords.speed                 + '<br />' +
                            'Timestamp: '          + new Date(position.timestamp)          + '<br />';
    }

    // onError Callback receives a PositionError object
    //
    function onError(error) {
        alert('code: '    + error.code    + '\n' +
              'message: ' + error.message + '\n');
    }

    </script>
  </head>
  <body>
    <p id="geolocation">Finding geolocation...</p>
  </body>
</html>

2 个答案:

答案 0 :(得分:1)

您是否在模拟器中设置了位置?

e.g:

$ telnet localhost 5554
$ geo fix -4.2 55.85

答案 1 :(得分:0)

您使用的是什么版本的模拟器?如果您使用的是2.3.x,请注意地理定位似乎不起作用:http://code.google.com/p/android/issues/detail?id=13046

你必须降到2.2。