HTML5地理定位中是否有任何可用的函数来获取行进距离的位置回调,例如每1000米,我想要一个lat lng。
我可以使用
navigator.geolocation.watchPosition(success, error, options);
并且在成功回调中计算此点和前一点之间的距离,如果它大于1000会触发我的距离回调,还有其他解决方案吗?
答案 0 :(得分:1)
使用此功能获取lat
和long
:
function initiate_watchlocation() {
if (watchProcess == null) {
watchProcess = navigator.geolocation.watchPosition(onSuccess, onError);
}
}
var onSuccess = function(position) {
var lat= position.coords.latitude;
var long= position.coords.longitude;
};
function onError(error) {
alert_box('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
并将其保存在数据库中并将其映射到lat1
和lon1
,然后继续关注lat
和lon
并将其映射到lat2
和{{ 1}}分别。并继续执行以下功能。
lon2
然后执行此操作:
function distance(lat1, lon1, lat2, lon2, unit) {
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var radlon1 = Math.PI * lon1/180
var radlon2 = Math.PI * lon2/180
var theta = lon1-lon2
var radtheta = Math.PI * theta/180
var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
dist = Math.acos(dist)
dist = dist * 180/Math.PI
dist = dist * 60 * 1.1515
if (unit=="K") { dist = dist * 1.609344 }
if (unit=="N") { dist = dist * 0.8684 }
return dist
}