Swift - 从坐标获取地址

Swift - Getting address from coordinates

我有一个本地搜索,可以为每个搜索结果创建注释。我正在尝试为每个注释添加一个标注附件,一旦按下该附件,地图应用程序将打开并设置如何到达特定位置的方向。

我遇到的问题是,为了使呼叫附件正常工作,您必须在地址簿导入中使用位置标记获取地址。我进行了大量搜索,但无法弄清楚如何正确设置它,我可以将注释坐标隐藏到 kABPersonAddressStreetKey 中,以便地图应用程序可以正确读取它。下面是我的搜索功能和打开地图应用程序功能的代码。

func performSearch() -> MKMapItem {

    matchingItems.removeAll()
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText.text
    request.region = mapView.region

    let search = MKLocalSearch(request: request)

    search.startWithCompletionHandler({(response:
        MKLocalSearchResponse!,
        error: NSError!) in

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

            for item in response.mapItems as! [MKMapItem] {
                println("Name = \(item.name)")
                println("Phone = \(item.phoneNumber)")

                self.matchingItems.append(item as MKMapItem)
                println("Matching items = \(self.matchingItems.count)")

                var annotation = MKPointAnnotation()
                var coordinates = annotation.coordinate
                var location = CLLocation(latitude: annotation.coordinate.latitude, longitude: annotation.coordinate.longitude)
                var street = // Insert code here for getting address from coordinates
                var addressDictionary = [String(kABPersonAddressStreetKey): street]
                var placemark = MKPlacemark(coordinate: coordinates, addressDictionary: addressDictionary)
                var mapItem = MKMapItem(placemark: placemark)
                return mapItem

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

            }
        }
    })
}


func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
    calloutAccessoryControlTapped control: UIControl!) {
        let location = view.annotation as! FirstViewController
        let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
        location.performSearch().openInMapsWithLaunchOptions(launchOptions)
}

任何帮助将不胜感激,提前致谢。

你的假设是错误的。您可以使用 lat/long 坐标生成行车路线请求,就像使用地址一样简单。

这是您的操作:

  1. 获取用户选择的引脚的 MKMapItem

  2. 为用户的当前位置创建第二个 MKMapItem MKMapItem 方法 mapItemForCurrentLocation().

  3. 调用MKMapItem class方法openMapsWithItems()传入 当前位置和目的地的两个 MKMapItems,以及一个 指定 MKLaunchOptionsDirectionsModeDriving 的 launchOptions 字典。

编辑:

以下是为用户点击的项目创建地图项目的方法:

在您的 calloutAccessoryControlTapped 方法中,您获得了一个注释视图。 MKAnnotationViewclass有一个注解属性,一个符合MKAnnotation属性.

的对象

一个 MKAnnotation 对象有一个坐标 属性。

您可以从注释视图中获取注释对象,并从注释对象中获取坐标。

然后您将获取坐标并使用它创建 MKPlacemark,使用 initWithCoordinate:addressDictionary: 方法(传入 nil addressDictionary)。

该调用可能如下所示:

let sourcePlacemark = MKPlacemark(
      coordinate: fromCoordinate,
      addressDictionary: nil)

然后您将使用位置标记并使用它创建一个 MKMapItem,使用 initWithPlacemark: 方法。

获取用户当前位置的 MKMapItem:

有一个 MKMapView 方法,mapItemForCurrentLocation returns 当前位置的 MKMapItem。