Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Swift – 从地图中的当前位置选择注释的方向大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我的应用程序目前有一个本地搜索,为搜索结果添加注释.我想设置它,当您选择注释并单击调出按钮时,它将在地图应用程序中打开,其中包含指向当前设备位置的注释的说明.我遇到了一些问题. 首先,我没有出现注释上的调出按钮.其次,我认为我没有正确检测选定的注释. 这是我的搜索和选定注释的代码: func performSearch() { matchingItems.removeAll()
我的应用程序目前有一个本地搜索,为搜索结果添加注释.我想设置它,当您选择注释并单击调出按钮时,它将在地图应用程序中打开,其中包含指向当前设备位置的注释的说明.我遇到了一些问题.

首先,我没有出现注释上的调出按钮.其次,我认为我没有正确检测选定的注释.

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

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委托方法中显式设置callout按钮(认红色引脚没有).以下是一个可能实现的简单示例:

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需要@L_630_0@mKMapItem对象数组,但是在你传递的数组中([SELEctedLoc,currentLoc]),SELEctedLoc不是MKMapItem – 它只是一个实现MKAnnotation的对象.

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

当地图应用尝试使用SELEctedLoc时,就像它是MKMapItem一样.

相反,您需要从SELEctedLoc注释创建MKMapItem.这可以通过首先使用MKPlacemark(坐标:addressDictionary :)从注释创建MKPlacemark,然后使用MKMapItem(地标:)从地标创建MKMapItem来完成.

例:

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

        let SELEctedLoc = view.Annotation

        println("Annotation '\(SELEctedLo@R_801_5740@!)' 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)
}

大佬总结

以上是大佬教程为你收集整理的Swift – 从地图中的当前位置选择注释的方向全部内容,希望文章能够帮你解决Swift – 从地图中的当前位置选择注释的方向所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。