/**
*@desc HTML5+获取当前的经纬度
**/
function getPos(){
var coords = {
lat:0,
lon:0
};
plus.geolocation.getCurrentPosition(function(p){
coords.lat = p.coords.latitude; //纬度
coords.lon = p.coords.longitude; //经度
});
return coords;
}
/**
* @desc 根据两点间的经纬度计算距离 单位:M
* @param float $lat 纬度值
* @param float $lng 经度值
*/
function getDistance($lat1, $lng1, $lat2, $lng2){
$earthRadius = 6367000;
$lat1 = ($lat1 * Math.PI ) / 180;
$lng1 = ($lng1 * Math.PI ) / 180;
$lat2 = ($lat2 * Math.PI ) / 180;
$lng2 = ($lng2 * Math.PI ) / 180;
$calcLongitude = $lng2 - $lng1;
$calcLatitude = $lat2 - $lat1;
$stepOne = Math.pow(Math.sin($calcLatitude / 2), 2) + Math.cos($lat1) * Math.cos($lat2) * Math.pow(Math.sin($calcLongitude / 2), 2);
$stepTwo = 2 * Math.asin(Math.min(1, Math.sqrt($stepOne)));
$calculatedDistance = $earthRadius * $stepTwo;
return Math.round($calculatedDistance);
}
html5+js计算两地之间距离
我爱模板网今天做项目遇到一个技术点:已知客户的经纬度,计算客户距离当前距离自己的位置。这个在诸如饿了么、美团等APP上都有应用。下面,就把代码贡献给大家:
