osmdroid: Problem with zoomToBoundingBox()

I have problem to use zoomToBoundingBox(). According to what I’ve read somewhere here, I’ve created my own MapView class to call zoomToBoundingBox() in onLayout(). Here is my code:

public class MyMapView extends MapView {

    public void setBoundingBox(BoundingBoxE6 boundingBox) {
        this.boundingBox = boundingBox;
    }

    private BoundingBoxE6 boundingBox = null;

    ...

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        super.onLayout(arg0, arg1, arg2, arg3, arg4);

        // Now that we have laid out the map view,
        // zoom to any bounding box
        if (this.boundingBox != null) {
            this.zoomToBoundingBox(this.boundingBox);
        }
    }
}

So, In my fragment I initialize the boundingBox:

@Override
public void onResume() {
    super.onResume();

    final BoundingBoxE6 boundingBox = new BoundingBoxE6( ... );
    // this gives ``N:44899816; E:5020385; S:44899001; W:5019482``

    mMapView.setBoundingBox(boundingBox);

    mMapView.invalidate();
}

So, the calculated bounding box should obviously cause to get a max zoom level but it doesn’t.

First, problem in calculation; onLayout() is called after onResume() and I explored the code of zoomToBoundingBox() and here are the results on each data calculated:

zoomLevel = 0
maxZoomLatitudeSpan = 55.08
requiredLatitudeZoom = 14.0  // I have a big doubt on this result!
maxZoomLongitudeSpan = 472.07
requiredLongitudeZoom = 17.0  // hum well, better but not best expected value
So zoomToBoundingBox() will choose a zoom level of 14.0 why? Anyway,  requiredLongitudeZoom of 17.0 is not the best result. As you can see here (http://imgur.com/ZEnX2Za), the max level of 18.0 is possible.

So how should I use zoomToBoundingBox() ?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17

Commits related to this issue

Most upvoted comments

simplest solution may be to just call zoomToBoundingBox twice.

        final BoundingBox boundingBox = new BoundingBox(41.906802,12.445436,41.900073,12.457852 );
        mMapView.zoomToBoundingBox(boundingBox, false);
        mMapView.zoomToBoundingBox(boundingBox, false);
        mMapView.invalidate();

this works for me on all zoom levels that i’ve tested. Always goes to the expected zoom level (15)

Hello, I tried to call zoomToBoundingBox() in MapView.addOnFirstLayoutListener(new OnFirstLayoutListener() { ...}); but that doesn’t solve all problems. The view is well centered (calling it with animate=true doesn’t work at all #264) but the zoom level is not ok. Below is the code:

mMapView.addOnFirstLayoutListener(new MapView.OnFirstLayoutListener() {

        @Override
        public void onFirstLayout(View v, int left, int top, int right, int bottom) {

            final BoundingBoxE6 boundingBox = new BoundingBoxE6(44899816, 5020385,44899001,5019482);
            mMapView.zoomToBoundingBox(boundingBox, false);
            mMapView.invalidate();
    }
}

If I put the same code in an onClickListener(), it perfectly works: