2013-03-05 118 views
2

我正在使用Xcode項目,並且遇到了一些問題。 在我的應用程序中,我有一個GPS位置,我在1000米範圍內有一些出租車服務。我想找到我附近最近的服務。出租車服務位置從JSON解析。它看起來像從GPS位置計算距離

NSNumber * latitude = [[diction objectAtIndex:13]objectAtIndex:1]; 
NSLog(@"lng is =>%@", latitude); 
NSNumber * longitude = [[diction objectAtIndex:13]objectAtIndex:2]; 
NSLog(@"lat is =>%@", longitude); 

而且,我有自己的位置,這樣的:

[locationManager stopUpdatingLocation]; 
NSLog(@"latitude: %f", newLocation.coordinate.latitude); 
NSLog(@"longitude: %f", newLocation.coordinate.longitude); 

我怎麼能減去緯度和經度以上,爲了找到最接近的服務。 最好的問候。

+0

[看看這個答案](http://stackoverflow.com/a/2318927/119114) – Nate 2013-03-05 03:44:26

回答

2
CLLocation *oldLocation = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]]; 
    double distance = [newLocation distanceFromLocation:oldLocation]; 

使用此可能會對您有所幫助,謝謝。

+0

嗨,我沒有清楚地理解你的代碼,oldLocation意味着當前的位置?而newLocation是右邊的服務位置?基於我的代碼,我們有:CLLocation * newLocation = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];那是對的嗎? lat和long在newLocation中是我從JSON數據獲得的值。再次感謝,我非常感謝您的幫助。 – 2013-03-11 16:39:52