从用户位置查找最接近的经度和纬度 - iOS Swift [英] Find closest longitude and latitude in array from user location - iOS Swift
问题描述
在问题发布这里,用户问:
我有一个阵列充满了经度和纬度。我有两个双重变量与我的用户位置。我想测试我的用户位置与阵列之间的距离,以查看哪个位置最接近。我如何做到这一点?
这将得到2个位置之间的距离,但是拼命地理解我将如何对阵一系列位置进行测试。 b $ b
作为回应,他得到了以下代码:
NSArray * locations = //您的CLLocation对象数组
CLLocation * currentLocation = //当前设备位置
CLLocation * closestLocation;
CLLocationDistance smallestDistance = DBL_MAX; //设置最大值
(CLLocation * location in location){
CLLocationDistance distance = [currentLocation distanceFromLocation:location];
if(distance< smallestDistance){
smallestDistance = distance;
closestLocation = location;
}
}
NSLog(@smallestDistance =%f,smallestDistance);
在我正在处理的应用程序中,我有完全相同的问题,我认为这块代码可以完美工作。但是,我正在使用Swift,并且此代码位于Objective-C中。
我唯一的问题是:它应该在Swift中看起来如何?
感谢您的帮助。我对这一切都很陌生,在Swift中看到这段代码可能是一个大问题。
var closestLocation:CLLocation?
var smallestDistance:CLLocationDistance?
位置位置{
let distance = currentLocation.distanceFromLocation(位置)
如果smallestDistance == nil ||距离< smallestDistance {
closestLocation = location
smallestDistance = distance
}
}
print(smallestDistance = \(smallestDistance))
或作为函数:
func locationInLocations(位置:[CLLocation],closestToLocation位置:CLLocation) - > CLLocation? {
如果locations.count == 0 {
返回nil
}
var closestLocation:CLLocation?
var smallestDistance:CLLocationDistance?
位置位置{
let distance = location.distanceFromLocation(位置)
如果smallestDistance == nil ||距离< smallestDistance {
closestLocation = location
smallestDistance = distance
}
}
print(closestLocation:\(closestLocation),distance:\(最小距离))
返回最近位置
}
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.
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.
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
}
这篇关于从用户位置查找最接近的经度和纬度 - iOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!