将mapView缩放到osmdroid上的某个边界框

时间:2013-03-27 12:00:28

标签: android maps openstreetmap bounding-box

我想使用zoomToBoundingBox方法将地图缩放到特定的边界框 该方法除了在缩放级别0上显示地图外什么都不做。

在mapView.java源代码中我找到了:

  

/ **            *缩放地图以尽可能地包围指定的边界框。            *必须在显示布局完成后调用,或者屏幕尺寸未知,并且            *将始终缩放到缩放级别0的中心。            *建议:检查getScreenRect(null).getHeight()> 0            * /

我检查了getScreenRect(null).getHeight() > 0并且它给了我错误。

如何以编程方式完成显示布局?
我能做什么才能使上述谓词真实?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。我通过在onLayout()方法的末尾调用zoomToBoundingBox()来解决它。

我有自己的MapView子类,用于将我与地图库选项隔离开来。该类具有clearPoints / addPoint / layoutPoints样式的接口。在layoutPoints()方法中,我从点集合创建逐项覆盖层,并创建存储在类的实例方法中的边界框。

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;

我的onLayout()覆盖有:

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

我不确定这是否是最佳方式,但它似乎对我有效(除了缩放看起来有点太远了,但这是另一个问题)。