百度地图Api 根据两个坐标点计算距离


百度地图Android Sdk的Api里面,没有现成的直接获取两个坐标点之间距离的方法,但是,在jsapi里面,有直接计算距离的方法。

class Point:  
    pass  
  
def max(a,b):  
    if a>b:  
        return a  
    return b  
def min(a,c):  
    if a>c:  
        return c  
    return a  
  
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)  
    return a  
  
def ew(a, b, c):  
      
    while a > c:  
        a -= c - b  
    while a < b:  
        a += c - b  
    return a  
          
  
def oi(a):  
    return math.pi * a / 180  
  
def Td(a, b, c, d):   
    return 6370996.81 * math.acos(math.sin(c) * math.sin(d) + math.cos(c) * math.cos(d) * math.cos(b - a))  
  
def Wv(a, b):  
    if not a or not b:   
        return 0;  
    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);  
    return Td(oi(a.lng), oi(b.lng), oi(a.lat), oi(b.lat))  
  
def getDistance(a, b):  
    c = Wv(a, b);  
    return c  

所以,根据上面的方法,我们把计算距离的方法用java语言实现如下:

    public static double getDistance(double lat_a, double lng_a, double lat_b, double lng_b){
        double pk = 180 / 3.14169;
        double a1 = lat_a / pk;
        double a2 = lng_a / pk;
        double b1 = lat_b / pk;
        double b2 = lng_b / pk;
        double t1 = Math.cos(a1) * Math.cos(a2) * Math.cos(b1) * Math.cos(b2);
        double t2 = Math.cos(a1) * Math.sin(a2) * Math.cos(b1) * Math.sin(b2);
        double t3 = Math.sin(a1) * Math.sin(b1);
        double tt = Math.acos(t1 + t2 + t3);
        return 6371000 * tt;
    }

参考:

http://blog.csdn.net/mad1989/article/details/9933089

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

相关