2013-03-05 114 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