MapBox setProperties on LineLayer fails to resolve

Viewed 438

I'm using the Android Mapbox SDK to add a VectorSource to a MapboxMap and then I'm trying to add a LineLayer to the map also.

Currently using the version 5.1.3

This code is going to be in TypeScript because it's using the NativeScript framework which allows the consumption of native libraries and direct access to native android/iOS APIs.

// these constants are just simple ways to reference a class in the mapbox package

const PropertyFactory = com.mapbox.mapboxsdk.style.layers.PropertyFactory;
const Property = com.mapbox.mapboxsdk.style.layers.Property;
const LineLayer = com.mapbox.mapboxsdk.style.layers.LineLayer;
const VectorSource = com.mapbox.mapboxsdk.style.sources.VectorSource;
const lineCap = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineCap;
const lineColor = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineColor;
const lineJoin = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineJoin;
const lineWidth = com.mapbox.mapboxsdk.style.layers.PropertyFactory.lineWidth;
const LatLngBounds = com.mapbox.mapboxsdk.geometry.LatLngBounds;
const FillLayer = com.mapbox.mapboxsdk.style.layers.FillLayer;
const SymbolLayer = com.mapbox.mapboxsdk.style.layers.SymbolLayer;
const CircleLayer = com.mapbox.mapboxsdk.style.layers.CircleLayer;


 // set the map GLSource (vector) to the mapbox map
    const vectorSource = new VectorSource(
      layer.ID.toString(),
      `http://themaptiles.cloudapp.net/data/${layer.GLSource}.json`
    );
    this._mapboxMap.addSource(vectorSource);

  let newLayer;
    if (layer.Type == "line") {
      console.log(`*** creating new LineLayer ***`);
      newLayer = new LineLayer("line-layer", layer.ID.toString());

      // get the line color for this style
      const lColor = style["line"]["line-color"];
      const androidColor = new Color(lColor).android; // ends up valid and something like -1293839 for android to use

      newLayer.setSourceLayer(layer.ID.toString());

      // will throw here with `Failed resolving method setProperties on class com.mapbox.mapboxsdk.style.layers.Layer`
      newLayer.setProperties(
        PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
        PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
        PropertyFactory.lineColor(androidColor),
        PropertyFactory.lineWidth(new java.lang.Float(2))
      );
    }


this._mapboxMap.addLayer(newLayer);

If I don't attempt to use setProperties method then the code executes fine but there is no visible line on the map after the layer is added.

1 Answers

One of the many Java <-> JavaScript incompatibilities, and challenges in NativeScript - the variadic functions.

Looking at mapbox's official API ref the Layer.setProperties method takes a list of arguments: https://www.mapbox.com/android-docs/api/map-sdk/5.2.0-beta.3/com/mapbox/mapboxsdk/style/layers/Layer.html#setProperties-com.mapbox.mapboxsdk.style.layers.PropertyValue...-

However, arguments meant for the JNI -> Java call to Layer.setProperties cannot be easily extracted from a function call in JavaScript, so instead you should wrap the arguments in an array.

  newLayer.setProperties([
    PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
    PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
    PropertyFactory.lineColor(androidColor),
    PropertyFactory.lineWidth(new java.lang.Float(2))
  ]);
Related