获取某一个地点的经纬度
复制代码- (void)getLongitudeAndLatitudeWithCity:(NSString *)city
{
//city可以为中文
NSString *oreillyAddress = city;
CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
[myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
if ([placemarks count] > 0 && error == nil)
{
NSLog(@"Found %lu placemark(s).", (unsigned long)[placemarks count]);
CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
NSLog(@"Longitude = %f", firstPlacemark.location.coordinate.longitude);
NSLog(@"Latitude = %f", firstPlacemark.location.coordinate.latitude);
}
else if ([placemarks count] == 0 && error == nil)
{
NSLog(@"Found no placemarks.");
}
else if (error != nil)
{
NSLog(@"An error occurred = %@", error);
}
}];
}
计算两个地点之间的距离
复制代码
- (double)distanceByLongitude:(double)longitude1 latitude:(double)latitude1 longitude:(double)longitude2 latitude:(double)latitude2{
CLLocation* curLocation = [[CLLocation alloc] initWithLatitude:latitude1 longitude:longitude1];
CLLocation* otherLocation = [[CLLocation alloc] initWithLatitude:latitude2 longitude:longitude2];
double distance = [curLocation distanceFromLocation:otherLocation];//单位是m
return distance;
}
首先我们可以用上面的getLongitudeAndLatitudeWithCity方法获取某一个地点的经纬度。比如我们获取北京和上海的经纬度分别为:北京Longitude = 116.405285,Latitude = 39.904989 上海Longitude = 121.472644, Latitude = 31.231706, 那么北京和上海之间的距离就是:
复制代码










