在Mapbox Android中使用循环从Geojson添加许多符号图标? [英] Adding many symbol icons from geojson using a loop in Mapbox Android?

查看:176
本文介绍了在Mapbox Android中使用循环从Geojson添加许多符号图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张带有500个符号图标的地图。我想知道是否有一种方法可以使用函数或循环来添加这些标记,该函数或循环可以遍历我的Geojson信息并获取所需的功能。现在,我正在单独添加每个标记,这使得我的MapBox活动有几千行之长。这一切都工作得很好,但我觉得如果可能的话,清理一下代码会很好。以下是我到目前为止的情况:

添加图标可绘制项时,我会分别对每个图标执行此操作:

loadedMapStyle.addImage(
        Constants.ICON_ID_1_GREEN, BitmapFactory.decodeResource(
            this.resources, R.drawable.green_icon_1
        )
    )

有没有办法通过for循环或其他方式添加所有这些资源?

然后通过SymbolLayer和匹配表达式添加每个图标:

loadedMapStyle.addLayer(
        SymbolLayer("route-location-layer-id", Constants.SOURCE_ID)
            .withProperties(
                iconImage(
                    match(
                        get("icon_property"), literal(Constants.ICON_ID_1_GREEN),
                        stop(literal(Constants.ICON_ID_41_GREEN), Constants.ICON_ID_41_GREEN),
                        stop(literal(Constants.ICON_ID_40_GREEN), Constants.ICON_ID_40_GREEN),
                        stop(literal(Constants.ICON_ID_39_GREEN), Constants.ICON_ID_39_GREEN),
                        stop(literal(Constants.ICON_ID_38_GREEN), Constants.ICON_ID_38_GREEN),
                        stop(literal(Constants.ICON_ID_37_GREEN), Constants.ICON_ID_37_GREEN),
                        stop(literal(Constants.ICON_ID_36_GREEN), Constants.ICON_ID_36_GREEN),

                        ),
                ),
                iconOpacity(0.0f),
                textField(get(Constants.ICON_NAME)),
                textColor(Color.WHITE),
                textVariableAnchor(
                    arrayOf(TEXT_ANCHOR_TOP)
                ),
                textJustify(TEXT_JUSTIFY_AUTO),
                textRadialOffset(.5f),
                textAllowOverlap(true),
                textSizePropertyValue,
                iconAllowOverlap(true),
                iconOffset(arrayOf(0f, -16.5f)),
                iconSize(
                    match(
                        toString(get(Constants.PROPERTY_SELECTED)),
                        literal(1.0f),
                        stop("true", 2.0f),
                        stop("false", 1.0)
                    )
                )
            )

为了这篇文章,我删掉了很多上面的代码,因为它是每个图标的重复代码。

我使用fromJson()方法将GeoJSON文件转换为可用的FeatureCollection对象

@get:Throws(IOException::class)
private val featureCollectionFromJson: Unit
    get() {
        try {
            // Use fromJson() method to convert the GeoJSON file into a usable FeatureCollection object
            featureCollection = "list_of_climbing_routes.geojson".loadGeoJsonFromAsset()?.let {
                FeatureCollection.fromJson(
                    it
                )
            }
        } catch (exception: Exception) {
            Log.e("MapBoxActivity", "getFeatureCollectionFromJson: $exception")
        }
    }

最后是我的Geojson代码片段。此时,我正在访问Geojson的坐标和ICON_PROPERTY属性:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "id":"1",
        "name":"Bump That",
        "climbingRouteNum":"1",
        "climbingRouteVRating":3,
        "climbingRouteBoulder":"Vandala Boulder",
        "area":"Front Area",
        "image_url":"https://routes/13/vandala_1_4.jpg",
        "ratingImageUrl":"https://routes/13/onestar.png",
        "climbingRouteBeta":"Another great squeeze problem. Stand start, surf to high sloping sidepulls, then squeeze your way continuously to better holds.",
        "vRating":"V3",
        "climbingRouteTickbox":"false",
        "selected":"false",
        "icon_property":"ICON_ID_1_GREEN"
      },
      "geometry": {
        "type": "Point",
       "coordinates": [
         -86.30726135484986,
         33.92127026676458
        ]
      }
    },

如果需要,我可以发布更多代码。我以为这会是个好的开始。谢谢!!

好的,所以我想知道我是不是在正确的轨道上。我不知道如何让它与停止一起工作,但是我能够使用这个循环函数来获取一些信息。我只是不确定如何在我的匹配表达式中应用它。以下是我的函数:

        private fun getLoadingSymbolStops(): Array<Stop?> {
    val stops: MutableList<Stop> = ArrayList()
    val featureList: List<Feature> = featureCollection!!.features() as List<Feature>

    for (x in featureList.indices) {
        val iconProperty = featureList[x].getStringProperty("icon_property")

        stops.add(stop(x, x))

                Log.d("tag", "${stops.toTypedArray()}")
                Log.d("tag", iconProperty)
    }
    return stops.toTypedArray()
}

//还尝试:

stops.add(stop(x, iconProperty))

//还尝试:

stops.add(stop(iconProperty, iconProperty))

也不起作用

根据@sergio链接到的Mapbox示例,以下是我尝试使用它的方式。虽然它没有崩溃,但它也没有设置我的任何图标。下面是我的loadedMapStyle.addLayer代码:

        loadedMapStyle.addLayer(
        SymbolLayer("route-location-layer-id", Constants.SOURCE_ID)
            .withProperties(
                iconImage(
                    match(
                        get("icon_property"), literal(Constants.ICON_ID_1_GREEN),
                        stop(literal(getLoadingSymbolStops()), getLoadingSymbolStops()),

                        ),
                ),
                iconOpacity(0.0f),
                textField(get(Constants.ICON_NAME)),
                textColor(Color.WHITE),
                textVariableAnchor(
                    arrayOf(TEXT_ANCHOR_TOP)
                ),
                textJustify(TEXT_JUSTIFY_AUTO),
                textRadialOffset(.5f),
                textAllowOverlap(true),
                textSizePropertyValue,
                iconAllowOverlap(true),
                iconOffset(arrayOf(0f, -16.5f)),
                iconSize(
                    match(
                        toString(get(Constants.PROPERTY_SELECTED)),
                        literal(1.0f),
                        stop("true", 2.0f),
                        stop("false", 1.0)
                    )
                )
            )
    )

以下是我的logcat中的一段代码:

D/tag: [Lcom.mapbox.mapboxsdk.style.expressions.Expression$Stop;@6dad284
D/tag: ICON_ID_1_GREEN
D/tag: [Lcom.mapbox.mapboxsdk.style.expressions.Expression$Stop;@d680a6d
D/tag: ICON_ID_2_YELLOW
D/tag: [Lcom.mapbox.mapboxsdk.style.expressions.Expression$Stop;@5c7d6a2
D/tag: ICON_ID_3_YELLOW

等.

我的日志显示我正在循环遍历停止所需的所有ICON_PROPERTIES,并且我还显示函数正在返回stops.toTypedArray。我从这里到哪里去呢?有人吗?谢谢!

以下是从Expressions.java停止的代码:

  public static Stop stop(@NonNull Object stop, @NonNull Object value) {
return new Stop(stop, value);

}

/**

  • 通过计算由
  • 对定义的分段常量函数来生成离散的分步结果
  • 输入值和输出值(";停止";)。input可以是任何数字表达式(例如,["get", "population"])。
  • 停止输入必须是严格升序的数字文字。
  • 返回恰好小于输入的停靠点的输出值
  • 如果输入少于第一个停靠点,则为第一个输入。
  • 示例用法:
  • {@code
  • CircleLayer circleLayer = new CircleLayer("layer-id", "source-id");
  • circleLayer.setProperties(
  • circleRadius(
    
  •     step(zoom(), literal(0.0f),
    
  •     literal(1.0f), literal(2.5f),
    
  •     literal(10.0f), literal(5.0f))
    
  • )
    
  • );
  • }
  • @param输入输入值
  • @param defaultOutput默认输出表达式
  • @param停止输入输出值对
  • @返回表达式
  • @请参见Style specification */

推荐答案

根据文档,您应该只需要提供停止数组,而不需要使用更改参数的表达式。您是否应该尝试此操作:

loadedMapStyle.addLayer(
  SymbolLayer("route-location-layer-id", Constants.SOURCE_ID)
      .withProperties(
          iconImage(
             match(
                get("icon_property"),
                literal(Constants.ICON_ID_1_GREEN),
                // Instead of stop() based on array of stops, just add stops' array
                *getLoadingSymbolStops()
             ),
          ),
          /* Remaining Properties*/
      )
)

这篇关于在Mapbox Android中使用循环从Geojson添加许多符号图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆