ios - 使用默认 map View 图钉的 Swift Mapview 自定义标注 View

标签 ios swift mapkit mapkitannotation mkpointannotation

我相信这将是一个非常简单的答案,但我一直在努力弄清楚如何使用 map View 默认图钉添加自定义标注 View 。使用我当前的代码,我似乎只能将图像添加为 MKPointAnnotation 而不是默认引脚。第一个“viewFor 注释”是我设置默认引脚的方式,而下面的所有内容都是针对自定义标注 View 的……我想要做的是使用默认引脚设置自定义标注 View 。如果我想要自定义调出 View ,是否必须添加自定义图像图钉?

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation { return nil }

    if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "") {
        annotationView.annotation = annotation
        return annotationView
    } else {
        let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:"")
        annotationView.isEnabled = true
        annotationView.canShowCallout = true

        let btn = UIButton(type: .detailDisclosure)
        annotationView.rightCalloutAccessoryView = btn
        return annotationView
    }
}


func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation { return nil }
    var annotationView = self.mapView.dequeueReusableAnnotationView(withIdentifier: "Pin")
    if annotationView == nil{
        annotationView = CustomBusinessCallOutAnnotatiion(annotation: annotation, reuseIdentifier: "Pin")
        annotationView?.canShowCallout = false
    }else{
        annotationView?.annotation = annotation
    }
    annotationView?.image = UIImage(named: "car")
    return annotationView
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {

    if view.annotation is MKUserLocation { return }

    let customAnnotation = view.annotation as! CustomBusinessPoint
    let views = Bundle.main.loadNibNamed("CustomBusinessCallOut", owner: nil, options: nil)
    let calloutView = views?[0] as! CustomBusinessCallOut


    calloutView.businessName.text = customAnnotation.businessName
    calloutView.businessStreet.text = customAnnotation.businessStreet
    calloutView.businessState.text = customAnnotation.businessState
    calloutView.businessDistance.text = customAnnotation.businessDistance


    calloutView.center = CGPoint(x: view.bounds.size.width / 2, y: -calloutView.bounds.size.height * -0.0001)
    view.addSubview(calloutView)
    mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    if view.isKind(of: CustomBusinessCallOutAnnotatiion.self) {
        for subview in view.subviews {
            subview.removeFromSuperview()
        }
    }
}

最佳答案

您不需要添加 SubView calloutView。您可以使用 MKAnnotationView 作为自定义标注。

例如你应该整理源码

实现 MKAnnotationMKAnnotationView 的子类。

class PinAnnotation : NSObject, MKAnnotation {
    var coordinate : CLLocationCoordinate2D
    var title: String?
    var calloutAnnotation: CustomBusinessCallOut?

    init(location coord:CLLocationCoordinate2D) {
        self.coordinate = coord
        super.init()
    }
}

class CustomBusinessCallOut : NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?

    init(location coord:CLLocationCoordinate2D) {
        self.coordinate = coord
        super.init()
    }
}

class CalloutAnnotationView : MKAnnotationView {
}

实现 mapView 委托(delegate)方法。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    if annotation is PinAnnotation {
        let reuseId = "Pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        }
        else {
            pinView?.annotation = annotation
        }

        return pinView
    } else if annotation is CustomBusinessCallOut {
        let reuseId = "Callout"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = CalloutAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView?.addSubview(UIImageView(image: UIImage(named: "car")))
        }
        else {
            pinView?.annotation = annotation
        }

        return pinView
    } else {
        return nil
    }
}

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    guard view.annotation is PinAnnotation else { return }
    if let pinAnnotation = view.annotation as? PinAnnotation {
        let calloutAnnotation = CustomBusinessCallOut(location: pinAnnotation.coordinate)
        calloutAnnotation.title = pinAnnotation.title
        pinAnnotation.calloutAnnotation = calloutAnnotation
        mapView.addAnnotation(calloutAnnotation)
    }
}

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    guard view.annotation is PinAnnotation else { return }
    if let pinAnnotation = view.annotation as? PinAnnotation,
        let calloutAnnotation = pinAnnotation.calloutAnnotation {
        mapView.removeAnnotation(calloutAnnotation)
        pinAnnotation.calloutAnnotation = nil
    }
}

关于ios - 使用默认 map View 图钉的 Swift Mapview 自定义标注 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52582638/

相关文章:

ios - 倒数计时器的圆形进度条/PIE

ios - 从 Swift 2.3 更新到 Swift 3.0 后类协议(protocol)不起作用

ios - 移动到另一个城市时触发通知

快速参数类型 () -> ()

ios - 将 agvtool 与一个目录中的多个 .xcodeproject 文件一起使用

dictionary - 如何将项目字典添加到另一个字典中

ios - 为什么我的 MapKit 标注 View 这么小,没有显示它需要显示的所有内容?

ios - MapKit 异常 : Cannot remove an observer for the key path "title"

iPhone MapKit - 同时删除多个图钉

ios - react native : RCT-Folly fatal error - 'fmt/compile.h' file not found