4

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.

8370
  • 153
  • 10

2 Answers2

3

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.

avi
  • 912
  • 8
  • 22
0
    // Round the Conor of the line.
    polyline.outlinePaint.strokeCap = Paint.Cap.ROUND
    // Add to map.
    binding.openMapView.overlays.add(polyline)
    // Zoom map to fit with polyline.
    binding.openMapView.post {
        binding.openMapView.zoomToBoundingBox(polyline.bounds.increaseByScale(1.5f), false)
    }

Example https://github.com/osmdroid/osmdroid/blob/4094f3869a72215e5f7fb5ead78c21674b60ab07/OpenStreetMapViewer/src/main/java/org/osmdroid/samplefragments/drawing/ShowAdvancedPolylineStyles.java