2012-01-11 68 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; 
    } 
相關問題