React-native-mapbox-gl: Move layers to the front/back

Created on 25 Jan 2018  路  4Comments  路  Source: nitaliano/react-native-mapbox-gl

Hey,

Is it somehow possible the reorder/sort multiple ShapeSource components inside a MapView?
I'm trying to sort the ShapeSource components based on a type but it does not reorder the layers on the map.

renderFeatures(layers) {
   const types = {
     symbol: 0,
     line: 1,
     fill: 2,
   };

  return layers.sort((prev, next) => {

    const nextType = types[next.type];
    const prevType = types[prev.type];

    if (prevType > nextType) {
      return 1;
    }

    if (nextType > prevType) {
      return -1;
    }
    return 0;
  }).map((layer) => {

    return (
      <MapboxGL.ShapeSource>
            ... 
      </MapboxGL.ShapeSource>
    );
  });
}

render() {
  return (
    <MapboxGL.MapView>
    {this.renderFeatures(layers)}           
     </MapboxGL.MapView>
  )
}

Most helpful comment

@nitaliano when I use belowLayerID with 1 of 10 reloads I get
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'A style layer cannot be placed below (null) in the style.
I guess the order during adding them is not guaranteed and the below layer is not yet available.
Whats the best way to guarantee the order?

All 4 comments

Nope, the best you can do is use layerIndex, belowLayerID, and aboveLayerID to handle this for you. In the future we might add a special source to the react native sdk that would handle this under the hood but for now what I mentioned is your best bet.

@nitaliano when I use belowLayerID with 1 of 10 reloads I get
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'A style layer cannot be placed below (null) in the style.
I guess the order during adding them is not guaranteed and the below layer is not yet available.
Whats the best way to guarantee the order?

Yes to new users of this library this is very unclear, it doesn't seem trivial to order layers.

Same issue from me. Tried aboveLayerID``belowLayerID and layerIndex but it doesn't work on some reload

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lernerbot picture lernerbot  路  3Comments

vyankat70war picture vyankat70war  路  3Comments

madroneropaulo picture madroneropaulo  路  4Comments

VentsislavDinev picture VentsislavDinev  路  3Comments

Gp2mv3 picture Gp2mv3  路  3Comments