简体   繁体   中英

How do I add a UIView on top of a MKAnnotationView and then display it on a MKMapView?

My goal is to have custom pins on a MKMapView . I have a UIView with a label on it and I'd like to use it for the custom pins. I've implemented the viewForAnnotation method to display my UIView on the map.

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return mapView.dequeueReusableAnnotationViewWithIdentifier("")
    } else {
        let annotationView = MKAnnotationView()
        annotationView.canShowCallout = false
        annotationView.addSubview(myView) // myView is the regular UIView
        return annotationView
    }
}

The pins are now displayed, but they're placed randomly on the map. I think this is because I have to create a MKAnnotation and assign coordinates to it. How do I add a UIView to a MKAnnotationView and then display it on a MKMapView ?

您省略了一个微小但关键的步骤:在每种情况下,您必须将传入注释分配给注释视图的annotation属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM