Swift - 从地图中的当前位置到所选注释的方向

Swift - Directions to Selected Annotation from Current Location in Maps

我的应用目前有一个本地搜索,它为搜索结果添加了注释。我想将其设置为当您 select 注释并单击调出按钮时,它将在地图应用程序中打开,并显示从当前设备位置到注释的方向。我遇到了一些问题。

首先,我在注释上的呼出按钮没有出现。其次,我认为我没有正确检测到 selected 注释。

这是我的搜索代码和 selected 注释:

func performSearch() {

    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
                annotation.coordinate = item.placemark.coordinate
                annotation.title = item.name
                self.mapView.addAnnotation(annotation)

            }
        }
    })
}



func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
    calloutAccessoryControlTapped control: UIControl!) {

        if self.mapView.selectedAnnotations?.count > 0 {

            if let selectedLoc = self.mapView.selectedAnnotations[0] as? MKAnnotation {
                println("Annotation has been selected")
                let currentLoc = MKMapItem.mapItemForCurrentLocation()
                let mapItems = NSArray(objects: selectedLoc, currentLoc)
                let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
                MKMapItem.openMapsWithItems([selectedLoc, currentLoc], launchOptions: launchOptions)
            }
        }

}

任何帮助将不胜感激,在此先感谢。

第一期:

需要在 viewForAnnotation 委托方法中明确设置标注按钮(默认的红色引脚没有)。这是一种可能实现的简单示例:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if annotation is MKUserLocation {
        return nil
    }

    let reuseId = "pin"

    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    if pinView == nil {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        pinView!.canShowCallout = true
        pinView!.pinColor = .Purple

        //next line sets a button for the right side of the callout...
        pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIButton
    }
    else {
        pinView!.annotation = annotation
    }

    return pinView
}


第二期:

首先,在 calloutAccessoryControlTapped 中,可以使用 view.annotation 直接访问注释,因此不需要在其中使用 selectedAnnotations 数组。

接下来,openMapsWithItems 需要一个 MKMapItem 对象数组,但在您传递的数组中 ([selectedLoc, currentLoc]),selectedLoc 不是 一个 MKMapItem -- 它只是一些实现 MKAnnotation.

的对象

运行 此代码将导致崩溃并出现以下错误:

-[MKPointAnnotation dictionaryRepresentation]: unrecognized selector sent to instance

当地图应用程序尝试将 selectedLoc 当作 MKMapItem 使用时。

相反,您需要从 selectedLoc 注释创建一个 MKMapItem。这可以通过首先使用 MKPlacemark(coordinate:addressDictionary:) 从注释创建 MKPlacemark 然后使用 MKMapItem(placemark:).

从地标创建 MKMapItem 来完成

示例:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!,
    calloutAccessoryControlTapped control: UIControl!) {

        let selectedLoc = view.annotation

        println("Annotation '\(selectedLoc.title!)' has been selected")

        let currentLocMapItem = MKMapItem.mapItemForCurrentLocation()

        let selectedPlacemark = MKPlacemark(coordinate: selectedLoc.coordinate, addressDictionary: nil)
        let selectedMapItem = MKMapItem(placemark: selectedPlacemark)

        let mapItems = [selectedMapItem, currentLocMapItem]

        let launchOptions = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]

        MKMapItem.openMapsWithItems(mapItems, launchOptions:launchOptions)
}