我使用以下两种方法使用纬度和经度计算谷歌地图中两点之间的距离。
TYPE I:
function CalcDistanceBetween(lat1, lon1, lat2, lon2) {
//Radius of the earth in: 1.609344 miles, 6371 km | var R = (6371 / 1.609344);
var R = 6371; // Radius of earth in Miles
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
alert("d==" + d);
return d;
}
alert(CalcDistanceBetween(13.125511084202,80.029651635576,12.9898593006481,80.2497744326417));
function toRad(Value) {
/** Converts numeric degrees to radians */
return Value * Math.PI / 180;
}
TYPE II:
var p1 = new google.maps.LatLng(13.125511084202,80.029651635576);
var p2 = new google.maps.LatLng(12.9898593006481,80.2497744326417);
alert(calcDistance(p1, p2));
//calculates distance between two points in km's
function calcDistance(p1, p2){
return (google.maps.geometry.spherical.computeDistanceBetween(p1, p2) /
1000).toFixed(2);
}
以上两种类型的距离均为28 公里。但是当我检查具有相同经度和经度的谷歌地图时,它返回 36.8 公里。示例检查我的代码和谷歌地图如下所示..:
你建议我得到准确的结果吗?