OSMDroid: zooming to show the whole PathOverlay
Asked Answered
B

3

6

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?

Bedfordshire answered 16/12, 2013 at 10:31 Comment(2)
Did you find the solution ?Tridentum
@bikash I did not but you might want to look at the newer versions of OSMDroid, they might have fixed that.Bedfordshire
W
5

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

    }
Wame answered 30/9, 2016 at 7:56 Comment(0)
E
3

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);
Esthonia answered 8/8, 2019 at 14:2 Comment(0)
A
1

Going by memory here... should be something like this:

double north = 47.111111;
double east = -122.111111;
double south = 46.111111;
double west = -120.111111;

BoundingBoxE6 boundingBox = new BoundingBoxE6(north, east, south, west);

mMapView.getController().zoomToSpan(boundingBox);
Acicular answered 28/12, 2013 at 20:27 Comment(1)
IMapController has no such method zoomToSpan. Use mMapView.zoomToBoundingBox(boundingBox, bool animate)Wame

© 2022 - 2024 — McMap. All rights reserved.