使用系统自带API计算两坐标之间的距离

来源:互联网 发布:穿t恤不好看 知乎 编辑:程序博客网 时间:2024/05/22 00:46

层次关系:

  • Core Location
  • CLLocation
  • distanceFromLocation:

Instance MethoddistanceFromLocation:









Returns the distance (in meters) from the receiver’s location to the specified location. 


Declaration

- (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;

Parameters

location

The other location. 

Return Value

The distance (in meters) between the two locations.

Discussion

This method measures the distance between the two locations by tracing a line between them that follows the curvature of the Earth. The resulting arc is a smooth curve and does not take into account specific altitude changes between the two locations.


// 注意:经纬度不要写颠倒
CLLocation*curLocation = [[CLLocationalloc]initWithLatitude:curLatlongitude:curLng];

CLLocation*oilLocation = [[CLLocationalloc]initWithLatitude:oilLatlongitude:oilLng];
// 注意:单位是米,而且是坐标之间的直线距离(最好是GPS-84坐标,即大家常说的GPS坐标,apple就是使用的这个标准)
CLLocationDistancedis = [oilLocation distanceFromLocation:curLocation];

补充:
GPS坐标(WGS-84坐标)即地球坐标,国际通用的坐标;
中国使用的是对WGS-84进一步加密后的GCJ-02;
百度是对国家测绘局的GCJ-02坐标的进一步加密后的BD-09;

/// GPS设备采集的原始GPS坐标(WGS-84)
/// GCJ-02坐标,google地图、soso地图、aliyun地图、mapabc地图和amap地图所用坐标
/// BD09ll 百度经纬度坐标


度换算成度分秒


度:E 108.90593度    N 34.21630度
如何将度:108.90593度换算成度分秒;东经E 108度54分22.2秒,转换方法是将108.90593整数位不变取108(度),用0.90593*60=54.3558,取整数位54(分),0.3558*60=21.348再取整数位21(秒),故转化为108度54分21秒。
同样将度分秒:东经E 108度54分22.2秒 换算成度的方法如下:108度54分22.2秒=108+(54/60)+(22.2/3600)=108.90616度;因为计算时小数位保留的原因,导致正反计算存在一定误差,但误差影响不是很大,1秒的误差就是几米的样子,GPS车友可以用上述方法换算成自己需要的单位坐标。
 
 
关于经纬度十进制表示法:

对于两个点,在纬度相等的情况下:
经度每隔0.00001度,距离相差约1米;每隔0.0001度,距离相差约10米;每隔0.001度,距离相差约100米;每隔0.01度,距离相差约1000米;每隔0.1度,距离相差约10000米。

对于两个点,在经度相等的情况下:
纬度每隔0.00001度,距离相差约1.1米;每隔0.0001度,距离相差约11米;每隔0.001度,距离相差约111米;每隔0.01度,距离相差约1113米;每隔0.1度,距离相差约11132米。


另一种距离计算方法:

把度化成弧度的公式:
     弧度=度×π/180°

把弧度化成度的公式:
      度=弧度×180°/π

关于弧度和角度的转换:
http://blog.csdn.net/hongxiali/article/details/5355954

Google地图提供的方法:
公式:

对上面的公式解释如下:
1.Lat1 Lung1 表示A点经纬度,Lat2 Lung2 表示B点经纬度;
2.a=Lat1 – Lat2 为两点纬度之差  b=Lung1 -Lung2 为两点经度之差;
3.6378.137为地球半径,单位为千米;
计算出来的结果单位为千米,若将半径改为米为单位则计算的结果单位为米。
计算精度与谷歌地图的距离精度差不多,相差范围在0.2米以下。

// 将角度转为弧度
+ (float)radians:(float)degrees
{
    return (degrees * M_PI) / 180.0;
}

// 根据经纬度换算出直线距离
+ (float)getDistance:(float)lat1 lng1:(float)lng1 lat2:(float)lat2 lng2:(float)lng2
{
    // 地球半径 单位m
    int R = 6378137;
    
    // 将角度转为弧度
    float radLat1 = [self radians:lat1];
    
    float radLat2 = [self radians:lat2];
    
    float radLng1 = [self radians:lng1];
    
    float radLng2 = [self radians:lng2];
    
    // 结果
    float s = acos(cos(radLat1)*cos(radLat2)*cos(radLng1-radLng2)+sin(radLat1)*sin(radLat2))*R;
    
    // 精度
    s = round(s * 10000) / 10000;
    
    return  round(s);
}

参考资料:
// 计算两个经纬度之间的距离
http://www.cnblogs.com/mafeng/p/5850594.html


0 0
原创粉丝点击