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

5

我正在使用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个回答

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)

5
你实际上得到的错误是:
初始化 'CLLocationDistance *'(又名 'double *')时,使用了不兼容类型的表达式 'CLLocationDistance'(又名 'double')
它的意思是你正在用返回CLLocationDistance的东西(注意没有星号)来初始化itemDist(你已经声明为CLLocationDistance *的变量)。 CLLocationDistance不是一个对象。它只是一个原始类型(具体来说是double,请参见Core Location Data Types Reference)。
所以,不要将itemDist声明为指向CLLocationDistance的指针,而是将其声明为CLLocationDistance(没有星号)即可。
CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];

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

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

0

以下代码将在视图加载时为您工作

//************************** 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 : 需要位置 2. NSLocationAlwaysUsageDescription Needed : 需要位置

这将对您有用,但您需要添加一个检查iOS 8的条件。否则,在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];
    }

希望这对你有用。 :)

0

计算两个不同的纬度和经度之间的距离(单位:公里)

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接