【问题标题】:How to measure the distance in meters between two CLLocations?如何测量两个 CLLocation 之间的距离(以米为单位)?
【发布时间】:2012-05-13 00:08:45
【问题描述】:

如何获得两个CLLocations 之间的距离(以米为单位)? CLLocation 似乎没有提供任何方法来做到这一点。

【问题讨论】:

  • 你在开玩笑吗? CLLocation Reference Cmd-F,“米”:“以米为单位的高度” Cmd-G:“不确定性的半径,以米为单位”再次:“以米/秒为单位的瞬时速度”再次:“以米为单位的高度值。”再一次:“distanceFromLocation:返回从接收器位置到指定位置的距离(以米为单位)。”天啊!这在 Xcode 本身中更容易。

标签: objective-c cocoa-touch core-location cllocation


【解决方案1】:
CLLocationDistance distance = [aCLLocationA distanceFromLocation:aCLLocationB];
// distance is a double representing the distance in meters
【解决方案2】:
CLLocationDistance distance = [secondLocation distanceFromLocation:firstLocation];  //      distance is expressed in meters

CLLocationDistance kilometers = distance / 1000.0;
// or you can also use this..
CLLocationDistance meters = distance;

NSString *distanceString = [[NSString alloc] initWithFormat: @"%f", kilometers];

flot totaldistancecovered = [distanceString floatValue];

//Now,you can use this float value for addition...
// distanceMoved  should be float type variable which is declare in .h file...

 distanceMoved = distanceMoved + totaldistancecovered ;
 theLabel.text = [NSString stringWithFormat: @"%f meters", distanceMoved];

希望对你有所帮助...

【讨论】:

    【解决方案3】:
    CLLocation *loc1 = [[CLLocation alloc]  initWithLatitude:dblLatitude longitude:dblLongitude];   
    CLLocation *loc2 = [[CLLocation alloc]  initWithLatitude:dblCurrentLatitude longitude:dblCurrentLongitude];
    double dMeters = [loc1 distanceFromLocation:loc2];
    [loc1 release];
    [loc2 release];
    

    【讨论】:

      【解决方案4】:
      + (CLLocationDistance)distanceBetweenCoordinate:(CLLocationCoordinate2D)originCoordinate andCoordinate:(CLLocationCoordinate2D)destinationCoordinate {    
          CLLocation *originLocation = [[CLLocation alloc] initWithLatitude:originCoordinate.latitude longitude:originCoordinate.longitude];
          CLLocation *destinationLocation = [[CLLocation alloc] initWithLatitude:destinationCoordinate.latitude longitude:destinationCoordinate.longitude];
          CLLocationDistance distance = [originLocation distanceFromLocation:destinationLocation];
          [originLocation release];
          [destinationLocation release];
      
          return distance;
      }
      

      【讨论】:

        【解决方案5】:

        Swift 3.0

        let location1 = CLLocation(latitude: <CLLocationDegrees>, longitude: <CLLocationDegrees>)
        let location2 = CLLocation(latitude: <CLLocationDegrees>, longitude: <CLLocationDegrees>)
        
        let distance = location1.distance(from: location2)
        

        distance 以米为单位为Double

        https://developer.apple.com/reference/corelocation/cllocation/1423689-distance

        【讨论】:

          猜你喜欢
          • 2017-08-22
          • 1970-01-01
          • 1970-01-01
          • 2015-02-26
          • 2017-04-17
          • 2020-04-13
          • 2011-12-21
          • 2021-12-19
          • 2012-01-17
          相关资源
          最近更新 更多