iOS反向地理编码(从其坐标获取位置的名称,而不仅仅是地址)

时间:2016-06-30 16:32:16

标签: ios mapkit geocoding

我正在为自己构建一个小应用程序 - 现在,它有一个名为performSearch的功能,可以在您所在附近的咖啡馆,健身房和餐馆的当地区域内进行搜索,然后在每个咖啡馆中放置针脚。位置。但是,我对如何获取注释以显示实际地图视图中显示的位置名称感到困惑。有人有经验吗?

基本上,我不想只显示地址,而是希望注释说“星巴克” 地址..."

示例代码:

使用任何给定的搜索字段进行搜索,并使用该搜索字段删除给定区域中所有位置的地图视图上的图钉。

var matchingItems: [MKMapItem] = [MKMapItem]()
@IBOutlet weak var map: MKMapView!

func performSearch(searchField: String) {

    matchingItems.removeAll()

    //search request
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchField
    request.region = self.map.region

    // process the request
    let search = MKLocalSearch(request: request)
    search.startWithCompletionHandler { response, error in
        guard let response = response else {
            print("There was an error searching for: \(request.naturalLanguageQuery) error: \(error)")
            return
        }

        for item in response.mapItems {
            // customize your annotations here, if you want
            var annotation = item.placemark
            self.reverseGeocoding(annotation.coordinate.latitude, longitude: allData.coordinate.longitude)

            self.map.addAnnotation(annotation)
            self.matchingItems.append(item)
        }

    }
}


func reverseGeocoding(latitude: CLLocationDegrees, longitude: CLLocationDegrees) {
    let location = CLLocation(latitude: latitude, longitude: longitude)
    CLGeocoder().reverseGeocodeLocation(location, completionHandler: {(placemarks, error) -> Void in
        if error != nil {
            print(error)
            return
        }
        else if placemarks?.count > 0 {
            let pm = placemarks![0]
            let address = ABCreateStringWithAddressDictionary(pm.addressDictionary!, false)
            print("\n\(address)")
            if pm.areasOfInterest?.count > 0 {
                let areaOfInterest = pm.areasOfInterest?[0]
                print(areaOfInterest!)
            } else {
                print("No area of interest found.")
            }
        }
    })
}

1 个答案:

答案 0 :(得分:0)

首先,reverseGeocodeLocation以异步方式运行,因此您必须使用完成处理程序模式。

但是,其次,反向地理编码是不必要的,因为item中的response.mapItems具有name属性。因此,请将其用作注释的标题。

例如:

func performSearch(searchField: String) {
    matchingItems.removeAll()

    //search request
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchField
    request.region = map.region

    // process the request
    let search = MKLocalSearch(request: request)
    search.startWithCompletionHandler { response, error in
        guard let response = response else {
            print("There was an error searching for: \(request.naturalLanguageQuery) error: \(error)")
            return
        }

        for item in response.mapItems {
            let annotation = MKPointAnnotation()
            annotation.title = item.name
            annotation.subtitle = item.placemark.title
            annotation.coordinate = item.placemark.coordinate
            self.map.addAnnotation(annotation)
            self.matchingItems.append(item)
        }
    }
}