如何解决使用 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))
也没有用
根据@Sergio 链接到的 mapBox 示例,这是我尝试使用它的方式。虽然它没有崩溃,但它也没有设置我的任何图标。这是我的loadedMapStyle.addLayer 代码:
loadedMapStyle.addLayer(
SymbolLayer("route-location-layer-id",stop(literal(getLoadingSymbolStops()),getLoadingSymbolStops()),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,这是我在停止时需要的,并且我还显示该函数正在返回 stop.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 input 输入值
- @param defaultOutput 默认输出表达式
- @param 停止输入和输出值对
- @return 表达式
- @see Style specification */
解决方法
根据文档,你应该只需要提供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*/
)
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。