H.Karatsanov
H.Karatsanov

Reputation: 319

Mapbox: Do we need LineLayer if we want to display a Line on the map

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?

Upvotes: 3

Views: 2243

Answers (1)

langsmith
langsmith

Reputation: 2546

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:

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

enter image description here

Create a new Line each time you want to update

Upvotes: 2

Related Questions