Swift - 从坐标获取地址

3
我有一个本地搜索功能,为每个搜索结果创建注释。我尝试为每个注释添加一个呼叫附件,一旦按下该附件,地图应用程序将打开并设置方向以到达特定位置。
我遇到的问题是,为了使呼叫附件正常工作,必须使用地址簿导入中的地点标记获取地址。我已经进行了大量搜索,但无法弄清楚如何正确设置它,以便我可以将注释坐标转换为 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个回答

1
你的假设是错误的。使用纬度/经度坐标生成驾驶路线请求和使用地址一样简单。
以下是操作步骤:
1. 获取用户选择的标记的MKMapItem。 2. 使用MKMapItem类的mapItemForCurrentLocation()方法,为用户的当前位置创建第二个MKMapItem。 3. 调用MKMapItem类方法openMapsWithItems(),传入当前位置和目的地的两个MKMapItems,以及一个launchOptions字典,指定MKLaunchOptionsDirectionsModeDriving。
编辑:
以下是如何为用户点击的项目创建地图项:
在你的calloutAccessoryControlTapped方法中,你会收到一个注释视图(annotation view)。 MKAnnotationView类有一个annotation属性,该属性是符合MKAnnotation协议的对象。
MKAnnotation对象有一个coordinate属性。
你可以从注释视图获取注释对象,然后从注释对象获取坐标。
然后,你可以使用该坐标创建一个MKPlacemark,使用initWithCoordinate:addressDictionary:方法(传入nil addressDictionary)。
那个电话可能是这样的:
let sourcePlacemark = MKPlacemark(
      coordinate: fromCoordinate,
      addressDictionary: nil)

你可以使用这个方法,将地点标记转换为MKMapItem对象:initWithPlacemark:

获取用户当前位置的MKMapItem:

有一个单独的MKMapView方法forCurrentLocation(),它返回当前位置的MKMapItem对象。

没错。您可以使用当前位置和目的地的经纬度获取方向。 - Duncan C
在你提到的“传入当前位置和目的地的两个MKMapItems”的部分,你能具体说明如何做到这一点吗?还有,如何获取当前选定的MKMapItem,我不确定我是否做对了。 - Alec
我已经详细地解释了。如果不写出基本的Swift语句,你可能会遇到困难,需要回过头来多读一些书,然后再尝试这样的事情。我开始学习Swift是通过阅读苹果iBook《Swift编程语言》。我不确定如何在不为你编写代码的情况下提供更多信息。 - Duncan C
尝试按照我概述的步骤进行工作。如果遇到困难,请回帖。 - Duncan C
你的init调用语法完全错误。我在我的答案中添加了创建标记的示例代码。 - Duncan C
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接