如何使用iPhone中的Haversine Formula计算两个地方之间的距离?

Pug*_*gal 0 iphone formula haversine

我想用Haversine公式计算两个地方之间的距离.实际上我有两个地方的纬度和经度值.现在我想用Haversine公式计算那些地方之间的距离.

对于Eg:

 First Place: 
           "lat" :  12.97159870,
           "lng" : -77.59456270

Second Place:
           "lat" :  9.915996999999999,
           "lng" : -78.1218470
        },
Run Code Online (Sandbox Code Playgroud)

现在我想用Haversine Formula计算距离.

请帮帮我.谢谢!

dan*_*emm 12

您可以使用CLLocation类(CoreLocation.framework)方法的distanceFromLocation:(CLLocation*)loc;

CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
CLLocationDistance distance = [locA distanceFromLocation:locB];
[locA release]; [locB release];
Run Code Online (Sandbox Code Playgroud)


Vla*_*mir 6

iOS提供了计算两个地理位置之间距离的标准方法 - 您需要使用CLLocation类:

#import <CoreLocation/CoreLocation.h>

CLLocation *loc1 = [[[CLLocation alloc] initWithLatitude:12.97159870 longitude:-77.59456270] autorelease];

CLLocation *loc2 = [[[CLLocation alloc] initWithLatitude: 9.915996 longitude:-78.1218470] autorelease];

double distance = [loc1 distanceFromLocation: loc2];
Run Code Online (Sandbox Code Playgroud)

您还需要添加CoreLocation.framework链接到您的项目.