1
votes

I'm using mapbox, with GeoJsonSource and symbollayer. When user clicks on feature it should change a color. I handle this logic with following code and it works, but it is too slow and takes several second to change icon color.

Here I configure symbol layer, add icon changelogic for 'PROPERTY_SELECTED':

mapBoxMap?.addLayer(SymbolLayer(markerStyleLayerIdentifier, markerSourceIdentifier)
                .withProperties(
                        PropertyFactory.iconImage(markerImage),
                        PropertyFactory.iconAllowOverlap(false),
                        PropertyFactory.iconImage(match(
                                get(PROPERTY_SELECTED), literal(0),
                                literal(markerImage),
                                literal(markerImageSelected)
                        ))
                ))

on map click features objects are update:

 override fun onMapClick(point: LatLng) {
    val screenPoint = mapBoxMap?.projection?.toScreenLocation(point)
    var features = mapBoxMap?.queryRenderedFeatures(screenPoint
            ?: return, markerStyleLayerIdentifier)

    if ((features ?: return).isNotEmpty()) {
        var feature = features[0]
        showMarkerInfo(feature)
        doAsync {
            var featureList = featureCollection?.features()

            var id = feature.getNumberProperty(PROPERTY_STOP_ID)

            if (featureList != null) {
                for (i in 0 until featureList.size) {

                    var fId = featureList[i].getNumberProperty(PROPERTY_STOP_ID)

                    if (fId == id) {
                        featureList[i].properties()?.addProperty(PROPERTY_SELECTED, 1)
                    } else {
                        featureList[i].properties()?.addProperty(PROPERTY_SELECTED, 0)
                    }
                }

                uiThread {
                    refreshSource()
                }
            }
        }
    }
}

and refresh source :

private fun refreshSource() {
    var source = mapBoxMap?.getSource(markerSourceIdentifier) as GeoJsonSource?
    if (source != null && featureCollection != null) {
        source.setGeoJson(featureCollection)
    }
}

after 'refreshSource' is called , it takes several time before icon update. In my case there are 2050 features is source. Is there any better way to implement it ? Or any way to optimise this solution ?

1
How can i create Expression if "SELECED" field is true use A color for fill and else use B color in properties?Mahdi

1 Answers

0
votes

here is a second , faster way from github answer:

 var selectedLayer = mapBoxMap?.getLayer(markerSelectedStyleLayerIdentifier) as SymbolLayer?
            var id = feature.getNumberProperty(PROPERTY_STOP_ID)
            var selectedExpression = any(
                    eq(get(PROPERTY_STOP_ID), literal(id.toString()))
            )
            selectedLayer?.filter = selectedExpression

you can see whole issue there

https://github.com/mapbox/mapbox-java/issues/892