I am trying to add new geoJson polyline layer to mapbox in Android Studio. It works fine if I am adding it using the default style of mapbox, but if I set my own styles, the line is not drawn anymore in my app. Could anyone tell me what could be wrong?
mapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
// Create the LineString from the list of coordinates and then make a GeoJSON
// FeatureCollection so we can add the line to our map as a layer.
if ("city".equals(spinner2)) {
mapboxMap.setStyleUrl("mapbox://styles/johan88/cj1fdzcoo00012spgu1lldduk");
}
else if ("beach".equals(spinner2)) {
mapboxMap.setStyleUrl("mapbox://styles/johan88/cj3cuj7a800032rmqsrhir1av");
}
else if ("adventure".equals(spinner2)) {
mapboxMap.setStyleUrl("mapbox://styles/johan88/cj37g9zzz00092rqp7c55ufxt");
// Customize map with markers, polylines, etc.
}
LineString lineString = LineString.fromCoordinates(routeCoordinates);
FeatureCollection featureCollection =
FeatureCollection.fromFeatures(new Feature[]{Feature.fromGeometry(lineString)});
Source geoJsonSource = new GeoJsonSource("line-source", featureCollection);
mapboxMap.addSource(geoJsonSource);
LineLayer lineLayer = new LineLayer("linelayer", "line-source");
// The layer properties for our line. This is where we make the line dotted, set the
// color, etc.
lineLayer.setProperties(
PropertyFactory.lineDasharray(new Float[]{0.01f, 2f}),
PropertyFactory.lineCap(Property.LINE_CAP_ROUND),
PropertyFactory.lineJoin(Property.LINE_JOIN_ROUND),
PropertyFactory.lineWidth(5f),
PropertyFactory.lineColor(Color.parseColor("#e55e5e"))
);
mapboxMap.addLayerBelow(lineLayer, "Water labels");
}
});
Best Answer
I was also not able to get Mapbox (5.5.0) to draw a LineLayer, until I built the FeatureCollection from a JSON string.
I was inspired to try this by this old bug report: https://github.com/mapbox/mapbox-gl-native/issues/6240
Try using this function to build your FeatureCollection: