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.
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.
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