2

I am plotting a route using the osmdroid bonuspack. The start and end coordinates of this route are:-

origin - 52.24896,0.71795

destination - 54.27916,-1.9732

The relevant part of my code is:-

        BoundingBox b = getBoundingBox(origin,destination);
        map.setMinZoomLevel(7d);  //   <<====  Needed to initialise zoom level
        map.zoomToBoundingBox(b,false);
        map.invalidate();

and

private BoundingBox getBoundingBox(GeoPoint start, GeoPoint end) {
    double north;
    double south;
    double east;
    double west;
    if(start.getLatitude() > end.getLatitude()) {
        north = start.getLatitude();
        south = end.getLatitude();
    } else {
        north = end.getLatitude();
        south = start.getLatitude();
    }
    if(start.getLongitude() > end.getLongitude()) {
        east = start.getLongitude();
        west = end.getLongitude();
    } else {
        east = end.getLongitude();
        west = start.getLongitude();
    }

    return new BoundingBox(north, east, south, west);
}

The map tiles I'm using are zoom levels 7-13.

Without the 'setMinZoomLevel' statement the origin/destination markers are displayed in the top left corner of the screen at a zoom level that can best be described as deep space. If I include 'setMinZoomLevel' then the map is displayed at that level and doesn't zoom to fit the boundingbox (which should be at level 8). I have tried a much smaller route with exactly the same end result.

What am I doing wrong?

Doug Conran
  • 437
  • 1
  • 5
  • 17

1 Answers1

2

It transpires that a map needs to already be being displayed, presumably so that the screen dimensions have been recorded. The answer, therefore, is firstly to set a zoom level using MapController as part of the onCreate process and then to invoke the zoomToBoundingBox within MapView.OnFirstLayoutListener. The code I used was:-

map.addOnFirstLayoutListener(new MapView.OnFirstLayoutListener() {
    public void onFirstLayout(View v, int left, int top, int right, int bottom) {
        BoundingBox b = getBoundingBox(origin,destination);
        map.zoomToBoundingBox(b,false,100);
        map.invalidate();
    }
});
Doug Conran
  • 437
  • 1
  • 5
  • 17