Mani
Mani

Reputation: 1320

How to always show map view annotation callouts?

How do you always show the annotation callouts, i.e. don't hide them when you tab the map view?

annotation callout

Upvotes: 1

Views: 2573

Answers (4)

Gabs
Gabs

Reputation: 422

I just set isSelected property to true on viewFor annotation method and that is all.

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }
    
    let annotationV = MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
    annotationV.image = UIImage(named: "ZeusSurveyMarkerTaskIcon", in: Bundle(for: ZsurveysGeofenceLocationMapView.self), compatibleWith: nil)
    annotationV.canShowCallout = true
    annotationV.isSelected = true
    
    return annotationV
}

Upvotes: 0

Werner Kratochwil
Werner Kratochwil

Reputation: 382

Thanks, @Zumry Mohammed for this idea. This solution in swift works for me:

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
    guard let ann = view.annotation else {return}
    mapView.removeAnnotation(ann)
    mapView.addAnnotation(ann)
    mapView.selectAnnotation(ann, animated: false)
}

Upvotes: 0

Zumry Mohamed
Zumry Mohamed

Reputation: 9558

Resetting the annotations also will bring the callout to view state true.

    [mapView removeAnnotation: currentMarker];
    [mapView addAnnotation:currentMarker];

Upvotes: 2

Daniel
Daniel

Reputation: 23359

The callout is shown when an MKAnnotationView is selected and the view's canShowCallout property is set to YES.

It is then hidden when that MKAnnotationView is deselected. This occurs by tapping another annotation view, or by tapping outside of the currently selected annotation view.

As the delegate of MKMapView (conforming to MKMapViewDelegate), you are told when an annotation view is selected and deselected, but it's too late to do anything about it.

If you want to not deselect an annotation view, you should subclass MKAnnotationView and override the setSelected:animated: method and stop the annotation view from being deselected.

Upvotes: 1

Related Questions