I'm reading the documentation of the Annotation Plugin along with the examples, but still can't quite understand how to work with Lines.
I want to create a Line, which need to be updated frequently with additional coordinates. In Google Maps is easy, since we declare a Polyline object, take the LatLng points and set the new one. Basically, I'm not going to use a lot of customization, only width and color of the line.
So do we need to define a layer, or we could just use the options for this case?
And a second question: (From the documentation, I see a lot of examples with FeatureCollection and GeoJSON) Is this the only way of updating the line?
Tldr; There are two main options and it's basically up to you as to which route to take. Your LatLng coordinates can be used to create a LineString/Line each time you want to update the visual line.
You can use a LineLayer:
https://docs.mapbox.com/android/maps/examples/draw-a-geojson-line/
https://docs.mapbox.com/android/maps/examples/create-a-line-layer (in this example,
style.addSource(new GeoJsonSource("line-source", FeatureCollection.fromFeatures(new Feature[] {Feature.fromGeometry( LineString.fromLngLats(routeCoordinates) )})));
can actually just be style.addSource(new GeoJsonSource("line-source", LineString.fromLngLats(routeCoordinates))); .
Updating GeoJSON of the source that's being used by the LineLayer. Create a new LineString each time you want to update:
See second code block in the GeoJSON updates section (source.setGeoJson(), etc.): https://docs.mapbox.com/android/java/overview/geojson/#geojson-updates
You can use a LineManager in the Annotation Plugin:
https://github.com/mapbox/mapbox-plugins-android/blob/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/LineActivity.java#L48
https://github.com/mapbox/mapbox-plugins-android/blob/master/app/src/main/java/com/mapbox/mapboxsdk/plugins/testapp/activity/annotation/LineActivity.java#L64-L73
Updating the line via LineManager

Create a new Line each time you want to update
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With