Android phonegap GPS准确性

时间:2013-04-18 08:26:44

标签: android jquery cordova geolocation phonegap-build

在下面的代码中,我试图从phonegap示例中获取GPS位置。我在实际设备上尝试此操作,而不是在模拟器上我每次都有时间。我从未获得成功警报。什么是我做错了。我已启用GPS和互联网

JS

document.addEventListener("deviceready", onDeviceReady, false);

var watchID = null;

// Cordova is ready
//
function onDeviceReady() {
// Throw an error if no update is received every 30 seconds
var options = { timeout: 30000 };
watchID = navigator.geolocation.watchPosition(onSuccess, onError, options);
}

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

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

HTML

 <html>
 <head>
 <title>Cordova</title>
 <script type="text/javascript" charset="utf-8" src="cordova-2.1.0.js"></script>
 <script type="text/javascript" src="js/jquery.mobile-1.2.0/jquery-1.8.1.min.js"></script>
 <script type="text/javascript" src="js/jquery.mobile-1.2.0/jquery.mobile-1.2.0.min.js"> </script>
 <link rel="stylesheet" href="js/jquery.mobile-1.2.0/jquery.mobile-1.2.0.min.css" />
 <script type="text/javascript" src="js/index.js"></script>
 </head>
 <body >
 <h1>Hello World</h1>
 <p id="geolocation">Finding geolocation...</p>
 </body>
 </html>

1 个答案:

答案 0 :(得分:1)

尝试使用以下选项:

var options = { timeout: 30000, enableHighAccuracy: true };