简体   繁体   English

计算两组坐标(NSStrings)之间的距离

[英]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). 我正在尝试获取用户(userLatitude / userLongitude)与地点(placeLatitude / placeLongitude)之间的距离(以英里为单位)。 My coordinates are stored as NSStrings as displayed below. 我的坐标存储为NSStrings,如下所示。 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 尝试从lat创建CLLocation

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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