1

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");
    }
});
1

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:

private static FeatureCollection getFeatureCollection(List<LatLng> positions) {
    StringBuilder jsonBuilder = new StringBuilder();
    jsonBuilder.append("{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"geometry\":{\"type\":\"LineString\",\"coordinates\":[");
    ListIterator<LatLng> listIterator = positions.listIterator();
    while (listIterator.hasNext()) {
        LatLng position = listIterator.next();
        jsonBuilder.append('[');
        jsonBuilder.append(position.longitude);
        jsonBuilder.append(',');
        jsonBuilder.append(position.latitude);
        jsonBuilder.append(']');
        if (listIterator.hasNext()) {
            jsonBuilder.append(',');
        }
    }
    jsonBuilder.append("]},\"properties\":{}}]}");
    return BaseFeatureCollection.fromJson(jsonBuilder.toString());
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.