从用户位置找到最近的经度和纬度——iOS Swift

时间:2022-11-15 20:19:38

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.

这将得到两个位置之间的距离,但要理解如何对一个位置数组进行测试。

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?

我唯一的问题是:它在斯威夫特应该是什么样子?

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.

感谢任何帮助。我对这一切都很陌生,看到斯威夫特的这段代码会让我大吃一惊。

3 个解决方案

#1


17  

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
}

#2


17  

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

#3


0  

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

#1


17  

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
}

#2


17  

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

#3


0  

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