gpt4 book ai didi

ios - Swift 3.0 map viewForAnnotation 未输出正确的标注

转载 作者:行者123 更新时间:2023-11-28 15:25:24 27 4
gpt4 key购买 nike

我无法理解 viewForAnnotation 方法的代码及其调用时间。我的意图是根据图钉颜色显示特定的标注。第一次加载 mapView 时,标注会正确显示,但是在切换 View Controller 后,标注按钮无法根据图钉颜色正确显示。我有一种预感,这可能与调用 viewForAnnotation 时有关(是否每次 map 出现时都调用它)。我无法弄清楚哪里出了问题,因为我不确定代码的每一部分都做了什么。

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
}

}

最佳答案

方法func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?MapViewDelegate的委托(delegate)方法,每次调用时都会执行该方法您的 mapView 在区域中显示注释,如果实现了,您可以在 map 中将 annotationView 的任何子类显示为 annotationView,要显示自定义标注,您需要实现此方法MapViewDelegate

//example code
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView){
let customCallout = CustomCallout(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width * 0.8, height: 50))
customCallout.backgroundColor = UIColor.red //your color here
view.addSubView(customCallout)
//adjust any param you need here
}

希望对你有帮助

关于ios - Swift 3.0 map viewForAnnotation 未输出正确的标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45330290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com