按距离另一个坐标计算坐标

时间:2014-05-23 07:13:13

标签: ios objective-c

如何计算距离d远离点1(具有lat1和lon1坐标)的点(x,y)的位置,如图所示

enter image description here

已知参数:
Point1 = [lat1,lon1],
Point2 = [lat2,lon2],
距离= d,(以米为单位)
Angle =α= atan((lat2 - lat1)/(lon2 - lon1))

需要找到: 目标点:x和y值。

总之,我需要这样的东西,反之亦然

CLLocation *location1;
CLLocation *location2; 
double distance =  [location1 distanceFromLocation:location2];

e.g。按给定的距离和角度计算位置2。

我尝试了什么

double lat = location1.coordinate.latitude + distance * sin(alpha);
double lon = location1.coordinate.longitude + distance * cos(alpha);

但这些值是错误的,因为1纬度和1经度不等于1米。

2 个答案:

答案 0 :(得分:3)

    CLLocation Point1;
    CLLocation Point2;
    float targetDistance;

    float length = sqrt((Point1.x-Point2.x)*(Point1.x-Point2.x) + (Point1.y-Point2.y)*(Point1.y-Point2.y));

    CLLocation result;
    result.x = Point1.x + (Point2.x-Point1.x)*(targetDistance/length);
    result.y = Point1.y + (Point2.y-Point1.y)*(targetDistance/length);

或换句话说Point1 + normalized(Point2-Point1)*targetDistance

由于你有一个角度,你也可以这样做:

Point1 + (cos(angle)*targetDistance, sin(angle)*targetDistance)

答案 1 :(得分:0)

前段时间我写了一篇详细的博客,解释了两种不同的计算方法及其背后的数学证明。一种方法是使用余弦定律计算arcLength,另一种方法是使用Haversine公式。您可以在此处找到博客:http://rbrundritt.wordpress.com/2008/10/14/calculate-distance-between-two-coordinates/