【问题标题】:How to find out distance between coordinates?如何找出坐标之间的距离?
【发布时间】:2016-01-23 02:35:42
【问题描述】:

我想让它显示两个 CLLocation 坐标之间的距离。没有复杂的数学公式有没有办法做到这一点?如果没有公式,你会怎么做?

【问题讨论】:

  • 提问前请阅读文档
  • @Cosyn 好的,我下次会。对此感到抱歉。

标签: ios swift cllocation cllocationdistance


【解决方案1】:
import CoreLocation

//My location
let myLocation = CLLocation(latitude: 31.5101892, longitude: 74.3440842)

//My Next Destination
let myNextDestination = CLLocation(latitude: 33.7181584, longitude: 73.071358)

//Finding my distance to my next destination (in km)
let distance = myLocation.distance(from: myNextDestination) / 1000

【讨论】:

    【解决方案2】:

    你也可以像安卓开发者一样使用HaversineDistance algorithm,当你在安卓中有其他类似的应用时这很有帮助,否则上面的答案对你来说是正确的。

    import UIKit
    
    func haversineDinstance(la1: Double, lo1: Double, la2: Double, lo2: Double, radius: Double = 6367444.7) -> Double {
    
    let haversin = { (angle: Double) -> Double in
        return (1 - cos(angle))/2
    }
    
    let ahaversin = { (angle: Double) -> Double in
        return 2*asin(sqrt(angle))
    }
    
    // Converts from degrees to radians
    let dToR = { (angle: Double) -> Double in
        return (angle / 360) * 2 * .pi
    }
    
    let lat1 = dToR(la1)
    let lon1 = dToR(lo1)
    let lat2 = dToR(la2)
    let lon2 = dToR(lo2)
    
    return radius * ahaversin(haversin(lat2 - lat1) + cos(lat1) * cos(lat2) * haversin(lon2 - lon1))
    }
    
    let amsterdam = (52.3702, 4.8952)
    let newYork = (40.7128, -74.0059)
    
    // Google says it's 5857 km so our result is only off by 2km which could be due to all kinds of things, not sure how google calculates the distance or which latitude and longitude google uses to calculate the distance.
    haversineDinstance(la1: amsterdam.0, lo1: amsterdam.1, la2: newYork.0, lo2: newYork.1)
    

    我从参考链接中选择了上面编写的代码 https://github.com/raywenderlich/swift-algorithm-club/blob/master/HaversineDistance/HaversineDistance.playground/Contents.swift

    【讨论】:

      【解决方案3】:
      import UIKit
      import CoreLocation
      
      class ViewController: UIViewController {
      
          override func viewDidLoad() {
              super.viewDidLoad()
              var currentLocation = CLLocation(latitude: 23.1929, longitude: 72.6156)
              var DestinationLocation = CLLocation(latitude: 23.0504, longitude: 72.4991)
              var distance = currentLocation.distance(from: DestinationLocation) / 1000
              print(String(format: "The distance to my buddy is %.01fkm", distance))
          }
      }
      

      【讨论】:

      • 虽然这段代码可能会回答这个问题,但最好在不介绍其他代码的情况下解释它是如何解决问题的,以及为什么要使用它。从长远来看,纯代码的答案没有用处。
      【解决方案4】:

      斯威夫特 4.1

      import CoreLocation
      
      //My location
      let myLocation = CLLocation(latitude: 59.244696, longitude: 17.813868)
      
      //My buddy's location
      let myBuddysLocation = CLLocation(latitude: 59.326354, longitude: 18.072310)
      
      //Measuring my distance to my buddy's (in km)
      let distance = myLocation.distance(from: myBuddysLocation) / 1000
      
      //Display the result in km
      print(String(format: "The distance to my buddy is %.01fkm", distance))
      

      【讨论】:

        【解决方案5】:

        斯威夫特 5.

        func calculateDistance(mobileLocationX:Double,mobileLocationY:Double,DestinationX:Double,DestinationY:Double) -> Double {
        
                let coordinate₀ = CLLocation(latitude: mobileLocationX, longitude: mobileLocationY)
                let coordinate₁ = CLLocation(latitude: DestinationX, longitude:  DestinationY)
        
                let distanceInMeters = coordinate₀.distance(from: coordinate₁)
        
                return distanceInMeters
            }
        

        用于

        let distance = calculateDistance("add parameters")
        

        【讨论】:

          【解决方案6】:

          对于 Swift 4

             let locationOne = CLLocation(latitude: lat, longitude: long)
             let locationTwo = CLLocation(latitude: lat,longitude: long)
          
            let distance = locationOne.distance(from: locationTwo) * 0.000621371
          
            distanceLabel.text = "\(Int(round(distance))) mi"
          

          【讨论】:

            【解决方案7】:

            CLLocation 有一个 distanceFromLocation 方法,所以给定两个 CLLocation:

            CLLocationDistance distanceInMeters = [location1 distanceFromLocation:location2];
            

            或在 Swift 4 中:

            //: Playground - noun: a place where people can play
            
            import CoreLocation
            
            
            let coordinate₀ = CLLocation(latitude: 5.0, longitude: 5.0)
            let coordinate₁ = CLLocation(latitude: 5.0, longitude: 3.0)
            
            let distanceInMeters = coordinate₀.distance(from: coordinate₁) // result is in meters
            

            你到达这里距离 所以1英里 = 1609米

            if(distanceInMeters <= 1609)
             {
             // under 1 mile
             }
             else
            {
             // out of 1 mile
             }
            

            【讨论】:

            • 如何将其转换为里程?
            • 根据谷歌,一米有 0.000621371 英里。所以,我建议将它乘以 0.000621371。
            • 这不是真正的距离“By road”根据苹果文档 - “这种方法通过追踪它们之间的一条遵循地球曲率的线来测量两个位置之间的距离。由此产生的弧是一条平滑的曲线,没有考虑两个位置之间的特定高度变化。” developer.apple.com/documentation/corelocation/cllocation/… .. 因此,如果您正在寻找行驶距离,那么到目前为止找到正确距离的可靠方法是 Google Maps Distance Matrix API 或 MKRoute。
            • @GlennHowes 是的,有可能是 OP 试图找到两点之间的驾驶(或其他运输方式)距离,在这种情况下,只是由distanceFromLocation 得出的地理空间距离将是完全不正确的。对于行驶距离,正确的实现是使用MKRoute,它将为您提供以米为单位的“路线距离”distance 以及其他信息,例如预期的旅行时间、运输类型等 - developer.apple.com/documentation/mapkit/mkroute
            • 正如@vijay 所报告的(在上面的 cmets 中),这就是他的距离与谷歌地图相比不正确的原因。谷歌地图和MKRoute会考虑路线信息来计算实际运输距离,而简单的distanceFromLocation不会,所以我建议你更新你的答案以澄清这一点,以避免混淆通过谷歌到达此页面的人。跨度>
            【解决方案8】:
            func calculateDistanceInMiles(){
            
                let coordinate₀ = CLLocation(latitude:34.54545, longitude:56.64646)
                let coordinate₁ = CLLocation(latitude: 28.4646, longitude:76.65464)
                let distanceInMeters = coordinate₀.distance(from: coordinate₁)
                if(distanceInMeters <= 1609)
                {
                    let s =   String(format: "%.2f", distanceInMeters)
                    self.fantasyDistanceLabel.text = s + " Miles"
                }
                else
                {
                    let s =   String(format: "%.2f", distanceInMeters)
                    self.fantasyDistanceLabel.text = s + " Miles"
            
                }
            }
            

            【讨论】:

            • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值。
            【解决方案9】:

            对于objective-c

            您可以使用distanceFromLocation 来查找两个坐标之间的距离。

            代码片段:

            CLLocation *loc1 = [[CLLocation alloc] initWithLatitude:lat1 longitude:lng1];
            
            CLLocation *loc2 = [[CLLocation alloc] initWithLatitude:lat2 longitude:lng2];
            
            CLLocationDistance distance = [loc1 distanceFromLocation:loc2];
            

            您的输出将以米为单位。

            【讨论】:

            • 这里的问题以“swift”为标题。所以也许你需要更新你的答案,或者添加“For objective-c :”@Vijay
            【解决方案10】:

            试试这个:

            distanceInMeters = fromLocation.distanceFromLocation(toLocation)
            distanceInMiles = distanceInMeters/1609.344
            

            来自Apple Documentation

            返回值:两个位置之间的距离(以米为单位)。

            【讨论】:

            • 有没有办法把它变成英里?
            • 同样来自苹果文档 - “此方法通过跟踪它们之间的一条遵循地球曲率的线来测量两个位置之间的距离。生成的弧线是一条平滑曲线,没有考虑到两个位置之间的特定高度变化。"developer.apple.com/documentation/corelocation/cllocation/…
            • 很好,您使用了正确的因子1609.344en.wikipedia.org/wiki/Mile
            • 对于 iOS 10+,通过Measurement 类执行到英里的转换会很有用:let miles = Measurement(value: meters, unit: UnitLength.meters).converted(to: UnitLength.miles).value
            猜你喜欢
            • 2020-08-06
            • 1970-01-01
            • 2021-10-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-02-25
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多