OSMDroid Limit the map in North/South

941 views Asked by At

I am working with the OSMDroid API and I'm wondering if there's a way to limit the map in north and south. I mean, is this possible to prevent the map from endlessly repeating itself on the Y-Axis.

I found this issue on the Github's "osmdroid" project but the code of the patch is too old and cannot be applied to the new version of osmdroid (4.2)

EDIT

I tried the setScrollableAreaLimit method but there is a bug (maybe) in the upper-left point. The map jump on the other side when I come close to its end.

Thank you by advance

4

There are 4 answers

0
riderofzion On BEST ANSWER

I modified two lines in the class MapView.

I replaced x += worldSize; by x=0; and y += worldSize; by y=0;

      public void scrollTo(int x, int y) {
        final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
        while (x < 0) {
            //x += worldSize;
         x=0;
        }
        while (x >= worldSize) {
            x -= worldSize;
        }
       while (y < 0) {
          // y += worldSize;
           y=0;
        }
        while (y >= worldSize) {
            y -= worldSize;
        }
     [...]
     }
0
ryanecrist On

I was able to overcome this by expanding on @kenji's answer. With the subclass approach, I found that rapidly zooming out would sometimes reset the map to the south pole. To prevent this, the y-value needs to be downscaled first (just as the super class implementation does in v5.6.5).

@Override
public void scrollTo(int x, int y) {

    final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
    final int mapViewHeight = getHeight();

    // Downscale the y-value in case the user just zoomed out.
    while (y >= worldSize) {
        y -= worldSize;
    }

    if (y < 0) {
        y = 0;
    } else if ((y + mapViewHeight) > worldSize) {
        y = worldSize - mapViewHeight;
    }

    super.scrollTo(x, y);
}
0
yago On

use setScrollableAreaLimit.

BoundingBoxE6 bbox_bariloche = new BoundingBoxE6(-41.033770,  -71.591065, -41.207056,-71.111444);
            map.setScrollableAreaLimit(bbox_bariloche);
1
kenji matsuoka On

I was extend MapView and override to scrollTo()

public void scrollTo(int x, int y) {
    final int worldSize = TileSystem.MapSize(this.getZoomLevel(false));
    if(y < 0) { // when over north pole
        y = 0; // scroll to north pole
    }else if(y + getHeight() >= worldSize) { // when over south pole
        y = worldSize-getHeight() - 1; // scroll to south pole
    }
    super.scrollTo(x,y);
}