Leonardo Maia Pugliese
Holy Swift

Holy Swift

How to remove/suppress the default MapKit user location callout annotation

How to remove/suppress the default MapKit user location callout annotation

How hard can be to try to remove an annotation from MapKit?

Leonardo Maia Pugliese's photo
Leonardo Maia Pugliese
·Feb 14, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Hallo iedereen, Leo hier.

Today 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 in 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

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 print maker, 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

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: Screenshot 2022-02-14 at 08.07.39.png

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:

Screenshot 2022-02-14 at 08.08.28.png

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

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 below!

That's all my people, I hope you liked it as I enjoyed writing this article. If you want to support this blog you can Buy Me a Coffee or just leave a comment saying hello. You can also sponsor posts and I'm open to writing freelancing! Just reach me in LinkedIn or Twitter for details.

Thanks for the reading and... That's all folks.

credits: image

Did you find this article valuable?

Support Leonardo Maia Pugliese by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
 
Share this