0
我无法理解viewForAnnotation方法的代码以及何时调用它。我的意图是根据引脚颜色的颜色显示特定的标注。在第一次加载mapView时,标注显示正确,但是在切换视图控制器后,根据引脚颜色不能正确显示标注按钮。我有一个预感,这可能与viewForAnnotation调用时有关(无论每次出现地图时调用它)。我无法弄清楚什么是错误的,因为我不确定代码的每个部分是干什么的。Swift 3.0 Map viewForAnnotation不输出正确标注
func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if (annotation is MKUserLocation) {
return nil
}
let identifier = "pinAnnotation"
// In particular I am not sure what the code below does
if let pin = annotation as? ColorPointAnnotation {
if let view = map.dequeueReusableAnnotationView(withIdentifier: identifier) as? MKPinAnnotationView {
view.annotation = annotation
view.pinTintColor = pin.color ?? .purple
return view
} else {
let view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
view.isEnabled = true
view.canShowCallout = true
view.pinTintColor = pin.color ?? .purple
let btn = UIButton(type: .detailDisclosure)
view.rightCalloutAccessoryView = btn
if view.pinTintColor == .red || view.pinTintColor == .green {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
button.setBackgroundImage(UIImage(named: "car"), for: .normal)
button.addTarget(self, action: #selector(MapVC.getDirections), for: .touchUpInside)
view.leftCalloutAccessoryView = button
} else {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
button.setBackgroundImage(UIImage(named: "other"), for: .normal)
button.addTarget(self, action: #selector(MapVC.other), for: .touchUpInside)
view.leftCalloutAccessoryView = button
}
return view
}
}
if let annotationView = map.dequeueReusableAnnotationView(withIdentifier: identifier) {
annotationView.annotation = annotation
return annotationView
} else {
let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier: identifier)
annotationView.isEnabled = true
annotationView.canShowCallout = true
let btn = UIButton(type: .detailDisclosure)
annotationView.rightCalloutAccessoryView = btn
if annotationView.pinTintColor == .red || annotationView.pinTintColor == .green {
let smallSquare = CGSize(width: 120, height: 120)
let button = UIButton(frame: CGRect(origin: CGPoint(x: 0,y :0), size: smallSquare))
button.setBackgroundImage(UIImage(named: "car"), for: .normal)
button.addTarget(self, action: #selector(MapVC.getDirections), for: .touchUpInside)
annotationView.leftCalloutAccessoryView = button
} else {
let button = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
button.setBackgroundImage(UIImage(named: "other"), for: .normal)
button.addTarget(self, action: #selector(MapVC.other), for: .touchUpInside)
annotationView.leftCalloutAccessoryView = button
}
return annotationView
}
}
为了澄清,当你说的MapView显示了重新注解gion,你的意思是如果注释被放置,或者用户看到的当前视图是否有注释。还有什么意思,“把任何UIView作为注释在地图上”? – Kevin
@Kevin是的,如果注释添加在一个区域内的坐标,然后显示使用这种委托方法,如果实施,再次检查我的答案已更新 –
@Kevin您正在使用2种类型的注释?,我想missunderstood您的问题 –