1

I followed the example here: How to set screen zoom by geopoints?

But my problem is that my route is very small in the mapview. It should end near the borders.

This is how it looks with the code below:

this is how it looks with the code below

private void centerMap(ArrayList<GeoPoint> waypoints) {
    GeoPoint[] geoPoints = new GeoPoint[waypoints.size()];
    for(int i = 0; i<waypoints.size(); i++) {
        geoPoints[i] = waypoints.get(i);
    }

    int minLat = Integer.MAX_VALUE;
    int maxLat = Integer.MIN_VALUE;
    int minLon = Integer.MAX_VALUE;
    int maxLon = Integer.MIN_VALUE;

    //for (Point point : twoPoints) {
    for(GeoPoint point: geoPoints){
        int lat = (int) (point.getLatitude() * 1E6);
        int lon = (int) (point.getLongitude() * 1E6);

        maxLat = Math.max(lat, maxLat);
        minLat = Math.min(lat, minLat);
        maxLon = Math.max(lon, maxLon);
        minLon = Math.min(lon, minLon);
    }

    mapView.getController().zoomToSpan(Math.abs(maxLat - minLat), Math.abs(maxLon - minLon));
    mapView.getController().setCenter(new GeoPoint((maxLat + minLat) / 2, (maxLon + minLon) / 2));
}
Community
  • 1
  • 1
Anna
  • 11
  • 3
  • Beware that `(maxLat + minLat) / 2` can overflow. Use `maxLat - (maxLat - minLat) / 2` instead. The same applies to the longitude. – scai Jan 05 '17 at 17:27

1 Answers1

0
private void centerMap(ArrayList<GeoPoint> waypoints) {
    BoundingBox bb = BoundingBox.fromGeoPoints(waypoints);
    mapView.zoomToBoundingBox(bb);
}

And note that lat/lon values are now double by default in osmdroid.

MKer
  • 3,430
  • 1
  • 13
  • 18
  • thanx for your answer. But in my app there is the same problem with the scale. Do this work in your app? – Anna Jan 15 '17 at 17:04
  • yes, it works as long as you use it when the map is already displayed = not working during onCreate. – MKer Jan 18 '17 at 16:51