Search code examples
nativescriptmapboxmapbox-android

How to get a geometry from a Mapbox GL Native GeoJSONSource?


I am using the Mapbox GL Native Android SDK v 7.1.2 in a NativeScript {N} app.

I have a line drawn on a map using a GeoJSON source.

I would like to retrieve the geometry from the line at a later point in the code (as opposed to keeping the coordinates lying around separately).

I am able to get a reference to the source of the line using Style.getSource(id) however I can't find an example or explanation how to correctly form an Expression filter for querySourceFeatures() to get the geometry of the first feature from the list.

Not including a parameter to querySourceFeatures() causes NativeScript not to be able to resolve the call but passing null to it returns an empty list.

How can I form an Expression filter to return the geometry of the first feature from a GeoJSON source?


Solution

  • It turns out that in the Android Mapbox GL Native SDK you cannot immediately query a source for it's geometry after you have added it.

    To get all features from a source you can use:

    source.querySourceFeatures( com.mapbox.mapboxsdk.style.expressions.Expression.literal(true));
    

    however, you have to give the map a chance to update the source before calling this method. In my test I just did a:

    setTimeout( () => {
      ....
    }, 500 );
    

    In production you'd probably set a callback onDidBecomeIdleListener.

    Sadly, I noticed that the source values are modified after being added to the map. What I query from the source does not exactly match what I put in, so this approach turned out not to be useful for me.