Ale*_*lec 7 annotations mapkit mklocalsearch 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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激,提前感谢.
小智 6
第一期:
需要在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
}
Run Code Online (Sandbox Code Playgroud)
对于第二个问题:
首先,在中calloutAccessoryControlTapped,可以直接访问注释,view.annotation因此使用selectedAnnotations数组是不必要的.
接下来,openMapsWithItems预计数组MKMapItem对象,但在数组中您传递([selectedLoc, currentLoc]),selectedLoc是不是一个MKMapItem-它只是一些对象实现MKAnnotation.
运行此代码将导致此错误崩溃:
- [MKPointAnnotation dictionaryRepresentation]:发送到实例的无法识别的选择器
当地图应用尝试使用时selectedLoc,就像它是一个MKMapItem.
相反,您需要MKMapItem从selectedLoc注释创建一个.这可以通过首先MKPlacemark使用注释MKPlacemark(coordinate:addressDictionary:)创建注释然后使用创建MKMapItem地标来完成MKMapItem(placemark:).
例:
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)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5718 次 |
| 最近记录: |