【问题标题】:Display 3D Terrain Mapbox v10 Android显示 3D 地形 Mapbox v10 Android
【发布时间】:2021-12-16 16:27:55
【问题描述】:

我正在尝试使用新的 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:尝试调用 接口方法'java.util.Iterator java.lang.Iterable.iterator()' on 空对象引用 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err:在 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:在 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:在 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:在 com.mapbox.maps.StyleObserver.onStyleLoaded(StyleObserver.kt:58) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W / System.err:在 com.mapbox.maps.NativeObserver.notify(NativeObserver.kt:61) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err:
在 android.os.MessageQueue.nativePollOnce(Native Method) 2021-11-02 17:21:39.439 23646-23646/com.example.myapplication W/System.err:
在 android.os.MessageQueue.next(MessageQueue.java:335) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
在 android.os.Looper.loop(Looper.java:206) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:在 android.app.ActivityThread.main(ActivityThread.java:8633) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
在 java.lang.reflect.Method.invoke(Native Method) 2021-11-02 17:21:39.440 23646-23646/com.example.myapplication W/System.err:
在 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:在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130) 2021-11-02 17:21:39.446 23646-23646/com.example.myapplication E/libc++abi:以未捕获的类型异常终止 jni::PendingJavaException 2021-11-02 17:21:39.447 23646-23646/com.example.myapplication A/libc:致命信号 6 (SIGABRT), tid 23646 (e.myapplication) 中的代码 -1 (SI_QUEUE), pid 23646 (e.myapplication)

【问题讨论】:

    标签: java android mapbox mapbox-android


    【解决方案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】:
      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) 
          }
      )
      

      希望它能帮到我,因为它对我来说很完美,没有任何问题。

      【讨论】:

        猜你喜欢
        • 2023-02-23
        • 2022-10-21
        • 2022-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多