How to Automatically Display Title/Subtitle on Map Annotation (pin)

后端 未结 4 1977
甜味超标
甜味超标 2020-12-31 10:19

I am loading an annotation onto my map view. The annotation displays as a pin when the map is loaded.

However, the title and subtitle do not appear on the pin automa

4条回答
  •  旧时难觅i
    2020-12-31 11:07

    Starting in iOS 11, there is a new type of MKAnnotationView called MKMarkerAnnotationView, which can display title and subtitle without being selected. Check https://developer.apple.com/documentation/mapkit/mkmarkerannotationview

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
        guard !(annotation is MKUserLocation) else {
            return nil
        }
    
        if #available(iOS 11.0, *) {
            let annoView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "HunterAnno")
            annoView.canShowCallout = true
            return annoView
        }
    
        let annoView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "HunterAnnoLow")
        annoView.canShowCallout = true
        return annoView
    }
    

提交回复
热议问题