【问题标题】:distanceFromLocation - Calculate distance between two pointsdistanceFromLocation - 计算两点之间的距离
【发布时间】:2011-04-23 18:47:57
【问题描述】:

只是一个关于核心位置的快速问题,我正在尝试计算两点之间的距离,代码如下:

    -(void)locationChange:(CLLocation *)newLocation:(CLLocation *)oldLocation
    {   

    // Configure the new event with information from the location.
        CLLocationCoordinate2D newCoordinate = [newLocation coordinate];
        CLLocationCoordinate2D oldCoordinate = [oldLocation coordinate];

        CLLocationDistance kilometers = [newCoordinate distanceFromLocation:oldCoordinate] / 1000; // Error ocurring here.
        CLLocationDistance meters = [newCoordinate distanceFromLocation:oldCoordinate]; // Error ocurring here.
}

我在最后两行收到以下错误:

错误:无法转换为指针类型

我一直在谷歌上搜索,但找不到任何东西。

【问题讨论】:

    标签: ios objective-c core-location


    【解决方案1】:

    试试这个:

    CLLocationDistance meters = [newLocation distanceFromLocation:oldLocation];
    

    您尝试使用的方法是 CLLocation 对象上的方法 :)

    【讨论】:

    • 并且记住“这种方法通过追踪它们之间的一条遵循地球曲率的线来测量两个位置之间的距离”苹果文档。所以这不会给出通过道路的距离。
    • 如何通过道路找到距离? @deanWombourne
    • @AnkitSrivastava 我怎样才能找到公路距离
    • 最简单的方法是询问有关堆栈溢出的问题;) Google 有一个您可以使用的 API (developers.google.com/maps/documentation/directions),Apple 也有 (developer.apple.com/library/ios/documentation/UserExperience/…) 我猜是因为您是在 iOS 问题上提出这个问题,您可能会想要 Apple 的 :)
    • 好答案,但在 iOS9 中,此代码功能比 iOS8 需要更多时间。你知道如何解决吗?
    【解决方案2】:
    #import <CoreLocation/CoreLocation.h>
    
    CLLocation *locA = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];
    
    CLLocation *locB = [[CLLocation alloc] initWithLatitude:"Value" longitude:"Value"];
    
    CLLocationDistance distance = [locA distanceFromLocation:locB];
    NSLog(@"distance:-%f",distance);//distance in Miter
    

    【讨论】:

    • 这个答案需要解释
    • 嗨 Burhanuddin Rashid 这不是 Android 开发代码。这是“计算两点之间的距离”的ios开发代码
    • 它的 IOS、Android 或任何其他技术领域都没有关系。 Think is answer应该对代码有一点解释......只是不要复制粘贴代码..请有礼貌的一些解释
    • 嗨 Burhanuddin Rashid 这个答案不是复制和过去的。获取距离是苹果的默认功能。我的回答很少,但它的工作完美。您输入 locA 输入 Lat,long locB 输入 Lat,long 以计算 LocA 到 LocB 的距离。更多信息link谢谢..
    【解决方案3】:

    距离是计算 2 个 CLLocations 之间的距离,而不是坐标之间的距离。

    您需要使用这些坐标获取相应坐标的 CLLocations,使用以下代码行

    CLLocation *newLocation = [[CLLocation alloc] initWithCoordinate: newCoordinate altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:nil];
    

    对于另一个坐标也是如此,然后您可以使用以下代码行计算这两个位置之间的距离

    CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;
    

    希望这会对你有所帮助。

    更新: Swift 3.0

    let distanceKiloMeters = (newLocation.distance(from: oldLocation))/1000
    

    【讨论】:

      【解决方案4】:

      在 Swift 中

      让我们创建一个计算两个位置之间距离的方法函数:

       func distanceBetweenTwoLocations(source:CLLocation,destination:CLLocation) -> Double{
      
              var distanceMeters = source.distanceFromLocation(destination)
              var distanceKM = distanceMeters / 1000
              let roundedTwoDigit = distanceKM.roundedTwoDigit
              return roundedTwoDigit
      
          }
      

      如果你只想要两个数字:

      extension Double{
      
          var roundedTwoDigit:Double{
      
              return Double(round(100*self)/100)
      
              }
          }
      

      【讨论】:

      • 好答案。只是一个小问题。您应该将 var 替换为 let iniside distanceBetweenTwoLocations func ;-)
      【解决方案5】:

      如果您打算使用 2 个 CLLocationCoordinate2D 值,那么您可以使用它。

      这是 Xcode 7.1 上的 Swift 2.1

      import CoreLocation
      
      extension CLLocationCoordinate2D {
      
          func distanceInMetersFrom(otherCoord : CLLocationCoordinate2D) -> CLLocationDistance {
              let firstLoc = CLLocation(latitude: self.latitude, longitude: self.longitude)
              let secondLoc = CLLocation(latitude: otherCoord.latitude, longitude: otherCoord.longitude)
              return firstLoc.distanceFromLocation(secondLoc)
          }
      
      }
      

      【讨论】:

        【解决方案6】:

        取自优秀的库CoreLocation utitlities

        - (CLLocationDistance) distanceFromCoordinate:(CLLocationCoordinate2D) fromCoord;
        {
            double earthRadius = 6371.01; // Earth's radius in Kilometers
        
            // Get the difference between our two points then convert the difference into radians
            double nDLat = (fromCoord.latitude - self.coordinate.latitude) * kDegreesToRadians;  
            double nDLon = (fromCoord.longitude - self.coordinate.longitude) * kDegreesToRadians; 
        
            double fromLat =  self.coordinate.latitude * kDegreesToRadians;
            double toLat =  fromCoord.latitude * kDegreesToRadians;
        
            double nA = pow ( sin(nDLat/2), 2 ) + cos(fromLat) * cos(toLat) * pow ( sin(nDLon/2), 2 );
        
            double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
            double nD = earthRadius * nC;
        
            return nD * 1000; // Return our calculated distance in meters
        }
        

        【讨论】:

        【解决方案7】:

        这里的问题是你正在调用一个对象method

        - (CLLocationDistance)distanceFromLocation:(const CLLocation *)location;
        

        属于 CLLocation 类。

        CLLocationCoordinate2D 实际上是一个结构,由两个双精度组成:

        typedef struct
        {
            CLLocationDegrees latitude;
            CLLocationDegrees longitude;
        } CLLocationCoordinate2D;
        

        这样做的正确方法是获取一个CLLocation 对象并在其上调用distanceFromLocation。像这样:

        CLLocation* newLocation;
        CLLocation* oldLocation;
        CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
        

        当然,您首先需要初始化这两个值(例如,来自CLLocationManager)。

        【讨论】:

          猜你喜欢
          • 2010-10-30
          • 2014-11-14
          • 1970-01-01
          • 2017-08-27
          • 2015-08-16
          • 1970-01-01
          相关资源
          最近更新 更多