Implemented synchronous update for GeoJSON source (#3968)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Alex Cristici
2025-12-03 01:02:29 +02:00
committed by GitHub
parent f25a4581fe
commit 29d70ef0bb
32 changed files with 408 additions and 116 deletions

View File

@@ -23,10 +23,15 @@ class LayerSourceProvider {
@NonNull
GeoJsonSource generateSource(Feature locationFeature) {
return generateSource(locationFeature, false);
}
@NonNull
GeoJsonSource generateSource(Feature locationFeature, Boolean synchronousUpdate) {
return new GeoJsonSource(
LocationComponentConstants.LOCATION_SOURCE,
locationFeature,
new GeoJsonOptions().withMaxZoom(16)
new GeoJsonOptions().withMaxZoom(16).withSynchronousUpdate(synchronousUpdate)
);
}

View File

@@ -318,7 +318,7 @@ final class SymbolLocationLayerRenderer implements LocationLayerRenderer {
}
private void addLocationSource() {
locationSource = layerSourceProvider.generateSource(locationFeature);
locationSource = layerSourceProvider.generateSource(locationFeature, true);
style.addSource(locationSource);
}

View File

@@ -131,4 +131,15 @@ class GeoJsonOptions : HashMap<String?, Any?>() {
this["clusterProperties"] = properties
return this
}
/**
* Set a flag defining whether or not the fetched tiles for the given source
* should be synchronously updated on the render thread
*
* @param value current setting for synchronous. Defaults to false.
*/
fun withSynchronousUpdate(value: Boolean): GeoJsonOptions {
this["synchronousUpdate"] = value
return this
}
}

View File

@@ -228,36 +228,49 @@ class GeoJsonSource : Source {
}
/**
* Updates the GeoJson with a single feature. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
* Updates the GeoJson with a single feature.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param feature the GeoJSON [Feature] to set
*/
fun setGeoJson(feature: Feature?) {
if (detached) {
return
}
checkThread()
nativeSetFeature(feature)
if (nativeIsUpdateSynchronous()) {
nativeSetFeatureSync(feature)
} else {
nativeSetFeature(feature)
}
}
/**
* Updates the GeoJson with a single geometry. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
* Updates the GeoJson with a single geometry.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param geometry the GeoJSON [Geometry] to set
*/
fun setGeoJson(geometry: Geometry?) {
if (detached) {
return
}
checkThread()
nativeSetGeometry(geometry)
if (nativeIsUpdateSynchronous()) {
nativeSetGeometrySync(geometry)
} else {
nativeSetGeometry(geometry)
}
}
/**
* Updates the GeoJson. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
* Updates the GeoJson.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param featureCollection the GeoJSON FeatureCollection
*/
@@ -266,15 +279,37 @@ class GeoJsonSource : Source {
return
}
checkThread()
var featureCollection = featureCollection
if (featureCollection != null && featureCollection.features() != null) {
val features = featureCollection.features()
val featuresCopy: List<Feature> = ArrayList(features)
nativeSetFeatureCollection(FeatureCollection.fromFeatures(featuresCopy))
featureCollection = FeatureCollection.fromFeatures(featuresCopy)
}
if (nativeIsUpdateSynchronous()) {
nativeSetFeatureCollectionSync(featureCollection)
} else {
nativeSetFeatureCollection(featureCollection)
}
}
/**
* Updates the GeoJson.
* The update is performed synchronously or asynchronously, based on the source synchronous update flag.
* In the case of asynchronous updates, the data won't be immediately visible or available to query when this method returns.
*
* @param json the raw GeoJson FeatureCollection string
*/
fun setGeoJson(json: String) {
if (detached) {
return
}
checkThread()
if (nativeIsUpdateSynchronous()) {
nativeSetGeoJsonStringSync(json)
} else {
nativeSetGeoJsonString(json)
}
}
/**
* Updates the GeoJson with a single feature. The update is performed synchronously,
@@ -282,6 +317,7 @@ class GeoJsonSource : Source {
*
* @param feature the GeoJSON [Feature] to set
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(feature: Feature?) {
if (detached) {
return
@@ -296,6 +332,7 @@ class GeoJsonSource : Source {
*
* @param geometry the GeoJSON [Geometry] to set
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(geometry: Geometry?) {
if (detached) {
return
@@ -310,6 +347,7 @@ class GeoJsonSource : Source {
*
* @param featureCollection the GeoJSON FeatureCollection
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(featureCollection: FeatureCollection?) {
if (detached) {
return
@@ -324,26 +362,13 @@ class GeoJsonSource : Source {
}
}
/**
* Updates the GeoJson. The update is performed asynchronously,
* so the data won't be immediately visible or available to query when this method returns.
*
* @param json the raw GeoJson FeatureCollection string
*/
fun setGeoJson(json: String) {
if (detached) {
return
}
checkThread()
nativeSetGeoJsonString(json)
}
/**
* Updates the GeoJson. The update is performed synchronously,
* so the data will be immediately visible and available to query when this method returns.
*
* @param json the raw GeoJson FeatureCollection string
*/
@Deprecated("use {@link GeoJsonOptions#withSynchronousUpdate(Boolean)} to enable synchronous updates on construction")
fun setGeoJsonSync(json: String) {
if (detached) {
return
@@ -567,6 +592,9 @@ class GeoJsonSource : Source {
@Keep
private external fun nativeGetClusterExpansionZoom(feature: Feature): Int
@Keep
private external fun nativeIsUpdateSynchronous(): Boolean
@Keep
@Throws(Throwable::class)
protected external fun finalize()