Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display 3D Terrain Mapbox v10 Android

I am trying to use the new mapbox for android v10 with specifically the new 3d terrain feature. All the examples are in Kotlin, I have followed the online guide below but I keep running into the same error message.

Online example:

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

The following is the code I am using:

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

I keep getting the following error code:

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

like image 902
Reafidy Avatar asked Nov 02 '21 04:11

Reafidy


2 Answers

Turns out I was using the style extension incorrectly, the following now works:

@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();
}
like image 147
Reafidy Avatar answered Oct 09 '22 08:10

Reafidy


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

instead of this, try using

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

Hope it helps as it worked perfect for me without any issue.

like image 2
Sambhav Khandelwal Avatar answered Oct 09 '22 08:10

Sambhav Khandelwal