Zoom mapView for a specific bounding box on osmdroid

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

In the mapView.java source code, I found this:

/ ** * Zoom in on the map to close the specified bounding box as much as possible. * Must be called after completing the display layout, or screen sizes are unknown, and * will always approach the center of zoom level 0. * Suggestion: Check getScreenRect (null) .getHeight ()> 0 * /

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

How to complete the program screen?
What can I do to make the above predicate give me the truth?

+6
source share
1 answer

I had the same problem. I turned to him by calling zoomToBoundingBox () at the end of the onLayout () method.

I have my own subclass of MapView, which I use to isolate me from choosing a map library. This class has a clearPoints / addPoint / layoutPoints interface style. In the layoutPoints () method, I create an elementary overlay layer from a collection of points and create a bounding box that is stored in the class instance method.

 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 redefinition of onLayout () 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 to do this, but it seems to work for me (except that the zoom seems to be too far away, but this problem is at another time).

+2
source

All Articles