Swift - 从坐标获取地址

时间:2015-05-04 22:08:10

标签: ios swift mapkit mkannotation

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

我遇到的问题是,为了使呼出附件正常工作,您必须使用地址簿导入中的地点标记来获取地址。我已经做了很多搜索,无法弄清楚如何正确设置它,我可以将注释坐标转换为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)
}

非常感谢任何帮助,提前谢谢。

1 个答案:

答案 0 :(得分:1)

你的假设是错误的。您可以使用纬度/经度坐标生成驾驶路线请求,就像使用地址一样容易。

这是你做的事情:

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

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

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

  4. 编辑:

    以下是如何为用户点按的项目创建地图项目:

    在calloutAccessoryControlTapped方法中,您将传递一个注释视图。 MKAnnotationView类具有注释属性,该对象符合MKAnnotation属性。

    MKAnnotation对象具有坐标属性。

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

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

    该电话可能如下所示:

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

    然后您使用地方标记并使用initWithPlacemark:方法创建MKMapItem。

    获取用户当前位置的MKMapItem:

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