E-Riddie
E-Riddie

Reputation: 14780

Distance between 2 points with CLLocationCoordinate2D

Details

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 CLLocation, and the other one is a pin on the map which is of type CLLocationCoordinate2D.

Issue

I call distanceFromLocation to get my current location in CLLocation but it says bad receiver type from CLLocationCoordinate2D.

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

Question

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?

Upvotes: 4

Views: 12309

Answers (4)

Dave Wood
Dave Wood

Reputation: 13333

Based on @manujmv's answer above, here's a Swift extension (returns metres):

import Foundation
import CoreLocation

extension CLLocationCoordinate2D {
    func distanceTo(coordinate: CLLocationCoordinate2D) -> CLLocationDistance {
        let thisLocation = CLLocation(latitude: self.latitude, longitude: self.longitude)
        let otherLocation = CLLocation(latitude: coordinate.latitude, longitude: coordinate.longitude)

        return thisLocation.distanceFromLocation(otherLocation)
    }
}

Upvotes: 1

iOS
iOS

Reputation: 3616

Try to convert CLLocationCoordinate2D to CLLocation,

Create a CLLocation object using,

-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude

Upvotes: 3

nerowolfe
nerowolfe

Reputation: 4817

For example:

   +(double)distanceFromPoint:(double)lat lng:(double)lng
   {

      CLLocation *placeLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
      CLLocationCoordinate2D usrLocation = locationManager.location;
      CLLocation * userLocation = [[CLLocation alloc] initWithLatitude:usrLocation.latitude longitude:usrLocation.longitude];

      double meters = [userLocation distanceFromLocation:placeLocation];

      return meters;
   }

Upvotes: 6

manujmv
manujmv

Reputation: 6445

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;

    //NSLog(@"%f",dist);
    NSString *distance = [NSString stringWithFormat:@"%f",dist];

    return [distance floatValue];

}

Upvotes: 10

Related Questions