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

时间:2014-04-01 19:23:13

标签: ios cocoa-touch ios7 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);

4 个答案:

答案 0 :(得分:5)

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)

答案 1 :(得分:4)

您获得的错误实际上是:

  

使用不兼容类型'CLLocationDistance'(又名'double')的表达式初始化'CLLocationDistance *'(又名'double *')

它的含义是你正在将itemDist(你已宣布为CLLocationDistance *)初始化为返回CLLocationDistance的东西(注意没有星号)。

CLLocationDistance不是对象 它只是一种原始类型(具体为double - 请参阅Core Location Data Types Reference)。


因此,不要将itemDist声明为指针CLLocationDistance,而是将其声明为CLLocationDistance(无星号):

CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];

您还需要更新NSLog以期望 double 而不是对象,否则它将在运行时崩溃:

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

答案 2 :(得分:0)

以下代码适合您 在视图加载

//************************** GEtting USer's latitude/ Longitude ********************




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

pragma mark - 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];
    }

希望这对你有用。 :)

答案 3 :(得分:0)

从两个不同的Lat&长

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)")