2012-01-11 72 views
11

我实际上试图计算MKMapPoints的x和y坐标中最大和最小点之间的距离。为什么MKMetersBetweenMapPoints在交换参数时会给我不同的结果?

对于这一点,我这样做(在y轴的最大距离):

MKMapPoint test1, test2; 
double dist; 
test1.x = 0.0; 
test1.y = 0.0; 
test2.x = 0.0; 
test2.y = MKMapSizeWorld.height; 
dist = MKMetersBetweenMapPoints(test2, test1); 
NSLog(@"Distance %f",dist); 

我在控制台中看到18997878.291251。但是当我改变距离计算为:

dist = MKMetersBetweenMapPoints(test1, test2); 

我得到18873651.664238,所以我不明白有什么区别。我甚至不知道我是否做了正确的事情来获得x和y轴上距离的最大值。

任何帮助将不胜感激。

+1

相关:http://stackoverflow.com/questions/5558854/order-of-cllocation-objects-in-distancefromlocation – Anna 2012-01-11 14:55:43

+0

应日志行是 NSLog(@“Distance%f”,dist); – Damo 2012-01-11 15:36:10

+0

对不起,是一个错字。变量名称是dist。 (更正) – FranciscoAlexis 2012-01-11 18:24:22

回答

4

我想这是一个算法问题。某种近似在达到一定精度时停止。这就是为什么没有换位的原因。您可以尝试示例代码以获得在地图上两点之间的距离,而无需使用MKMapPoints:

- (float) distanceToLPU { 

     useDelegate 

     CLLocationCoordinate2D pointACoordinate = appDelegate.usrLoc; 
     CLLocation *pointALocation = [[CLLocation alloc] initWithLatitude:pointACoordinate.latitude longitude:pointACoordinate.longitude]; 

     CLLocationCoordinate2D pointBCoordinate; 
     pointBCoordinate.latitude = [self.latitude doubleValue]; 
     pointBCoordinate.longitude = [self.longitude doubleValue]; 

     CLLocation *pointBLocation = [[CLLocation alloc] initWithLatitude:pointBCoordinate.latitude longitude:pointBCoordinate.longitude]; 

     float distanceMeters = [pointALocation distanceFromLocation:pointBLocation]; 

     [pointALocation release]; 
     [pointBLocation release]; 

     NSString *dist = [NSString stringWithFormat:@" (%.0f м)", distanceMeters]; 
     NSLog(@"Distance to this LPU:%@", dist); 

     return distanceMeters; 
    } 
相关问题