Features added to a mapView through addAnnotation appear below raster tyle sources
Platform: iOS Mapbox SDK version: https://github.com/mapbox/mapbox-gl-native/releases/tag/ios-v3.4.0-beta.1
Steps to trigger behavior
Using the new styles API adding a raster source to the map style makes any annotation appear below the tiles
- Create a new MGLRasterSource with an URL
- Create a new MGLRasterStyleLayer with the previously created source
- Add annotations to mapView
Expected behavior
Annotations should appear on top of the raster layers
Actual behavior
I have a glimpse of the annotations appearing on the map before the tiles load. After tiles finish loading annotations do not show.
It happens either adding annotations conforming to MGLAnnotation protocol or MGLPointFeatures.
I don't know if there's anything I'm doing wrong or it is a bug. We have an app using the old RMTileSources and RMMapLayer where it works as expected.
Thanks
@Jmaguerre To speed this along, could you whip up a quick code demo? Based on the loading behavior, it does sound like a possible bug.
@icanus Okay so while trying to reproduce I think I know what's happening but don't know why. Basically what I'm doing is the following: In the viewDidLoad I have a network call and in the callback I add a raster source and layer to the map. As the mapView initializes before the call finishes it is loaded with the default mapbox streets base layer. Once the call finishes I add the new source and layer (mapbox streets satellite) and also any annotation and I can see the annotation drawn over the mapbox street default layer and afterwards , when the other source loads the annotation is covered.
@IBOutlet var mapView: MGLMapView!
override func viewDidLoad() {
self.client.doSomeNetworkCall(completion: { () -> Void in
let source = MGLRasterSource(identifier: "base", url: URL(string: "mapbox://mapbox.streets-satellite")!, tileSize: 256)
mapView.style().add(source)
let layer = MGLRasterStyleLayer(identifier: "base", source: source)
mapView.style().add(layer)
mapView.setCenter(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
view.addSubview(mapView)
// Declare the marker `hello` and set its coordinates, title, and subtitle.
let hello = MGLPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
hello.title = "Hello world!"
hello.subtitle = "Welcome to my marker"
// Add marker `hello` to the map.
mapView.addAnnotation(hello)
}
}
Result:

So here comes another posible bug. If I do the same thing without having the network call the source added is never loaded.
@IBOutlet var mapView: MGLMapView!
override func viewDidLoad() {
let source = MGLRasterSource(identifier: "base", url: URL(string: "mapbox://mapbox.streets-satellite")!, tileSize: 256)
mapView.style().add(source)
let layer = MGLRasterStyleLayer(identifier: "base", source: source)
mapView.style().add(layer)
mapView.setCenter(CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407), zoomLevel: 12, animated: false)
view.addSubview(mapView)
// Declare the marker `hello` and set its coordinates, title, and subtitle.
let hello = MGLPointAnnotation()
hello.coordinate = CLLocationCoordinate2D(latitude: 40.7326808, longitude: -73.9843407)
hello.title = "Hello world!"
hello.subtitle = "Welcome to my marker"
// Add marker `hello` to the map.
mapView.addAnnotation(hello)
}
Result:

Closing due to ticket age. If this is still a problem, please re-open
this is still a problem. I am running the IOS SDK version 3.7.0 B1. I have several MGLPointAnnotation added to my MGLMapView. They are visible on top of the default streetmap. I use the following code to add a raster layer to the map and the raster layer appears over top of the MGLPointAnnotations. The MGLPointAnnotations are no longer visible. This is also true of MGLPolyline that I have added to the map. The raster layer also hides them.
MGLRasterSource *source = [[MGLRasterSource alloc] initWithIdentifier:@"onlinemapssource" tileURLTemplates:tileSources options:nil];
MGLRasterStyleLayer *layer = [[MGLRasterStyleLayer alloc] initWithIdentifier:@"onlinemapslayer" source:source];
[_map.style addSource:source];
[_map.style addLayer:layer];
An implementation detail worth mentioning is that annotations are backed by a built-in symbol style layer that gets added to the style in after -mapView:didFinishLoadingStyle: gets called but before -mapView:didFinishLoadingMap: gets called.
Until we implement either #6181 or https://github.com/mapbox/mapbox-gl-native/issues/1734#issuecomment-242802281, the annotation and runtime styling APIs don’t always interact intuitively when used together with the same map view, but MGLMapViewDelegate and MGLStyle do provide hooks for avoiding these conflicts.
Okay so while trying to reproduce I think I know what's happening but don't know why. Basically what I'm doing is the following: In the viewDidLoad I have a network call and in the callback I add a raster source and layer to the map. As the mapView initializes before the call finishes it is loaded with the default mapbox streets base layer. Once the call finishes I add the new source and layer (mapbox streets satellite) and also any annotation and I can see the annotation drawn over the mapbox street default layer and afterwards , when the other source loads the annotation is covered.
@Jmaguerre, the network request in your code most likely finishes at some point after the annotation layer is added. A little advertised behavior of the SDK is that you can add arbitrary layers to the top of the style in -mapView:didFinishLoadingMap: or at any time thereafter. https://github.com/mapbox/mapbox-gl-native/issues/7707#issuecomment-290215386 proposes to remove this “feature”.
So here comes another posible bug. If I do the same thing without having the network call the source added is never loaded.
In fact, anything you do to an MGLStyle before -mapView:didFinishLoadingStyle: gets called is ignored. For this reason, we changed MGLMapView.style in #7664 to be optional: this property is now nil up until the call to -mapView:didFinishLoadingStyle:. This change shipped in v3.4.0. Now your code will sometimes fail to display the raster layers, depending on the speed of the network response.
this is still a problem. I am running the IOS SDK version 3.7.0 B1. I have several MGLPointAnnotation added to my MGLMapView. They are visible on top of the default streetmap. I use the following code to add a raster layer to the map and the raster layer appears over top of the MGLPointAnnotations. The MGLPointAnnotations are no longer visible. This is also true of MGLPolyline that I have added to the map. The raster layer also hides them.
@amphib, you can avoid this issue by adding the style layers in -mapView:styleDidFinishLoading:. If that’s too early for your needs, you can get the style’s ordered list of layers in MGLStyle.layers, then insert your layers below any layers used for point or shape annotations. (They all have identifiers beginning with com.mapbox..) I think this would be a solution for @Jmaguerre as well.
Perfectly understood, thanks for the details of the explanation.