EridB EridB - 1 year ago 131
iOS Question

Distance between 2 points with CLLocationCoordinate2D


I'm trying to measure the distance between two coordinates on a map (longitude, latitude). The first coordinate is my current location which is a
, and the other one is a pin on the map which is of type


I call
to get my current location in
but it says bad receiver type from

CLLocationCoordinate2D locationCoordinate;

// Set the latitude and longitude
locationCoordinate.latitude = [[item objectForKey:@"lat"] doubleValue];
locationCoordinate.longitude = [[item objectForKey:@"lng"] doubleValue];

CLLocationDistance dist = [locationCoordinate distanceFromLocation:locationManager.location.coordinate]; // bad receiver type from CLLocationCoordinate2D


I want to find the distance between these 2 coordinates in metric unit, and I know that types are mismatching, but how to convert them so I can find the distance between these 2 nodes?

Answer Source

Use the below method for finding distance between two locations

-(float)kilometersfromPlace:(CLLocationCoordinate2D)from andToPlace:(CLLocationCoordinate2D)to  {

    CLLocation *userloc = [[CLLocation alloc]initWithLatitude:from.latitude longitude:from.longitude];
    CLLocation *dest = [[CLLocation alloc]initWithLatitude:to.latitude longitude:to.longitude];

    CLLocationDistance dist = [userloc distanceFromLocation:dest]/1000;

    NSString *distance = [NSString stringWithFormat:@"%f",dist];

    return [distance floatValue];

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download