简体   繁体   中英

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"];

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

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

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM