varDef_pi=Math.PI;//3.14159265359
varDef_2pi=2*Math.PI;//6.28318530712
vardef_pi180=Math.PI/180.0;//0.01745329252
varDef_r=6370996.81;//radius of Earth
//Using Arc angle method to calculate the distance between two points on the map and inconsistent with the Baidu map calculation results
function getshortdistance(Lon1,LAT1,Lon2,LAT2){
varEw1,ns1,Ew2,ns2;
varDX,Dy,Dew;
varDistance;
//Angle converted to radians
Ew1=Lon1*def_pi180;
NS1=LAT1*def_pi180;
Ew2=Lon2*def_pi180;
NS2=LAT2*def_pi180;
//Longitude Difference
Dew=Ew1-Ew2;
//If it is 180 degrees across east longitude and west longitude, adjust
if (Dew>Def_pi)
Dew=Def_2pi-Dew;
Else if(Dew<-Def_pi)
Dew=Def_2pi+Dew;
DX=Def_r*Math.Cos(ns1)*Dew;//East-west length (projection length on the latitude circle)
Dy=Def_r*(ns1-ns2);//North-south direction length (projection length on the longitude circle)
//Pythagorean theorem to seek the hypotenuse length
Distance= Math.sqrt(DX*DX+Dy*Dy);
returnDistance;
}
//Use the arc angle method to calculate the distance between two points on the map, consistent with the Baidu way
function getlongdistance(Lon1,LAT1,Lon2,LAT2){
varEw1,ns1,Ew2,ns2;
varDistance;
//Angle converted to radians
Ew1=Lon1*def_pi180;
NS1=LAT1*def_pi180;
Ew2=Lon2*def_pi180;
NS2=LAT2*def_pi180;
//Find the Corner (Radian) of the large circle counterclockwise and the spherical sphere
Distance= Math.Sin(ns1)*Math.Sin(ns2)+Math.Cos(ns1)*Math.Cos(ns2)*Math.Cos(Ew1-Ew2);
//adjust to [ -1..1] in the range to avoid overflow
if (Distance>1.0)
Distance= 1.0;
Else if(Distance<-1.0)
Distance= -1.0;
//Find the length of the large circle counterclockwise
Distance=Def_r*Math.ACOs(Distance);
returnDistance;
}
Baidu Map Distance calculation, calculation results and call Baidu's API