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?

1 Answer 1

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();
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.