# Baidu Map API calculates distances based on two coordinate points

Source: Internet
Author: User
Tags acos cos sin

Baidu maps Android SDK API inside, there is no immediate way to get the distance between two coordinate points, but, in the JSAPI, there is a direct way to calculate the distance.

`class Point:pass def max (A, b):ifA>B:returnareturnb def min (a,c):ifA>C:returnCreturna def LW (A, B, c): # b! = N && (a =Math.max (A, b)); # c! = N && (a =Math.min (A, c)); A=Max (A, b) a=min (a,c)returna def ew (A, B, c): whileA >c:a-= C-b whileA <b:a+ = C-breturnA def oi (a):returnMath.PI * a/180def Td (A, B, C, D):return6370996.81 * Math.acos (Math.sin (c) * Math.sin (d) + Math.Cos (c) * Math.Cos (d) * Math.Cos (B-a) def Wv (A, b):ifNot A or not B:return0; A.LNG= EW (A.LNG,-180, 180); A.lat= LW (A.lat,-74, 74); B.LNG= EW (B.LNG,-180, 180); B.lat= LW (B.lat,-74, 74); returnTd (Oi (a.lng), Oi (b.lng), Oi (A.lat), Oi (B.lat)) def getdistance (A, B): C=Wv (A, b); returnC`

So, based on the above method, we have implemented the method of calculating distances in the Java language as follows:

`     Public Static DoubleGetdistance (DoubleLat_a,DoubleLng_a,DoubleLat_b,DoubleLng_b) {        DoublePK = 180/3.14169; DoubleA1 = lat_a/PK; DoubleA2 = lng_a/PK; DoubleB1 = lat_b/PK; DoubleB2 = Lng_b/PK; DoubleT1 = Math.Cos (A1) * MATH.COS (A2) * MATH.COS (B1) *Math.Cos (B2); DoubleT2 = Math.Cos (A1) * Math.sin (A2) * MATH.COS (B1) *Math.sin (B2); DoubleT3 = Math.sin (A1) *Math.sin (B1); Doublett = math.acos (t1 + t2 +T3); return6371000 *tt; }`

Reference:

http://blog.csdn.net/pleasurelong/article/details/26855049

Baidu Map API calculates distances based on two coordinate points

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.