Mapbox-gl-native: Display custom annotation for user location

Created on 3 Apr 2017  Â·  7Comments  Â·  Source: mapbox/mapbox-gl-native

Platform:
iOs
Mapbox SDK version:
3.5.0

Steps to trigger behavior

  1. Implement func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView?

Expected behavior

I am expecting to see it called even when there are no other annotations than the user location
in any case, receiving an annotation that is userLocation, so that I can provide a custom annotationview

Actual behavior

The method gets called only with locations with the MGLPoint annotations that I append to the map.
As an example, if I have one pin on the map, the method gets fired once alone.

In general I probably didn't understand how to customize the user location AnnotationView, but from some research in s.o. and google this seems to be the proper way to do it. I noticed that in Android there is a very handy example in the documentation, that seems to be missing in iOs.

https://www.mapbox.com/android-sdk/examples/user-location/

Thanks!

iOS support

Most helpful comment

Platform:
iOS
Mapbox SDK version:
3.5.2

I had been seeing the same behavior as @superandrew did. -mapView:viewForAnnotation: would get called only for custom MGLPoint annotations that I appended to the map. Then I turned off "Shows user location" for the mapView in Storyboard and added this

extension ViewController: MGLMapViewDelegate {
    func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
        mapView.showsUserLocation = true
    }

Now it works as expected.

All 7 comments

-mapView:viewForAnnotation: is the correct place to set a custom user location annotation. Here’s an in-progress example of a basic implementation.

If you’re not seeing MGLUserLocation in -mapView:viewForAnnotation:, make sure that MGLMapView.showsUserLocation is enabled.

I must be doing something really wrong because I can see the blue slow flashing dot, so showsUserLocation is enabled.

Double checking now. Any other property that might influence this behavior?

Platform:
iOS
Mapbox SDK version:
3.5.2

I had been seeing the same behavior as @superandrew did. -mapView:viewForAnnotation: would get called only for custom MGLPoint annotations that I appended to the map. Then I turned off "Shows user location" for the mapView in Storyboard and added this

extension ViewController: MGLMapViewDelegate {
    func mapViewDidFinishLoadingMap(_ mapView: MGLMapView) {
        mapView.showsUserLocation = true
    }

Now it works as expected.

I am seeing the same behavior that @superandrew is having and after updating to exactly as @swasta has stated the issue is solved. Thanks for the help! Saved me from a big headache!

Edit: Never mind it is very inconsistent. It seems to work only sometimes.

It’s unnecessary to wait until -[MGLMapViewDelegate mapViewDidFinishLoadingMap:] — just be sure to set MGLMapView.delegate _before_ setting MGLMapView.showsUserLocation (or any other property that implicitly enables it), otherwise -[MGLMapViewDelegate mapView:viewForAnnotation:] won’t be called as the user location annotation is initializing.

Here is an example of how to implement a custom user location annotation.

I had the same issue and found a simple workaround.
For future reference, if you are setting the showUserLocation via code, make sure it is turned off in the storyboard; no need to implement mapViewDidFinishLoadingMap.

Encountering this issue on iOS. I have tried setting showsUserLocation after setting the MGLMapView.delegate as well as the suggestion from @swasta to no avail. The only real distinction I'm seeing between my code and the example code is that in my code, the delegate is a UIView, not UIViewController. Could this be the underlying issue?

Was this page helpful?
0 / 5 - 0 ratings