Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zoom mapView to a certain bounding box on osmdroid

I want to use the zoomToBoundingBox method to zoom a map to a specific bounding box.
The method does nothing but display the map on zoom level 0.

In the mapView.java source code I found this:

/** * Zoom the map to enclose the specified bounding box, as closely as possible. * Must be called after display layout is complete, or screen dimensions are not known, and * will always zoom to center of zoom level 0. * Suggestion: Check getScreenRect(null).getHeight() > 0 */

I checked getScreenRect(null).getHeight() > 0 and it gives me false.

How do you complete display layout programmatically?
What can I do so that the above predicate gives me true?

like image 744
thanasio2 Avatar asked Mar 27 '13 12:03

thanasio2


1 Answers

I had the same problem. I addressed it by calling the zoomToBoundingBox() call at the end of the onLayout() method.

I have my own subclass of MapView that I use to insulate me from the map library choice. This class has a clearPoints / addPoint / layoutPoints style of interface. Within the layoutPoints() method I create the itemized overlay lay from the collection of points and create the bounding box that is stored in an instance method of the class.

public class MyMapView extends MapView {

// The bounding box around all map points
// Used to determine the correct zoom level to show everything
private int minLat = Integer.MAX_VALUE;
private int minLon = Integer.MAX_VALUE;
private int maxLat = Integer.MIN_VALUE;
private int maxLon = Integer.MIN_VALUE;

private BoundingBoxE6 boundingBox;

My onLayout() override has:

/* (non-Javadoc)
 * @see org.osmdroid.views.MapView#onLayout(boolean, int, int, int, int)
 */
@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);
    }
}

I'm not sure if this is the best way of doing it, but it seems to work for me (except that the zoom appears to be a little too far out, but that's another problem for another time).

like image 110
Colin Avatar answered Sep 24 '22 05:09

Colin