Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSMDroid: zooming to show the whole PathOverlay

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?

like image 835
syntagma Avatar asked Dec 16 '13 10:12

syntagma


2 Answers

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);

    }
like image 73
theBugger Avatar answered Sep 21 '22 13:09

theBugger


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);
like image 32
Guido De Vecchi Avatar answered Sep 19 '22 13:09

Guido De Vecchi