Hallo iedereen, Leo hier. The topic today is how to remove/suppress the Default MapKit user location callout annotation.

We will explore a quick tip about MapKit. MapKit has a handy property that you can turn on to show the user location on the map called showsUserLocation. When you set it to true it shows the native user location pin on the map. Very nice, right?

Yes, it is! But the problem is it also comes with a callout annotation that has a default user image in it and that may not be desirable.

Today let’s find out how to add the user location to your map, and how to remove the default callout.

Let’s code! But first…

 

The Painting of The Day

The painting I chose is called The Swing a 1767 art piece from Jean-Honore Fragonard.

Best known for his flourishingly hedonistic scenes, Jean Honoré Fragonard was a French Rococo painter and printmaker, who was one of the most prolific painters of the Ancién Regime. He showed a great talent for art at an early age and was sent to study with the Rococo painter Francois Boucher, who soon trusted him enough to paint replicas of his works.

I chose this painting because in all these years I never shared a Rococo-style painting. So I had to fill that gap!

 

The Problem – How to remove/suppress the Default MapKit User Location Callout Annotation

You want to use the user location map pin annotation but you don’t want the default callout.

This will be a very quick post. The current location map pin is this:
iOS map pin in Swift is an image of white circle with a blue inner circle

To enable it in your maps using MapKit. You just need to set the showsUserLocation property in your MKMapView. Like this:

mapView.showsUserLocation = true

Simple as it!

When you tap it, by default it will show this default map annotation callout:

default user callout bubble in iOS swift development how to remove the default annotation example image

Our goal today is to remove that behavior and don’t show this callout. To achieve that we need to do a couple of things:

  1. Use the MKMapViewDelegate to intercept the didSelect.
  2. In the didSelect we search for MKUserLocation annotation.
  3. If the annotation inside the view is the MKUserLocation we deselect the annotation and return doing nothing.

The example is below:

internal func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    if view.annotation is MKUserLocation {
        mapView.deselectAnnotation(view.annotation, animated: false)
        return
    }
    
    // all other didSelect annotation logic
}

And that’s it!

 

Summary – How to remove/suppress the Default MapKit User Location Callout Annotation

Today we learn how to add the user’s current location map pin to the map view and how to remove the default annotation callout of it. Searching for this problem I wasn’t able to find any other good solution for this problem. If you have a better one, please share with me!

That’s all my people, I hope you liked reading this article as much as I enjoyed writing it. If you want to support this blog you can Buy Me a Coffee or leave a comment saying hello. You can also sponsor posts and I’m open to freelance writing! You can reach me on LinkedIn or Twitter and send me an e-mail through the contact page.

Thanks for reading and… That’s all folks.

Credits:

title image