【问题标题】:Calculate Distance between Two Sets of Coordinates (NSStrings)计算两组坐标之间的距离(NSStrings)
【发布时间】:2012-12-16 02:12:40
【问题描述】:

我有两组地理坐标,我正在尝试计算它们之间的距离。我已经做了一些挖掘,但不知道该怎么做。我正在尝试获取用户(userLatitude/userLongitude)和地点(placeLatitude/placeLongitude)之间的距离(以英里为单位)。我的坐标存储为 NSStrings,如下所示。有谁知道如何做到这一点?谢谢你们!

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

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

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

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

【问题讨论】:

    标签: objective-c ios xcode nsstring cllocationmanager


    【解决方案1】:

    这可能会对您有所帮助。尝试从你拥有的 lat long 创建 CLLocation

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

    【讨论】:

      【解决方案2】:

      从下面你可以得到距离。

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

      【讨论】: