Brandon
Brandon

Reputation: 2171

Calculate Distance between Two Sets of Coordinates (NSStrings)

I have two sets of geo-coordinates and I am trying to calculate the distance between them. I have done some digging around, but cannot figure out how to do it. I am trying to get the distance in miles between the user(userLatitude/userLongitude) and the place (placeLatitude/placeLongitude). My coordinates are stored as NSStrings as displayed below. Does anyone know how to do this? Thank you all!

NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLatitude];

NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate 
getUserLongitude];

NSString *placeLatitude = [element1 objectForKey:@"placeLatitude"];

NSString *placeLongitude = [element1 objectForKey:@"placeLongitude"];

Upvotes: 2

Views: 3536

Answers (2)

Augustin A
Augustin A

Reputation: 127

From the following you can get the distance.

CLLocation *locA = [[CLLocation alloc] initWithLatitude:currentLocation.latitude longitude:currentLocation.longitude];

CLLocation *locB = [[CLLocation alloc] initWithLatitude:[[element1 objectForKey:@"placeLatitude"] doubleValue]  longitude:[[element1 objectForKey:@"placeLongitude"] doubleValue]];

CLLocationDistance currentDistance = [locA distanceFromLocation:locB];

Upvotes: 0

DivineDesert
DivineDesert

Reputation: 6954

This might help you. Try to create CLLocation from lat long you have

CLLocation* Location1;
CLLocation* Location2;
CLLocationDistance distance = [Location1 distanceFromLocation:Location2];

Upvotes: 6

Related Questions