【问题标题】:Swift 3.0 Map viewForAnnotation Not Outputting Proper CalloutSwift 3.0 Map viewForAnnotation 没有输出正确的标注
【发布时间】:2017-07-26 14:43:45
【问题描述】:

我无法理解 viewForAnnotation 方法的代码以及何时调用它。我的意图是根据引脚颜色的颜色显示特定的标注。首次加载 mapView 时,标注显示正确,但切换视图控制器后,标注按钮未根据引脚颜色正确显示。我有一种预感,这可能与调用 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
}

}

【问题讨论】:

    标签: ios swift mkmapview mkannotation mkannotationview


    【解决方案1】:

    方法func mapView(_ map: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?MapViewDelegate的一个委托方法,每次你的mapView在一个Region中显示Annotation时都会执行这个方法,如果实现了你可以将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
    }
    

    希望对你有帮助

    【讨论】:

    • 澄清一下,当你说mapView在一个区域中显示一个注解时,你的意思是是否放置了注解,或者用户看到的当前视图是否有注解。另外,“将任何 UIView 作为注释放在地图中”到底是什么意思?
    • @Kevin 是的,如果在区域内的坐标中添加注释,则使用此委托方法显示,如果实施,请再次检查我的答案是否已更新
    • @Kevin 你正在使用两种类型的注解?,我想你的问题被误解了
    • 使用了未解析的标识符 CustomCallout。所以你的意思是我可以在 didSelectView 委托中创建一个我需要的标注?
    • 我无法访问此 didSelectView 委托中的注释图钉颜色。我怎样才能访问它?有了这个我想我的答案就解决了
    猜你喜欢
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-17
    • 2017-01-03
    • 2012-09-26
    相关资源
    最近更新 更多