Search code examples
androidmapboxnavigationview

Mapbox NavigationView with custom Tileset or Style


how can I add one tileset to the NavigationView?

My first try was to create a whole style with Mapbox Studio and link a tileset to it. Then I added the style to the NavigationView inside of a Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    [...]

    // Mapbox
    navigationView = view.findViewById(R.id.mapboxNavigationView);
    navigationView.onCreate(null);

    [...]

    navigationView.initialize(this, initialPosition);

    return view;
}



@SuppressLint("MissingPermission")
@Override
public void onNavigationReady(boolean isRunning) {
    mapboxMap = navigationView.retrieveNavigationMapboxMap().retrieveMap();

    mapboxMap.setStyle("mySecretStyle", new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {
            // Never called ...
        }
    });
}

The style is successfully added as you can see here.

Custom style

But the following error occurs and no navigation starts

java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List com.mapbox.mapboxsdk.maps.Style.getSources()' on a null object reference at com.mapbox.services.android.navigation.ui.v5.map.NavigationMapboxMap.initializeWayName(NavigationMapboxMap.java:668) at com.mapbox.services.android.navigation.ui.v5.map.NavigationMapboxMap.addProgressChangeListener(NavigationMapboxMap.java:277) at com.mapbox.services.android.navigation.ui.v5.NavigationView.initializeNavigationListeners(NavigationView.java:691) at com.mapbox.services.android.navigation.ui.v5.NavigationView.initializeNavigation(NavigationView.java:632) at com.mapbox.services.android.navigation.ui.v5.NavigationView.startNavigation(NavigationView.java:394)

My second approach was to only add a tileset, uploaded at Mapbox Studio:

@Override
public void onNavigationReady(boolean isRunning) {
    mapboxMap = navigationView.retrieveNavigationMapboxMap().retrieveMap();

    mapboxMap.getStyle(new Style.OnStyleLoaded() {
        @Override
        public void onStyleLoaded(@NonNull Style style) {   
            String sourceId = "indoor-source";
            style.addSource(new RasterSource(sourceId, "mytileid"));
            String layerId = "indoor-layer";
            RasterLayer rasterLayer = new RasterLayer(layerId, sourceId);
            style.addLayer(rasterLayer);
        }
    });
}

But that doesn't work either - but at least the navigation is running with the default navigation style.

My third try (with a valid style)

@Override
public void onNavigationRunning() {
    navigationView.retrieveNavigationMapboxMap().retrieveMap().setStyle("mySecretStyle");
}

...results into a SIGSEGV

--------- beginning of crash 2020-02-28 15:49:32.835 A/libc: Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0x681713e80325b4 in tid 29828 (oid.med....), pid 29828 (oid.med....)

Any ideas?


Solution

    1. I see mapboxMap.setStyle("mySecretStyle",. Are you just passing through your style id instead of mySecretStyle there? If so, you should use new Style.Builder().fromUri() instead.
    mapboxMap.setStyle(new Style.Builder().fromUri("styleIdHere"), new Style.OnStyleLoaded() {
              @Override
              public void onStyleLoaded(@NonNull Style style) {
    
    
              }
            });
    
    1. Is your tileset coming from the Mapbox Tilesets API? If so:

    2. If you're using a RasterSource, see https://github.com/mapbox/mapbox-android-demo/search?q=RasterSource&unscoped_q=RasterSource of RasterSource usage in the demo app. It could be that you're incorrectly initializing various source objects.

    In general, if you could explain what type of data you're trying to add, I might be able to help you further. You could also add the data you want, to the style in Mapbox Studio rather than in runtime on the device. Then, all you'd have to do is load the style.