在 Mapbox Android 中使用循环从 geojson 添加许多符号图标?

问题描述 投票:0回答:2

我有一张包含 500 个符号图标的地图。我想知道是否有一种方法可以使用函数或循环来添加这些标记,该函数或循环迭代我的 geojson 信息并获取所需的功能。就目前情况而言,我单独添加每个标记,这使得我的地图框活动有数千行长。一切都很完美,但我觉得如果可能的话清理代码会更好。这是我到目前为止所拥有的:

添加我的图标绘图我为每个图标单独执行此操作:

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 示例来使用它的方法。虽然它没有崩溃,但它也没有设置我的任何图标。这是我的loadMapStyle.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\"]
    )。
  • 停止输入必须是严格升序的数字文字。
  • 返回刚好小于输入的停止输出值,
  • 或者如果输入小于第一个停止点,则为第一个输入。
  • 使用示例:
  • {@代码
  • CircleLayer CircleLayer = new CircleLayer("图层 ID", "源 ID");
  • circleLayer.setProperties(
  • circleRadius(
    
  •     step(zoom(), literal(0.0f),
    
  •     literal(1.0f), literal(2.5f),
    
  •     literal(10.0f), literal(5.0f))
    
  • )
    
  • );
  • }
  • @param input 输入值
  • @param defaultOutput 默认输出表达式
  • @param 停止输入和输出值对
  • @返回表达式
  • @参见样式规范 */
android mapbox geojson
2个回答
0
投票

根据文档,您应该只需要提供 Stops 数组,而不是使用更改参数的表达式。你应该尝试这个吗:

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*/
      )
)


0
投票

如果您来这里是为了 Mapbox 版本 10,通过功能集合动态显示 自定义符号标记,那么您可以检查此 答案

© www.soinside.com 2019 - 2024. All rights reserved.