我刚刚开始使用HTML5和Phonegap进行一些应用开发。我正在尝试使用GPS - 禁用Wifi - 来显示lon / lat坐标。代码工作......有点像。
如果我打开应用程序,我找不到该位置。如果我打开任何其他使用地理定位的应用程序 - 谷歌地图或GPS测试仪 - 然后切换回我的应用程序,它会工作并更新坐标。这就像我的应用程序没有打开GPS传感器,但一旦它打开,我的应用程序可以访问它。
我错过了什么吗?
代码;
<script>
window.onload = function() {
var startPos;
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
enableHighAccuracy: true;
startPos = position;
document.getElementById("startLat").innerHTML = startPos.coords.latitude;
document.getElementById("startLon").innerHTML = startPos.coords.longitude;
}, function(error) {
alert("Error occurred. Error code: " + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from locaton provider)
// 3: timed out
});
navigator.geolocation.watchPosition(function(position) {
enableHighAccuracy: true;
document.getElementById("currentLat").innerHTML = position.coords.latitude;
document.getElementById("currentLon").innerHTML = position.coords.longitude;
document.getElementById("distance").innerHTML =
calculateDistance(startPos.coords.latitude, startPos.coords.longitude,
position.coords.latitude, position.coords.longitude);
});
}
};
// Reused code - copyright Moveable Type Scripts - retrieved May 4, 2010.
// http://www.movable-type.co.uk/scripts/latlong.html
// Under Creative Commons License http://creativecommons.org/licenses/by/3.0/
function calculateDistance(lat1, lon1, lat2, lon2) {
var R = 6371; // km
var dLat = (lat2-51.202399).toRad();
var dLon = (lon2-parseFloat(-1.479645)).toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(51.202399.toRad()) * Math.cos(lat2.toRad()) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d.toFixed(2);
}
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
</script>
祝你好运 安迪
答案 0 :(得分:0)
使用org.apache.cordova.geolocation插件。