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);
The error you're getting is actually:
What it's saying is you're initializing
itemDist
(which you've declared as aCLLocationDistance *
) to something that is returning aCLLocationDistance
(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 aCLLocationDistance
, just declare it as aCLLocationDistance
(no asterisk):You'll also need to update the
NSLog
to expect a double instead of an object otherwise it will crash at run-time:Is swift 2.0 is in the following way:
To find distance in KM from two different Lat & Long
Below code will work for you in view load
pragma mark - CLLocationManagerDelegate
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
Hope this will work for you. :)