Question

I'm using CLLocationDistance to get the distance between two points, but I'm getting an error when passing my current location in it.

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);
Was it helpful?

Solution

The error you're getting is actually:

Initializing 'CLLocationDistance *' (aka 'double *') with an expression of incompatible type 'CLLocationDistance' (aka 'double')

What it's saying is you're initializing itemDist (which you've declared as a CLLocationDistance *) to something that is returning a CLLocationDistance (notice no asterisk).

CLLocationDistance is not an object.
It is just a primitive type (specifically double -- see the Core Location Data Types Reference).


So instead of declaring itemDist as a pointer to a CLLocationDistance, just declare it as a CLLocationDistance (no asterisk):

CLLocationDistance itemDist = [itemLoc distanceFromLocation:current];

You'll also need to update the NSLog to expect a double instead of an object otherwise it will crash at run-time:

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

OTHER TIPS

Is swift 2.0 is in the following way:

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)

Below code will work for you in view load

//************************** 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);  
    }
}

You need to put below keys in info.plist, in ios 8 to access location we need to put below keys in info.plist 1. NSLocationWhenInUseUsageDescription : Location required 2. NSLocationAlwaysUsageDescription Needed : Location Needed

This will work for you but one thing you need to add is add a check for ios8. otherwise this will crash on 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];
    }

Hope this will work for you. :)

To find distance in KM from two different Lat & Long

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)")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top