# Baidu Map Distance calculation, calculation results and call Baidu's API

Source: Internet
Author: User
Tags cos

﻿﻿ varDef_pi=Math.PI;//3.14159265359
varDef_2pi=2*Math.PI;//6.28318530712
vardef_pi180=Math.PI/180.0;//0.01745329252

//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;
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;
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

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.