下面是城市列表,我将用户的当前位置与城市进行比较,并使用(min)函数

let closestCity = min(theDistanceInMetersFromBusselton,theDistanceInMetersFromBunbury,theDistanceInMetersFromJoondalup,theDistanceInMetersFromArmadale)

为了返回最近的城市,尽管现在我想返回第二个和第三个最近的城市。
我还没能让这个工作,尽管我在想:
citiesArray - closestCity = SecondClosestCitiesArray

然后做一个secondClosestCity = min(SecondClosestCitiesArray)得到第二个最近的城市。
然后重复这个找到第三个最接近的?
有什么想法吗?
extension HomePage {
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let db = Firestore.firestore()

        guard let uid = Auth.auth().currentUser?.uid else { return }

        let location = locations[0]
        let actualLatitude = String(location.coordinate.latitude)
        let actualLongitude = String(location.coordinate.longitude)

        guard let doubleActualLatitude = Double(actualLatitude) else { return }

        guard let doubleActualLongitude = Double(actualLongitude) else { return }

        ///users current location
        let usersCurrentLocation = CLLocation(latitude: doubleActualLatitude, longitude: doubleActualLongitude)

        //////////////list of locations ////////////////

        //////////ARMADALE/////////////////////

        let ArmadaleLatitude = Double(-32.1530)
        let ArmadaleLongitude = Double(116.0150)

        let ArmadaleCoordinates = CLLocation(latitude:ArmadaleLatitude, longitude: ArmadaleLongitude)

        let theDistanceInMetersFromArmadale = usersCurrentLocation.distance(from: ArmadaleCoordinates)

        ////////////////Bunbury///////////////////
        let BunburyLatitude = Double(-33.3256)
        let BunburyLongitude = Double(115.6396)

        let BunburyCoordinates = CLLocation(latitude:BunburyLatitude, longitude: BunburyLongitude)

        let theDistanceInMetersFromBunbury = usersCurrentLocation.distance(from: BunburyCoordinates)

        /////////////////////////////////////////////

        ////////Busselton//////////////////
        let busseltonLatitude = Double(-33.6555)
        let busseltonLongitude = Double(115.3500)

        let busseltonCoordinates = CLLocation(latitude:busseltonLatitude, longitude: busseltonLongitude)

        let theDistanceInMetersFromBusselton = usersCurrentLocation.distance(from: busseltonCoordinates)
        /////////////////////////////////

        /////////Joondalup////////////////////
        let JoondalupLatitude = Double(-32.5361)
        let JoondalupLongitude = Double(115.7424)

        let JoondalupCoordinates = CLLocation(latitude:JoondalupLatitude, longitude: JoondalupLongitude)

        let theDistanceInMetersFromJoondalup = usersCurrentLocation.distance(from: JoondalupCoordinates)

        //////////////////////////////////////

        /////return the the closest city
        let closestCity = min(theDistanceInMetersFromBusselton,theDistanceInMetersFromBunbury,theDistanceInMetersFromJoondalup,theDistanceInMetersFromArmadale)

        func findClosestCity(){
            //////////Armadale////////////////////////
            if closestCity == theDistanceInMetersFromArmadale{
                db.collection("Users").document(uid).setData(["Location": "Armadale" ], options: SetOptions.merge())

            /////////Bunbury////////////
            }else if closestCity == theDistanceInMetersFromBunbury{
                let Bunbury = "Bunbury"

                db.collection("Users").document(uid).setData(["Location": Bunbury ], options: SetOptions.merge())

            ///////////// Busselton//////////////
            }else if closestCity == theDistanceInMetersFromBusselton{
                let Busselton = "Busselton"

                db.collection("Users").document(uid).setData(["Location": Busselton ], options: SetOptions.merge())

            /////////////Joondalup//////////////////
            }else if closestCity == theDistanceInMetersFromJoondalup{
                db.collection("Users").document(uid).setData(["Location": "Joondalup" ], options: SetOptions.merge())
            }
        }
    }
}

最佳答案

let cityLocations = [
    "Armadale": CLLocation(latitude: -32.1530, longitude: 116.0150),
    "Bunbury": CLLocation(latitude: -33.3256, longitude: 115.6396),
    "Busselton": CLLocation(latitude: -33.6555, longitude: 115.3500),
    "Joondalup": CLLocation(latitude: -32.5361, longitude: 115.7424)
]

func distanceFromCity(_ city: String, location: CLLocation) -> Double? {
    return cityLocations[city].flatMap { location.distance(from: $0) }
}

func citiesClosestToLocation(_ location: CLLocation, n: Int) -> [String] {
    let cities = cityLocations.sorted {
        location.distance(from: $0.value) < location.distance(from: $1.value)
    }
    return cities.dropLast(cities.count - n).map({ $0.key })
}

let testLocation = cityLocations["Armadale"]!

print(citiesClosestToLocation(testLocation, n: 3)) // ["Armadale", "Joondalup", "Bunbury"]

关于swift - 快速找到数组中的第二和第三低数字-,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48836561/

10-12 05:42