缩放osmdroid上的地图视图以适应特定的边界框。

6
我想使用zoomToBoundingBox方法将地图缩放到特定的边界框。然而,这个方法只会在缩放级别0上显示地图。
我在mapView.java源代码中找到了以下内容:
/** * 尽可能紧密地缩放地图以包围指定的边界框。 * 必须在显示布局完成后调用,否则屏幕尺寸是未知的,并且总是缩放到缩放级别0的中心。 * 建议:检查getScreenRect(null).getHeight() > 0 */
我检查了getScreenRect(null).getHeight() > 0,得到的结果为false。
如何在程序中完成显示布局呢? 有什么方法可以使上述断言返回true呢?
1个回答

2

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

我有自己的MapView子类,用于保护我免受地图库选择的影响。这个类具有清除点、添加点和布局点的接口样式。在layoutPoints()方法中,我从点的集合创建itemized overlay lay,并创建存储在类的实例方法中的边界框。

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

我不确定这是否是最佳方法,但它对我来说似乎有效(除了缩放似乎有点太远,但那是另一个时间的另一个问题)。


尝试过这个,但是无法通过 MyMapView mapView = (MapView) findViewById(R.id.mapview); 创建视图。 - thanasio2
这会对layout.xml文件或创建视图的方式产生影响吗?如果我这样做:mapView =(MyMapView)findViewById(R.id.mapview);它会拒绝执行转换并崩溃...!!! - thanasio2
我通过编程方式创建我的地图视图(实际上是所有视图)。我认为有一种方法可以使用正确的构造函数定义“MyMapView”,以便IDE可以在XML中创建一个MyMapView。但这是很多年前模糊的“当我第一次开始使用Android”阅读的内容。 - Colin
@thanasio2,如果您使用 <my.package.name.MyMapView android:id="@+id/mapview" android:layout_width="match_parent" android:layout_height="match_parent" />,则应该可以在您的 xml 布局中正常工作。 - Tobias Helbich

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接