Objective c 在MKMapView上将经纬度坐标转换为笛卡尔坐标

Objective c 在MKMapView上将经纬度坐标转换为笛卡尔坐标,objective-c,mkmapview,cgpoint,Objective C,Mkmapview,Cgpoint,我有三个点A、B和p,它们都是由它们的经度/纬度坐标定义的 现在我正在尝试实现一个算法,请参见,以计算点p和线AB之间的最短距离: CLLocationCoordinate2D a; CLLocationCoordinate2D b; CLLocationCoordinate2D p; a.latitude = 3; a.longitude = 3; b.latitude = 3.01; b.longitude = 3.01; p.latitude = 11; p.longitude = 11;

我有三个点A、B和p,它们都是由它们的经度/纬度坐标定义的

现在我正在尝试实现一个算法,请参见,以计算点p和线AB之间的最短距离:

CLLocationCoordinate2D a;
CLLocationCoordinate2D b;
CLLocationCoordinate2D p;
a.latitude = 3;
a.longitude = 3;
b.latitude = 3.01;
b.longitude = 3.01;
p.latitude = 11;
p.longitude = 11;

CGPoint A = [mapView convertCoordinate:a toPointToView:mapView];
CGPoint B = [mapView convertCoordinate:b toPointToView:mapView];
CGPoint P = [mapView convertCoordinate:p toPointToView:mapView];    

if ((P.x - A.x) * (B.x - A.x) + (P.y - A.y) * (B.y - A.y) < 0) {
    return sqrt(pow((P.x - A.x), 2) + pow((P.y - A.y), 2));
}
else if((P.x - B.x) * (A.x - B.x) + (P.y - B.y) * (A.y - B.y) < 0) {
    return sqrt(pow(P.x - B.x, 2) + pow(P.y - B.y, 2));
}
else {return ABS((P.x * (A.y - B.y) + P.y * (B.x - A.x) + (A.x * B.y - B.x * A.y)) / sqrt(pow(B.x - A.x, 2) + pow(B.y - A.y, 2)));
}

这种方法似乎是错误的,因为结果48与预期的1252000米的距离相差甚远。有人知道我从经度/纬度到笛卡尔坐标的转换哪里错了吗?可能转换后的笛卡尔坐标必须按比例放大

结果是屏幕上的CG点,所以你必须以某种方式将其转换为米。特别使用使用MKMapPoints的第二个示例方法可能更容易。首先使用MKMapPointForCoordinate将CLLocationCoordinate2Ds转换为MKMapPoints,然后在链接的答案中调用该方法。太好了,这非常有效!谢谢你快速准确的回答!
(CGPoint)convertCoordinate:(CLLocationCoordinate2D)coordinate
           toPointToView:(NSView *)view