显示 3D 地形 Mapbox v10 Android

Display 3D Terrain Mapbox v10 Android

我正在尝试将新的地图框用于 android v10,特别是新的 3d 地形功能。所有示例都在 Kotlin 中,我已按照下面的在线指南进行操作,但我将 运行 保留在相同的错误消息中。

在线示例:

mapboxMap.loadStyle(
 styleExtension = style(Style.SATELLITE_STREETS) {
   +rasterDemSource("TERRAIN_SOURCE") {
     url("mapbox://mapbox.mapbox-terrain-dem-v1")
   }
   +terrain("TERRAIN_SOURCE") {
     exaggeration(1.1) 
   }
)

以下是我使用的代码:

public class MainActivity extends AppCompatActivity {    
    private MapView mapView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mapView = findViewById(R.id.mapView);
        //mapView.getMapboxMap().loadStyleUri(Style.OUTDOORS);

        MapboxMap mapboxMap = mapView.getMapboxMap();


        StyleContract.StyleExtension styleExtension = new StyleContract.StyleExtension() {
            @NonNull
            @Override
            public String getStyleUri() {
                return Style.SATELLITE;
            }

            @NonNull
            @Override
            public List<StyleContract.StyleSourceExtension> getSources() {
                RasterDemSource rasterDemSource = new RasterDemSource(new RasterDemSource.Builder("TERRAIN_SOURCE"));
                rasterDemSource.url("mapbox://mapbox.mapbox-terrain-v2");

                List<StyleContract.StyleSourceExtension> ex = new ArrayList<StyleContract.StyleSourceExtension>();
                ex.add(rasterDemSource);

                return ex;
            }

            @NonNull
            @Override
            public List<StyleContract.StyleImageExtension> getImages() {

                return null;
            }

            @NonNull
            @Override
            public List<Pair<StyleContract.StyleLayerExtension, LayerPosition>> getLayers() {
                return null;
            }

            @Nullable
            @Override
            public StyleContract.StyleLightExtension getLight() {
                return null;
            }

            @Nullable
            @Override
            public StyleContract.StyleTerrainExtension getTerrain() {
                Terrain terrain = new Terrain("TERRAIN_SOURCE");
                terrain.exaggeration(1.1);

                return null;
            }
        };

        mapboxMap.loadStyle(styleExtension);
    }
}

我不断收到以下错误代码:

2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.lang.Iterable.iterator()' on a null object reference 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err: at com.mapbox.maps.MapboxMap.onFinishLoadingStyleExtension$sdk_release(MapboxMap.kt:1349) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err: at com.mapbox.maps.MapboxMap$loadStyle.onStyleLoaded(MapboxMap.kt:163) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err: at com.mapbox.maps.MapboxMap$initializeStyleLoad.onStyleLoaded(MapboxMap.kt:214) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err: at com.mapbox.maps.StyleObserver.onStyleLoaded(StyleObserver.kt:58) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err: at com.mapbox.maps.NativeObserver.notify(NativeObserver.kt:61) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err:
at android.os.MessageQueue.nativePollOnce(Native Method) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err:
at android.os.MessageQueue.next(MessageQueue.java:335) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
at android.os.Looper.loop(Looper.java:206) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err: at android.app.ActivityThread.main(ActivityThread.java:8633) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
at java.lang.reflect.Method.invoke(Native Method) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 2021-11-02 17:21:39.446 23646-23646/com.example.myapplication E/libc++abi: terminating with uncaught exception of type jni::PendingJavaException 2021-11-02 17:21:39.447 23646-23646/com.example.myapplication A/libc: Fatal signal 6 (SIGABRT), code -1 (SI_QUEUE) in tid 23646 (e.myapplication), pid 23646 (e.myapplication)

mapboxMap.loadStyle(
    styleExtension = style(Style.SATELLITE_STREETS) {
    +rasterDemSource("TERRAIN_SOURCE") {
    url("mapbox://mapbox.mapbox-terrain-dem-v1")
    }
    +terrain("TERRAIN_SOURCE") {
      exaggeration(1.1) 
    }
)

而不是这个,尝试使用

mapboxMap.loadStyle(
    styleExtension = style(Style.SATELLITE) {
    +rasterDemSource("TERRAIN_SOURCE") {
    url("mapbox://mapbox.mapbox-terrain-dem-v1")
    }
    +terrain("TERRAIN_SOURCE") {
      exaggeration(1.1) 
    }
)

希望对我有所帮助,因为它对我来说非常完美,没有任何问题。

原来我错误地使用了样式扩展,现在可以使用以下内容了:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mapView = findViewById(R.id.mapView);
    MapboxMap mapboxMap = mapView.getMapboxMap();

    mapboxMap.loadStyle(createStyle());
}

private StyleContract.StyleExtension createStyle() {
    StyleExtensionImpl.Builder builder = new StyleExtensionImpl.Builder(Style.SATELLITE);

    RasterDemSource rasterDemSource = new RasterDemSource(new RasterDemSource.Builder("TERRAIN_SOURCE").tileSize(514));
    rasterDemSource.url("mapbox://mapbox.mapbox-terrain-dem-v1");
    builder.addSource(rasterDemSource);

    Terrain terrain = new Terrain("TERRAIN_SOURCE");
    terrain.exaggeration(1.1);
    builder.setTerrain(terrain);

    return builder.build();
}