【问题标题】:zoom mapView to a certain bounding box on osmdroid将 mapView 缩放到 osmdroid 上的某个边界框
【发布时间】:2013-03-17 12:00:21
【问题描述】:

我想使用 zoomToBoundingBox 方法将地图缩放到特定的边界框。
该方法只在缩放级别 0 上显示地图。

在 mapView.java 源代码中我发现了这个:

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

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

如何以编程方式完成显示布局?
我该怎么做才能让上面的谓词给我真实的感觉?

【问题讨论】:

    标签: android maps openstreetmap bounding-box


    【解决方案1】:

    我遇到了同样的问题。我通过在 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);
        }
    }
    

    我不确定这是否是最好的方法,但它似乎对我有用(除了缩放似乎有点太远了,但这是另一个问题)。

    【讨论】:

    • 试过了,但无法使用 MyMapView mapView = (MapView) findViewById(R.id.mapview); 创建视图
    • 这对 layout.xml 文件或您创建视图的方式有影响吗?如果我这样做:mapView = (MyMapView) findViewById(R.id.mapview);它拒绝执行演员表并崩溃......!!!
    • 我以编程方式创建我的地图视图(实际上是我的所有视图)。我认为有一种方法可以使用正确的构造函数定义“MyMapView”,以便 IDE 可以在 XML 中创建 MyMapView。但那是多年前读到的朦胧的“当我第一次开始使用 Android 时”。
    • @thanasio2 如果您使用<my.package.name.MyMapView android:id="@+id/mapview" android:layout_width="match_parent" android:layout_height="match_parent" />,它应该适用于您的xml布局。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 2018-04-26
    • 1970-01-01
    • 2018-11-07
    • 2018-11-05
    相关资源
    最近更新 更多