有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java显示3D地形地图盒v10 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$1.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$1.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 安卓.os.MessageQueue.nativePollOnce(Native Method) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err:
at 安卓.os.MessageQueue.next(MessageQueue.java:335) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
at 安卓.os.Looper.loop(Looper.java:206) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err: at 安卓.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.安卓.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.安卓.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)


共 (2) 个答案

  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();
        }
    
  2. # 2 楼答案

    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) 
        }
    )
    

    希望它对我有帮助,因为它对我来说完美无瑕