使用CLLocation计算两个坐标之间的距离

我正在使用CLLocationDistance获得两点之间的距离,但是当我将当前位置传递给它时,出现错误。

 CLLocation *current = [[CLLocation alloc] initWithLatitude:startLocation.coordinate.latitude longitude:startLocation.coordinate.longitude]; CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lat"] doubleValue] longitude:[[[getName objectAtIndex:indexPath.row] objectForKey:@"lon"] doubleValue]]; //Here the current location gives me an error "Initializing cllocation with an expression incompatible format" CLLocationDistance *itemDist = [itemLoc distanceFromLocation:current]; NSLog(@"Distance: %@", itemDist); 

你得到的错误实际上是:

使用不兼容的types“CLLocationDistance”(又名“double”)的expression式初始化“CLLocationDistance *”(又名'double *')

这是说你正在初始化itemDist (你已经声明为一个CLLocationDistance * )到返回一个CLLocationDistance (注意不星号)的东西。

CLLocationDistance不是一个对象。
它只是一个原始types(特别是double – 见核心位置数据types参考 )。

所以,而不是声明itemDist作为指向 CLLocationDistance指针 ,只需将其声明为一个CLLocationDistance (不星号):

 CLLocationDistance itemDist = [itemLoc distanceFromLocation:current]; 

你还需要更新NSLog来期望一个double 对象,否则它会在运行时崩溃:

 NSLog(@"Distance: %f", itemDist); 

Swift 2.0有以下几种方式:

 let userLocation:CLLocation = CLLocation(latitude: 11.11, longitude: 22.22) let priceLocation:CLLocation = CLLocation(latitude: 33.33, longitude: 44.44) let meters:CLLocationDistance = userLocation.distanceFromLocation(priceLocation) 

下面的代码将在你的视图中工作

 //************************** GEtting USer's latitude/ Longitude ******************** self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; [self.locationManager startUpdatingLocation]; //************************** GEtting USer's latitude/ Longitude ******************** 

编译标记 – CLLocationManagerDelegate

 - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"didFailWithError: %@", error); UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [errorAlert show]; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"didUpdateToLocation: %@", newLocation); CLLocation *currentLocation = newLocation; if (currentLocation != nil) { CLLocation *locA = [[CLLocation alloc] initWithLatitude:currentLocation.coordinate.longitude longitude:currentLocation.coordinate.latitude]; CLLocation *locB = [[CLLocation alloc] initWithLatitude:11.111111111111111 longitude:11.1111111111111111]; User_Vanue_Distance = [locA distanceFromLocation:locB]; NSLog(@"THIS IS THE DISTANCE BT TWO POINTS +++++++++++++++++++++%f",User_Vanue_Distance); } } 

你需要把下面的键放在info.plist中,在ios 8中要访问的位置,我们需要把下面的键放在info.plist中1. NSLocationWhenInUseUsageDescription : Location required 2. NSLocationAlwaysUsageDescription Needed : Location Needed

这将为你工作,但你需要添加的一件事是添加一个检查ios8。 否则这将在ios 7上崩溃

 // Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7. if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) { [self.locationManager requestWhenInUseAuthorization]; } 

希望这会为你工作。 🙂

从两个不同的纬度和经度中找出KM的距离

 let userLocation:CLLocation = CLLocation(latitude: 23.0320, longitude: 72.5250) let priceLocation:CLLocation = CLLocation(latitude: 23.0283, longitude: 72.5067) let distance = String(format: "%.2f km", userLocation.distanceFromLocation(priceLocation)/1000) print("Distance is KM is:: \(distance)")