循环通过坐标,找到最近的商店Swift 3点

时间:2017-05-23 17:46:57

标签: ios swift swift3 google-play

想法:

App让司机可以看到距离客户最近的商店/餐厅。

我有什么:

  • 坐标保存为字符串

let clientLat = "24.449384" let clientLng = "56.343243"

  • 查找我所在地区所有商店的功能

我试图保存当地一家商店的所有坐标,我成功了:

  var coordinates: [CLLocationCoordinate2D] = [CLLocationCoordinate2D]()

 func performSearch() {
    coordinates.removeAll()

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "starbucks"
    request.region = mapView.region

    let search = MKLocalSearch(request: request)

    search.start(completionHandler: {(response, error) in

        if error != nil {
            print("Error occured in search: \(error!.localizedDescription)")
        } else if response!.mapItems.count == 0 {
            print("No matches found")
        } else {
            print("Matches found")

            for item in response!.mapItems {

                self.coordinates.append(item.placemark.coordinate)


               // need to sort coordinates

                // need to find the closest


                let annotation = MKPointAnnotation()
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                self.mapView.addAnnotation(annotation)
            }
        }
    })

}

我需要什么:

我希望循环坐标并找到距离纬线和长线最近的商店(公里),然后在其上放一个别针。

更新

  func performSearch() {
    coordinates.removeAll()

    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = "starbucks"
    request.region = mapView.region

    let search = MKLocalSearch(request: request)

    search.start(completionHandler: {(response, error) in

        if error != nil {
            print("Error occured in search: \(error!.localizedDescription)")
        } else if response!.mapItems.count == 0 {
            print("No matches found")
        } else {
            print("Matches found")

            for item in response!.mapItems {

                self.coordinates.append(item.placemark.coordinate)

                let pointToCompare = CLLocation(latitude: 24.741721, longitude: 46.891440)
              let  storedCorrdinates =  self.coordinates.map({CLLocation(latitude: $0.latitude, longitude: $0.longitude)}).sorted(by: {
                    $0.distance(from: pointToCompare) < $1.distance(from: pointToCompare)


                })
                self.coordinate = storedCorrdinates
            }

            let annotation = MKPointAnnotation()
            annotation.coordinate = self.coordinate[0].coordinate
            self.mapView.addAnnotation(annotation)
        }
    })

}  谢谢@brimstone

2 个答案:

答案 0 :(得分:2)

您可以通过将坐标转换为CLLocation类型然后使用distance(from:)方法来比较坐标之间的距离。例如,取坐标数组并将其映射到CLLocation,然后根据与要比较它们的点的距离对其进行排序。

let coordinates = [CLLocationCoordinate2D]()
let pointToCompare = CLLocation(latitude: <#yourLat#>, longitude: <#yourLong#>)

let sortedCoordinates = coordinates.map({CLLocation(latitude: $0.latitude, longitude: $0.longitude)}).sorted(by: {
    $0.distance(from: pointToCompare) < $1.distance(from: pointToCompare)
})

然后,要将注释的坐标设置为最近的坐标,只需下标sortedCoordinates数组。

annotation.coordinate = sortedCoordinates[0].coordinate

答案 1 :(得分:0)

我想分享我的解决方案:)

1)就我而言,我是从API上传数据的,因此我需要创建一个模型。

import MapKit

struct StoresMap: Codable {
    let id: Int?
    let title: String?
    let latitude: Double?
    let longitude: Double?
    let schedule: String?
    let phone: String?
    let ukmStoreId: Int?
    var distanceToUser: CLLocationDistance?
}

最后一个变量不是来自API,而是来自我自己,以定义每个商店的距离。

2)在ViewController中,我定义:

func fetchStoresList() {
    NetworkManager.downloadStoresListForMap(firstPartURL: backendURL) { (storesList) in
        self.shopList = storesList
        let initialLocation = self.locationManager.location!

        for i in 0..<self.shopList.count {
            self.shopList[i].distanceToUser = initialLocation.distance(from: CLLocation(latitude: self.shopList[i].latitude!, longitude: self.shopList[i].longitude!))
        }
        self.shopList.sort(by: { $0.distanceToUser! < $1.distanceToUser!})
        print("Closest shop - ", self.shopList[0])
    }
}

3)不要忘记在viewDidLoad()import MapView framework中调用该函数:)