2014-05-23 120 views
1

如何计算点(X,Y)的位置,也就是从点1(已经LAT1和lon1 coords)使用用于距离d远如上图计算由距离从另一个坐标坐标

enter image description here

已知参数:
POINT1 = [LAT1,lon1],
POINT2 = [LAT2,lon2],
距离= d,(米)
角度=α= ATAN((LAT2 - LAT1)/ (lon2 - lon1))

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

在一个字,我需要像这样

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

例如反之亦然按给定的距离和角度计算位置2。

我已经试过

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

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

+0

我认为答案是使用毕达哥拉斯定理,这可能是平坦的表面和很短的距离。对于曲面和更适合更大距离的计算,请尝试以下答案:http://stackoverflow.com/a/6634982/467105 – Anna

回答

2
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) 
+0

非常感谢。我想我理解逻辑。 但你能解释一下,为什么result.y = Point2.x + ...而不是Point1.x + ...? – arturdev

+0

修好了,对不起 –