Swift-从当前位置的地图中的选定注释指示



我的应用当前有一个本地搜索,为搜索结果添加了注释。我想在选择注释并单击"呼叫输出"按钮时进行设置,它将在地图应用程序中打开,并在当前设备位置进行注释。我对此有一些问题。

首先,注释上的我的呼叫按钮没有出现。其次,我认为我无法正确检测到选定的注释。

这是我用于搜索的代码和选定的注释:

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 a MKMapItem-这只是实现MKAnnotation的某个对象。

运行此代码将导致此错误的崩溃:

- [mkpointannotation dictionaryReSentation]:未识别的选择器已发送到实例

当Maps应用程序尝试使用selectedLoc时,就好像是MKMapItem

相反,您需要从selectedLoc注释中创建MKMapItem。这可以通过首先使用MKPlacemark(coordinate:addressDictionary:)从注释中创建MKPlacemark,然后使用MKMapItem(placemark:)从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)
}

相关内容

  • 没有找到相关文章

最新更新