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

如何在Mapbox Android SDK中用Java添加GeoJsonSource?转换问题求助

在新版Mapbox Android SDK中实现Java版GeoJson多边形添加(替代内部工具类)

问题核心

将Kotlin版本的Mapbox GeoJson多边形添加逻辑转译为Java时,依赖了SDK内部的SourceUtils和LayerUtils工具类,这些类在新版SDK中不对外暴露,导致编译失败,需改用Style类的公开API实现。

修正后的完整Java代码

import android.util.Log;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.maps.MapView;
import com.mapbox.maps.Style;
import com.mapbox.maps.extension.style.layers.generated.FillLayer;
import com.mapbox.maps.extension.style.layers.generated.SymbolLayer;
import com.mapbox.maps.extension.style.sources.GeoJsonSource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class MapboxPolygonHelper {
    private MapView mapView;

    public MapboxPolygonHelper(MapView mapView) {
        this.mapView = mapView;
    }

    public void addGeoJsonPolygonToMap(@NotNull GeoPoint[] geoPoints, @NotNull String id, @NotNull String fillColor, double opacity, double fontSize) {
        // 验证参数非空
        if (geoPoints == null || id == null || fillColor == null) {
            throw new IllegalArgumentException("Required parameters cannot be null");
        }

        // 转换GeoPoint为Mapbox Point列表
        List<Point> pointList = new ArrayList<>();
        for (GeoPoint p : geoPoints) {
            Point currentPoint = Point.fromLngLat(p.getLongitude(), p.getLatitude());
            pointList.add(currentPoint);
        }

        // 创建GeoJsonSource
        Polygon polygon = Polygon.fromLngLats(Collections.singletonList(pointList));
        GeoJsonSource geoJsonSource = new GeoJsonSource.Builder(id)
                .geometry(polygon)
                .build();

        // 添加Source到Style
        Style style = mapView.getMapboxMap().getStyle();
        if (style != null) {
            style.addSource(geoJsonSource);
        }

        // 创建并配置填充图层
        FillLayer polygonLayer = new FillLayer(id + "FillLayer_ID", id);
        polygonLayer.fillColor(fillColor);
        polygonLayer.fillOpacity(opacity);

        // 创建并配置文字标注图层
        SymbolLayer symbolLayer = new SymbolLayer(id + "SymbolLAYER_ID", id);
        symbolLayer.textField(id);
        symbolLayer.textSize(fontSize);
        symbolLayer.textColor(fillColor);
        symbolLayer.textOpacity(1.0);

        // 添加图层到Style
        try {
            if (style != null) {
                style.addPersistentLayer(polygonLayer);
                style.addPersistentLayer(symbolLayer);
            }
        } catch (Exception ex) {
            Log.d("removeAnnotation", ex.getMessage() != null ? ex.getMessage() : "Unknown error");
        }
    }

    // 假设的GeoPoint类(根据实际项目调整)
    public static class GeoPoint {
        private double longitude;
        private double latitude;

        public GeoPoint(double longitude, double latitude) {
            this.longitude = longitude;
            this.latitude = latitude;
        }

        public double getLongitude() {
            return longitude;
        }

        public double getLatitude() {
            return latitude;
        }
    }
}

关键修改说明

  1. 替换Source添加逻辑

    • 移除SourceUtils.addSource,直接调用Style.addSource(geoJsonSource),这是SDK对外公开的标准方法,无需类型强制转换。
  2. 替换图层添加逻辑

    • 移除LayerUtils.addPersistentLayer$default,直接调用Style.addPersistentLayer(polygonLayer)和Style.addPersistentLayer(symbolLayer),这是添加持久化图层的公开API。
  3. 修正GeoJsonSource构建方式

    • 用Java标准的Collections.singletonList(pointList)替代Kotlin的CollectionsKt.listOf(mutableList),避免依赖Kotlin运行时工具类。
    • 直接调用GeoJsonSource.Builder.geometry(polygon),替代字节码生成的Builder.geometry$default方法,符合Java代码编写规范。
  4. 参数验证优化

    • 添加显式的非空参数检查,替代Intrinsics相关方法(若项目未引入Kotlin标准库,Intrinsics会导致编译错误)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 11:07:08