Search code examples
androidmapboxmapbox-android

MapBox Adding and Plotting Many Polylines Efficiently?


I have a dataset of polylines, markers and polygons stored as lat/lon data in a CSV file that need plotting in Android MapBox. I'm able to do this easily using mapboxmap.addPolyline, addMarker and addpolygons, but after adding hundreds of points scrolling the map and viewing the data on an Android phone becomes very slow.

Am I going about this wrongly? Is there a more efficient way to plot many data points that i'm supposed to be using?


Solution

  • What version of the Maps SDK are you using? Your use of addPolyline/addMarker/etc. makes me think that you're using a very outdated version. Please try using 8.5.0 and using sources/layers: https://docs.mapbox.com/android/maps/overview/data-driven-styling/

    In general, you should use the lat/lon data in the CSV file to create Feature objects. Use them to create a FeatureCollection and then add/style layers.

    Setting up a LineLayer

    mapboxMap.setStyle(Style.MAPBOX_STREETS, new Style.OnStyleLoaded() {
              @Override
              public void onStyleLoaded(@NonNull Style style) {
    
                Feature polygonFeature = Feature.fromGeometry(Polygon.fromLngLats());
                Feature lineStringFeature = Feature.fromGeometry(LineString.fromLngLats());
                Feature pointFeature = Feature.fromGeometry(Point.fromLngLat());
    
                FeatureCollection featureCollection = FeatureCollection.fromFeatures(new Feature[]{
                    pointPolygon, pointLineString, pointFeature
                });
    
                GeoJsonSource geoJsonSource = new GeoJsonSource("source-id", featureCollection);
    
                style.addSource(geoJsonSource);
    
                LineLayer lineLayer = new LineLayer("line-layer-id", "source-id");
                lineLayer.setProperties(
                    PropertyFactory.lineColor(Color.BLUE)
                );
                style.addLayer(lineLayer);
    
              }
            });