I need to populate my overlay with many (~400) points, add them to PathOverlay
and then set the zoom level so that the user could see the whole path on the screen.
One solution would be to keep max longitude, min longitude, min longitude, min longitude and on the end based on these 4 numbers calculate (this is the part I haven't figured out yet, as I don't know how is the int
in the setZoom()
related to distances on the map) calculate the appropriate zoom level. Then I would use setCenter()
method.
Is there any simpler way to do this using OSMDroid? If not, how should I determine correct zoom level?
The question is old but there's not a definitive answer, so I'll post mine:
I use this snippet everyday. THis will take care of handling the situation where the map view has just been created. Infact it can happend that you want to zoom to a boundingbox as a start behaiviour for the map. If you call this method before the view is displayed it will register a listener to execute the zoom as soon as the view is ready.
map
is my MapView
public void zoomToBounds(final BoundingBox box) {
if (map.getHeight() > 0) {
map.zoomToBoundingBox(box, true);
} else {
ViewTreeObserver vto = map.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
map.zoomToBoundingBox(box, true);
ViewTreeObserver vto2 = map.getViewTreeObserver();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
vto2.removeGlobalOnLayoutListener(this);
} else {
vto2.removeOnGlobalLayoutListener(this);
}
}
});
}
}
And here's how to get a boundingBox from a list of GeoPoints
public BoundingBox computeArea(ArrayList<GeoPoint> points) {
double nord = 0, sud = 0, ovest = 0, est = 0;
for (int i = 0; i < points.size(); i++) {
if (points.get(i) == null) continue;
double lat = points.get(i).getLatitude();
double lon = points.get(i).getLongitude();
if ((i == 0) || (lat > nord)) nord = lat;
if ((i == 0) || (lat < sud)) sud = lat;
if ((i == 0) || (lon < ovest)) ovest = lon;
if ((i == 0) || (lon > est)) est = lon;
}
return new BoundingBox(nord, est, sud, ovest);
}
As of OsmDroid 6.1.0, the easiest way to zoom to an Overlay (or to its BoundingBox) seems to be:
map.zoomToBoundingBox(overlay.getBounds(), false);
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