该代码用于初始化注释的视图,但是右侧的辅助视图在地图中不可见。
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var view = mapView.dequeueReusableAnnotationView(withIdentifier: "Map")
if view == nil {
view = MKAnnotationView(annotation: annotation, reuseIdentifier: "Map")
view?.canShowCallout = true
let btn = UIButton(type: .detailDisclosure)
view?.rightCalloutAccessoryView = btn
}
view?.annotation = annotation
return view
}
我不知道问题是什么。
最佳答案
用这个
func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
if annotation is MKUserLocation {
return nil
}
let reuseId = "AddPin"
let pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
if pinView == nil
{
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.pinColor = .Red
pinView!. leftCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
} else {
pinView!.annotation = annotation
}
return pinView
}