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

Swift-从地图中的当前位置到选定注释的方向,swift,annotations,mapkit,mklocalsearch,Swift,Annotations,Mapkit,Mklocalsearch,我的应用程序目前有一个本地搜索,为搜索结果添加注释。我想设置它,当您选择注释并单击调出按钮时,它将在地图应用程序中打开,并从当前设备位置指向注释。我有一些问题 首先,注释上的“我的调出”按钮不会出现。其次,我认为我没有正确地检测到所选注释 以下是我的搜索代码和所选注释: func performSearch() { matchingItems.removeAll() let request = MKLocalSearchRequest() request.natural

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

首先,注释上的“我的调出”按钮不会出现。其次,我认为我没有正确地检测到所选注释

以下是我的搜索代码和所选注释:

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]:发送到实例的无法识别的选择器

当地图应用程序尝试使用selectedLoc时,就好像它是一个MKMapItem

相反,您需要从selectedLoc注释创建一个MKMapItem。可以通过以下方法完成此操作:首先使用MKPlacemarkcoordinate:addressDictionary:从注释创建MKPlacemark,然后使用MKMapItemplacemark:从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)
}

不知怎的,我在不知道有人在玩我的手机的情况下否决了这个答案。我为此道歉。现在我无法撤销。如果你愿意,你可以进行编辑,这样我就可以还原。非常感谢你的帮助!我的最后一个问题是。。。如何将视图指定给performSearch中创建的注释?添加代码并运行后,注释视图仍然是默认的红色pin。您不直接调用viewForAnnotation方法,因为它是委托方法。地图视图将自动调用它。但故事板中地图视图的代理出口必须连接到视图控制器。或者,在代码中的viewDidLoad中,执行mapView.delegate=self。如果未设置地图视图的委托,则即使已实现,也不会调用委托方法,地图视图将执行其默认操作。工作正常,非常感谢您的帮助!我仍在学习过程中,很抱歉用这么多问题打扰你:D