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?
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With