Files
MapLibreParticleLab/platform/android/docs/styling/live-realtime-data.md

1.6 KiB

Add live realtime data

{{ activity_source_note("RealTimeGeoJsonActivity.kt") }}

In this example you will learn how to add a live GeoJSON source. We have set up a lambda function that returns a new GeoJSON point every time it is called.

First we will create a GeoJSONSource.

--8<-- "MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/style/RealTimeGeoJsonActivity.kt:addSource"

Next we will create a SymbolLayer that uses the source.

--8<-- "MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/style/RealTimeGeoJsonActivity.kt:addLayer"

We use define a Runnable and use android.os.Handler with a android.os.Looper to update the GeoJSON source every 2 seconds.

--8<-- "MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/style/RealTimeGeoJsonActivity.kt:Runnable"

Bonus: set icon rotation

You can set the icon rotation of the icon when ever the point is updated based on the last two points.

--8<-- "MapLibreAndroidTestApp/src/main/java/org/maplibre/android/testapp/activity/style/RealTimeGeoJsonActivity.kt:setIconRotation"