首先,我没有出现注释上的调出按钮.其次,我认为我没有正确检测选定的注释.
这是我的搜索和选定注释的代码:
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需要一个MKMAPItem对象数组,但是在你传递的数组中([selectedLoc,currentLoc]),selectedLoc不是MKMAPItem – 它只是一个实现MKAnnotation的对象.
运行此代码将导致此错误崩溃:
-[MKPointAnnotation dictionaryRepresentation]: unrecognized selector sent to instance
当地图应用尝试使用selectedLoc时,就像它是MKMAPItem一样.
相反,您需要从selectedLoc注释创建MKMAPItem.这可以通过首先使用MKPlacemark(坐标:addressDictionary :)从注释创建MKPlacemark,然后使用MKMAPItem(地标:)从地标创建MKMAPItem来完成.
例:
func mapVIEw(mapVIEw: MKMapVIEw!,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)}总结
以上是内存溢出为你收集整理的Swift – 从地图中的当前位置选择注释的方向全部内容,希望文章能够帮你解决Swift – 从地图中的当前位置选择注释的方向所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)