Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSMDroid - Center to Polyline

I am working with OSMDroid, I have a collections of Geopoints and then I create a Polyline like this:

private void drawPolyline(ArrayList<GeoPoint> polyline) {
    Polyline line = new Polyline(this);
    line.setPoints(polyline);
    line.setWidth(5f);
    line.setGeodesic(true);
    mv_map.getOverlays().add(line);
    mv_map.invalidate();
}

This code works fine, but the problem is that I need to focus this route in a map. I know I could find a mid point and center the map to it, but I don't know how to set the correct zoom and I don't know if there is already a way to find the center of the polyline more efficiently.

Thank you.

like image 777
8370 Avatar asked Oct 14 '25 07:10

8370


1 Answers

One possible solution could use the method zoomToBoundingBox. Here is a small sample on how you can use it with a list of points.

                    int minLat = Integer.MAX_VALUE;
                    int maxLat = Integer.MIN_VALUE;
                    int minLong = Integer.MAX_VALUE;
                    int maxLong = Integer.MIN_VALUE;

                    for (GeoPoint point : polylinePoints) {       
                        if (point.getLatitudeE6() < minLat)
                            minLat = point.getLatitudeE6();
                        if (point.getLatitudeE6() > maxLat)
                            maxLat = point.getLatitudeE6();
                        if (point.getLongitudeE6() < minLong)
                            minLong = point.getLongitudeE6();
                        if (point.getLongitudeE6() > maxLong)
                            maxLong = point.getLongitudeE6();
                    }

                    BoundingBoxE6 boundingBox = new BoundingBoxE6(maxLat, maxLong, minLat, minLong);
                    osmMap.zoomToBoundingBox(boundingBox.increaseByScale(1.3f));//this is some sort of padding to your bounding box

The idea is to find a square where all your points would fit into.

like image 120
avi Avatar answered Oct 17 '25 00:10

avi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!