Search code examples
androidmapboxmapbox-android

Stop infinite world scrolling in Mapbox


Mapbox map is having infinite world scrolling. I have tried a lot but I am unable to stop world infinite scrolling.

Mapbox was having some way previously (Disable horizontal repeating of world map with mapbox) but not currently supported and not sure for Android.

Also, by listening to camera move, I tried cancelling transaction (camera animation) but it did not worked.

I was not able to find any related API in mapbox which can stop this continuous world scrolling in Android.

I am using Mapbox SDK 9.0.0


Solution

  • This example from the Mapbox documentation shows how to restrict map panning. You can create a LatLngBounds object from the northwest and southeast corners of the region where you'd like to restrict map panning to. In this case, the region will be the entire world, and the boundaries will specify where you want the "left" and "right" vertical cutoffs to be.

    The coordinates below worked well for me, but you could further adjust them to get the exact effect that you are looking for. geojson.io is a great resource for visualizing GeoJSON features on a map, if you'd like to experiment with adjusting this bounding box.

    /* Define bounding box. */
        private static final LatLng BOUND_CORNER_NW = new LatLng(82.85338229176081, -141.328125);
        private static final LatLng BOUND_CORNER_SE = new LatLng( -62.59334083012023, 167.34375);
        private static final LatLngBounds RESTRICTED_BOUNDS_AREA = new LatLngBounds.Builder()
                .include(BOUND_CORNER_NW)
                .include(BOUND_CORNER_SE)
                .build();
    

    Note that Mapbox GL JS has setRenderWorldCopies and getRenderWorldCopies methods, which, as demonstrated in this render world copies example, allows you to toggle between rendering a single world and multiple copies of the world. The Maps SDK for Android does not include the same feature, however.