单击时的an​​droid mapbox选择功能

时间:2018-09-24 12:00:59

标签: android kotlin mapbox mapbox-android

我正在使用带有GeoJsonSource和symbollayer的mapbox。用户单击功能时,应更改颜色。我使用以下代码来处理此逻辑,并且可以正常工作,但是它太慢了,并且需要几秒钟来更改图标颜色。

在这里配置符号层,为'PROPERTY_SELECTED'添加图标changelogic:

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

在地图上单击要素对象已更新:

 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()
                }
            }
        }
    }
}

并刷新源:

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

在调用“ refreshSource”之后,图标更新需要花费一些时间。就我而言,有2050个要素是来源。有没有更好的方法来实现它?或以任何方式优化此解决方案?

1 个答案:

答案 0 :(得分:0)

这是来自github答案的第二种更快的方法:

 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

您可以在那里看到整个问题

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