2012-12-16 115 views
2

我有兩組地理座標,我試圖計算它們之間的距離。我已經做了一些挖掘,但無法弄清楚如何去做。我正在嘗試獲取用戶(userLatitude/userLongitude)和地點(placeLatitude/placeLongitude)之間的英里距離。我的座標存儲爲NSStrings,如下所示。有誰知道如何做到這一點?謝謝你們!計算兩組座標之間的距離(NSStrings)

NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLatitude]; 

NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLongitude]; 

NSString *placeLatitude = [element1 objectForKey:@"placeLatitude"]; 

NSString *placeLongitude = [element1 objectForKey:@"placeLongitude"]; 

回答

6

這可以幫助你。嘗試從緯度創建CLLocation長你有

CLLocation* Location1; 
CLLocation* Location2; 
CLLocationDistance distance = [Location1 distanceFromLocation:Location2]; 
0

從下面你可以得到的距離。

CLLocation *locA = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude]; 

CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[element1 objectForKey:@"placeLatitude"] doubleValue] longitude:[[element1 objectForKey:@"placeLongitude"] doubleValue]]; 

CLLocationDistance currentDistance = [locA distanceFromLocation:locB];