Why does MKMapView show user's location just as a pin?
Asked Answered
Z

2

2

I have a mapView in which I want to show user's current location. However, I don't want to centre the mapView to that location (it is centred to other location). So, for that reason, I don't get coordinates for the user's location to set the region. I just call showsUserLocation and set it to true. Everything works fine, except the fact that this makes my mapView show user's current location just as a pin. I would like it to be in a form of a default blue dot.

Related to the problem above, I have two questions: 1) Why does it show user's current location as a pin? 2) How can I make it show user's current location as a blue dot?

If you know the answers to these questions (or only to one of them) I would appreciate your help.

Zuzana answered 22/11, 2017 at 23:39 Comment(0)
F
2

Everything depends on your implementation of the map view delegate's mapView(_:viewFor:). You are probably returning a pin annotation.

You can distinguish the annotation provided by the map view as the user's location by checking its class, which will be MKUserLocation. If you want the default blue dot, return nil for that class.

Familiarize answered 22/11, 2017 at 23:43 Comment(7)
So, I should draw the dot on my own, right? But in that case, I've been watching tutorials (some of them are about really basic stuff of MKMapView) trying to figure out how to do that. So, in that tutorials the dot is appearing without calling mapView(_:viewFor:) method. That's why I am curious about this.Zuzana
What you're seeing may depend on other code you have not shown us. And different systems use different default annotation views to show the user location, so it may depend on that too.Familiarize
See also #6078857Familiarize
Well, I'm actually implementing mapView(_:viewFor:) method for my custom annotations. Could that be the reason? If needed, I can show the code for calling that method.Zuzana
Right, well in that case you need to check for MKUserLocation and return nil.Familiarize
Thanks, it helped.Zuzana
I'll correct my answer to reflect what we figured out! :)Familiarize
C
0

In Swift 5

We need to check the type of annotation, if it is a user location do not want to return a pin:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation {
        return nil
    }

    [... code for the other annotations ... ]

}

Now you will see your location as a classic blue dot.

Cabochon answered 19/7, 2021 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.