You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mapbox Android中高亮国家无效果,请求排查代码问题

Mapbox Android高亮国家代码问题修复

你的代码里有三个关键问题导致地图无变化:

  • Source ID不匹配:创建的VectorSource ID是countries,但添加FillLayer时指定的source ID是country-source,两者必须完全一致。
  • FillOpacity取值错误:透明度的有效范围是0(完全透明)到1(完全不透明),你写的4.5超出范围会被忽略,改成0.45这类合理值即可。
  • 缺少SourceLayer指定:Mapbox国家边界矢量源mapbox://mapbox.country-boundaries-v1内部的图层名称是country_boundaries,必须在FillLayer中指定该source layer才能获取到边界数据。

修复后的代码如下:

mapboxMap.loadStyleUri(Style.TRAFFIC_NIGHT) { style ->

    style.addSource(
        VectorSource.Builder("countries").apply {
            url("mapbox://mapbox.country-boundaries-v1")
        }.build()
    )
    style.addLayer(
        fillLayer("country-layer", "countries") { // 修正source ID
            fillColor(Color.parseColor("#4469f7"))
            fillOpacity(0.45) // 修正透明度取值
            sourceLayer("country_boundaries") // 添加source layer指定
            filter(
                eq {
                    literal("iso_3166_1_alpha_3")
                    literal("IND")
                }
            )
        }
    )
}

内容的提问来源于stack exchange,提问作者Ansal Rabin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 17:44:56