【问题标题】:Find closest longitude and latitude in array from user location - iOS Swift从用户位置查找数组中最近的经度和纬度 - iOS Swift
【发布时间】:2018-04-23 17:22:02
【问题描述】:

here发布的问题中,用户问:

我有一个充满经度和纬度的数组。我的用户位置有两个双重变量。我想测试我的用户位置与我的阵列之间的距离,以查看哪个位置最近。我该怎么做?

这将获得 2 个位置之间的距离,但很难理解我将如何针对一组位置进行测试。

作为回应,他得到了以下代码:

NSArray *locations = //your array of CLLocation objects
CLLocation *currentLocation = //current device Location

CLLocation *closestLocation;
CLLocationDistance smallestDistance = DBL_MAX; // set the max value

for (CLLocation *location in locations) {
    CLLocationDistance distance = [currentLocation  distanceFromLocation:location];

    if (distance < smallestDistance) {
        smallestDistance = distance;
        closestLocation = location;
    }
}
NSLog(@"smallestDistance = %f", smallestDistance);

我正在处理的应用程序中遇到完全相同的问题,我认为这段代码可以完美运行。但是,我使用的是 Swift,并且这段代码是在 Objective-C 中的。

我唯一的问题是:它在 Swift 中应该是什么样子?

感谢您的帮助。我对这一切都很陌生,在 Swift 中看到这段代码可能会大有帮助。

【问题讨论】:

    标签: ios swift google-maps cllocationmanager cllocationdistance


    【解决方案1】:

    对于 Swift 3,我创建了这段“函数式”代码:

    let coord1 = CLLocation(latitude: 52.12345, longitude: 13.54321)
    let coord2 = CLLocation(latitude: 52.45678, longitude: 13.98765)
    let coord3 = CLLocation(latitude: 53.45678, longitude: 13.54455)
    
    let coordinates = [coord1, coord2, coord3]
    
    let userLocation = CLLocation(latitude: 52.23678, longitude: 13.55555)
    
    let closest = coordinates.min(by: 
    { $0.distance(from: userLocation) < $1.distance(from: userLocation) })
    

    【讨论】:

      【解决方案2】:
      var closestLocation: CLLocation?
      var smallestDistance: CLLocationDistance?
      
      for location in locations {
        let distance = currentLocation.distanceFromLocation(location)
        if smallestDistance == nil || distance < smallestDistance {
          closestLocation = location
          smallestDistance = distance
        }
      }
      
      print("smallestDistance = \(smallestDistance)")
      

      或作为一个函数:

      func locationInLocations(locations: [CLLocation], closestToLocation location: CLLocation) -> CLLocation? {
        if locations.count == 0 {
          return nil
        }
      
        var closestLocation: CLLocation?
        var smallestDistance: CLLocationDistance?
      
        for location in locations {
          let distance = location.distanceFromLocation(location)
          if smallestDistance == nil || distance < smallestDistance {
            closestLocation = location
            smallestDistance = distance
          }
        }
      
        print("closestLocation: \(closestLocation), distance: \(smallestDistance)")
        return closestLocation
      }
      

      【讨论】:

      • 现在我可以用它与Objective-C进行比较。它的帮助不止一种。谢谢!
      【解决方案3】:
          func closestLoc(userLocation:CLLocation){
          var distances = [CLLocationDistance]()
          for location in locations{
              let coord = CLLocation(latitude: location.latitude!, longitude: location.longitude!)
              distances.append(coord.distance(from: userLocation))
              print("distance = \(coord.distance(from: userLocation))")
          }
      
          let closest = distances.min()//shortest distance
          let position = distances.index(of: closest!)//index of shortest distance
          print("closest = \(closest!), index = \(position)")
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-14
        • 1970-01-01
        • 2016-10-16
        • 2019-01-20
        相关资源
        最近更新 更多