question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem with zoomToBoundingBox()

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
spyhunter99commented, Oct 29, 2016

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)

1reaction
2ndGABcommented, Mar 4, 2016

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:

Read more comments on GitHub >

github_iconTop Results From Across the Web

OSMDroid5 : can't get zoomToBoundingBox() working correctly
First, problem in calculation; onLayout() is called after onResume() and I explored the code of zoomToBoundingBox() and here are the results ...
Read more >
zoomToBoundingBox not working - Google Groups
Hello everyone, i am a newbie to osmdroid and i encountered a problem when trying to view all ... addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
Read more >
Zoom to Bounding Box - Popular Blocks - Mike Bostock
Zoom to Bounding Box. Open. This example demonstrates how to compute a suitable translate and scale to zoom to the bounding box of...
Read more >
[Solved]-d3 zoom to bounding box II scrolling while zooming ...
Coding example for the question d3 zoom to bounding box II scrolling while ... There is another transition inside reset() function, and we...
Read more >
Zoom to Bounding Box / Raj Shrestha - Observable
Zoom to Bounding Box ... corona = Error: File not found: covid_data.json ... n), arc: ƒ(), area: ƒ(), areaRadial: ƒ(), ascending: ƒ(t, n),...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found