zoom mapView to a certain bounding box on osmdroid

柔情痞子 提交于 2019-12-07 03:27:21

问题


I want to use the zoomToBoundingBox method to zoom a map to a specific bounding box.
The method does nothing but display the map on zoom level 0.

In the mapView.java source code I found this:

/** * Zoom the map to enclose the specified bounding box, as closely as possible. * Must be called after display layout is complete, or screen dimensions are not known, and * will always zoom to center of zoom level 0. * Suggestion: Check getScreenRect(null).getHeight() > 0 */

I checked getScreenRect(null).getHeight() > 0 and it gives me false.

How do you complete display layout programmatically?
What can I do so that the above predicate gives me true?


回答1:


I had the same problem. I addressed it by calling the zoomToBoundingBox() call at the end of the onLayout() method.

I have my own subclass of MapView that I use to insulate me from the map library choice. This class has a clearPoints / addPoint / layoutPoints style of interface. Within the layoutPoints() method I create the itemized overlay lay from the collection of points and create the bounding box that is stored in an instance method of the class.

public class MyMapView extends MapView {

// The bounding box around all map points
// Used to determine the correct zoom level to show everything
private int minLat = Integer.MAX_VALUE;
private int minLon = Integer.MAX_VALUE;
private int maxLat = Integer.MIN_VALUE;
private int maxLon = Integer.MIN_VALUE;

private BoundingBoxE6 boundingBox;

My onLayout() override has:

/* (non-Javadoc)
 * @see org.osmdroid.views.MapView#onLayout(boolean, int, int, int, int)
 */
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
    super.onLayout(arg0, arg1, arg2, arg3, arg4);

    // Now that we have laid out the map view,
    // zoom to any bounding box
    if (this.boundingBox != null) {
        this.zoomToBoundingBox(this.boundingBox);
    }
}

I'm not sure if this is the best way of doing it, but it seems to work for me (except that the zoom appears to be a little too far out, but that's another problem for another time).



来源:https://stackoverflow.com/questions/15658277/zoom-mapview-to-a-certain-bounding-box-on-osmdroid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!