简体   繁体   English

从用户位置查找数组中最接近的经度和纬度-iOS Swift

[英]Find closest longitude and latitude in array from user location - iOS Swift

In the question posted here , the user asked: 此处发布的问题中,用户询问:

I have an array full of longitudes and latitudes. 我有一个充满经度和纬度的数组。 I have two double variables with my users location. 我的用户位置有两个双精度变量。 I'd like to test the distance between my user's locations against my array to see which location is the closest. 我想根据数组测试用户位置之间的距离,以查看哪个位置最近。 How do I do this? 我该怎么做呢?

This will get the distance between 2 location but stuggeling to understand how I'd test it against an array of locations. 这将获得2个位置之间的距离,但努力了解如何针对一系列位置对其进行测试。

In response, he got the following code: 作为响应,他得到了以下代码:

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

I have the exact same problem in an application I'm working on, and I think this piece of code could work perfectly. 在我正在处理的应用程序中,我遇到了完全相同的问题,我认为这段代码可以完美地工作。 However, I'm using Swift, and this code is in Objective-C. 但是,我正在使用Swift,并且此代码在Objective-C中。

My only question is: how should it look in Swift? 我唯一的问题是:在Swift中应该如何显示?

Thanks for any help. 谢谢你的帮助。 I'm new to all of this, and seeing this piece of code in Swift could be a big leg up. 我对所有这些都是新手,看到Swift中的这段代码可能会大有帮助。

For Swift 3 I have created this little piece of "functional" code: 对于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) })
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)")

or as a function: 或作为功能:

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
}
    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)")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从用户位置查找数组中最接近的经度和纬度 - Find closest longitude and latitude in array from user location 在距用户位置的数组中查找最接近的经度和纬度,并对与一个到另一个纬度的最短距离相对应的数组进行排序 - Find closest longitude and latitude in array from user location and Sorting array corresponding to shortest distance from one to another lat long Swift 4 CLLocation用户位置经度和纬度 - Swift 4 CLLocation User Location Longitude & Latitude 从 iOS 中的纬度和经度获取位置名称 - Get location name from Latitude & Longitude in iOS 检索用户的当前位置/检索经度和纬度watchkit / ios swift的值 - Retrieving the current location of the user/ retrieving the values of longitude and latitude watchkit/ios swift Swift:获取用户的位置坐标作为纬度和经度? - Swift: Get user's location coordinates as latitude and longitude? 如何在iOS中获取用户位置的经度和纬度 - How to get latitude and longitude of a user's location in iOS 使用Swift查找纬度和经度 - Find Latitude and Longitude using Swift 使用纬度和经度数组中的Mapbox绘制折线-Swift 3 iOS - Draw polyline using Mapbox from Latitude and longitude Arrays - Swift 3 iOS 在Swift中从经纬度查找城市名称和国家/地区 - Find city name and country from latitude and longitude in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM