Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update existing features in a GeoJSON style source not working #2517

Open
SigmaAppdevelopment opened this issue Nov 14, 2024 · 3 comments
Open
Labels
bug 🪲 Something isn't working

Comments

@SigmaAppdevelopment
Copy link

SigmaAppdevelopment commented Nov 14, 2024

Environment

  • Android OS version: 34
  • Maps SDK Version: 11.5.1

Observed behavior and steps to reproduce

Hi, we have a symbollayer with geoJsonSource. We want to update position (geometry) of this layer every second. So we add geometry-feature with addGeoJsonSourceFeatures and update with updateGeoJsonSourceFeatures. See code below.

fun initMarker() {
	val markerSource = geoJsonSource("currentMarkerSource")
	markerSource.addGeoJSONSourceFeatures(
   		listOf(
    		 Feature.fromGeometry(
         		Point.fromLngLat(0.0, 0.0),
         	   	null,
         	   	"geoId"
     		 ),
   		)
	)
	style.addSource(markerSource)
}
fun updateMarker(point: Point) {
	val feature = Feature.fromGeometry(point, null, "geoId")
	val locationSource = style.getSource("currentMarkerSource") as? GeoJsonSource
	locationSource?.updateGeoJSONSourceFeatures(features)
}

Mapbox returns error:
Error: [maps-core]: {}[Style]: Failed to load source currentMarkerSource: Cannot create new geojson impl for Update/Remove feature operations

What are we missing? Also tried to set dataId on addGeoJsonSourceFeature and updateGeoJsonSourceFeature, but didn't help. Couldn't find an example.

@SigmaAppdevelopment SigmaAppdevelopment added the bug 🪲 Something isn't working label Nov 14, 2024
@iolandarosavoid
Copy link

iolandarosavoid commented Nov 19, 2024

Hello.

I also don't understand why your code does not work, but maybe an alternative solution might be helpful. It is very similar to what you have implemented but using the FeatureCollection instead of the feature array directly.

fun addOrUpdateSource(style: Style, collection: FeatureCollection, sourceId: String) {
        val source = style.getSource(sourceId) as? GeoJsonSource?
        if (source == null) {
            style.addSource(
                geoJsonSource(sourceId).apply {
                    featureCollection(collection)
                },
            )
        } else {
            source.featureCollection(collection)
        }
    }

To get the FeatureCollection you just use the method FeatureCollection.fromFeatures(<your_features_array>) and this way it should be fine.

Anyway I am finding some weird bugs on source data update specially using the compose version of mapbox. I always have to use old methods because updates of GeoJSONData in compose source are very buggy if they are frequent. I am unsure how can I improve this and the fact that there are little documentation and examples for compose is really bad for the ones trying to use this library

@SigmaAppdevelopment
Copy link
Author

Hello,

yes, what you suggest works. But we wanted to use new api because it should be much more performant...

@iolandarosavoid
Copy link

Probably you are already doing this but you can always check for the error in mapLoadingError event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🪲 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants