计算两个经纬度之间距离

最近项目里有涉及到经纬度距离问题,在此记录一下实现方法。
一、ios自带方法实现

CLLocation *cllocation1=[[CLLocation alloc] initWithLatitude:latitude1  longitude:longitude1];
CLLocation * cllocation2=[[CLLocation alloc] initWithLatitude: latitude2 longitude:longitude2];
CLLocationDistance kilometers=[cllocation1 distanceFromLocation: cllocation2]/1000;
 NSLog(@"公里:%f",kilometers);

二、自定义实现

- (double)getDistance:(double)lat1 lng1:(double)lng1 lat2:(double)lat2 lng2:(double)lng2
{
    //地球半径
    int R = 6378137;
    //将角度转为弧度
   double Lat1 = lat1*3.1415926/180;
   double Lat2 = lat2*3.1415926/180;
   double Lng1 = lng1*3.1415926/180;
   double Lng2 = lng2*3.1415926/180;
   double s = acos(cos(Lat1)*cos(Lat2)*cos(Lng1-  Lng2)+sin(Lat1)*sin(Lat2))*R;
    //精度
    s = round(s* 10000)/10000;
    NSLog(@"round--->%f",round(s));
    return  round(s);
}

你可能感兴趣的:(计算两个经纬度之间距离)