OSMDroid5:无法使zoomToBoundingBox()正常工作

时间:2016-01-03 14:07:52

标签: osmdroid

我有使用``zoomToBoundingBox()```的问题。 根据我在这里读到的内容,我创建了自己的MapView类来调用On Layout()中的zoomToBoundingBox()。这是我的代码:

公共类MyMapView扩展了MapView {

    public void setBoundingBox(BoundingBoxE6 boundingBox) {
        this.boundingBox = boundingBox;
    }

    private BoundingBoxE6 boundingBox = null;

    ...

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

所以,在我的片段中,我初始化boundingBox:

@Override
public void onResume() {
    super.onResume();

    final BoundingBoxE6 boundingBox = new BoundingBoxE6( ... );
    // this gives ``N:44899816; E:5020385; S:44899001; W:5019482``

    mMapView.setBoundingBox(boundingBox);

    mMapView.invalidate();

}

因此,计算出的边界框显然会导致获得最大缩放级别但不会。

首先,计算中的问题;在onLayout()之后调用onResume(),我浏览了zoomToBoundingBox()的代码,以下是每个计算数据的结果:

zoomLevel = 0
maxZoomLatitudeSpan = 55.08
requiredLatitudeZoom = 14.0  // I have a big doubt on this result!
maxZoomLongitudeSpan = 472.07
requiredLongitudeZoom = 17.0  // hum well, better but not best expected value

所以zoomToBoundingBox()会选择14.0的缩放级别为什么? 无论如何,17.0的requiredLongitudeZoom不是最好的结果。 如您所见here,最高级别为18.0是可能的。

那么我应该如何使用zoomToBoundingBox()

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题this.mMapView.zoomToBoundingBox(boundingBox);导致某些情况下的次优缩放级别,所以我实现了自己的版本。

改编自LocationMapFragment.java

private void zoomToBoundingBox(BoundingBoxE6 boundingBox) {
    GeoPoint min = new GeoPoint(boundingBox.getLatSouthE6(), boundingBox.getLonWestE6());

    GeoPoint max = new GeoPoint(boundingBox.getLatNorthE6(), boundingBox.getLonEastE6());
    ZoomUtil.zoomTo(this.mMapView, min, max);
    // this.mMapView.zoomToBoundingBox(boundingBox); this is to inexact
}

改编自ZoomUtil.java

public class ZoomUtil {
    public static void zoomTo(MapView mapView, IGeoPoint min, IGeoPoint max) {
        MapTileProviderBase tileProvider = mapView.getTileProvider();
        IMapController controller = mapView.getController();
        IGeoPoint center = new GeoPoint((max.getLatitudeE6() + min.getLatitudeE6()) / 2, (max.getLongitudeE6() + min.getLongitudeE6()) / 2);

        // diagonale in pixels
        double pixels = Math.sqrt((mapView.getWidth() * mapView.getWidth()) + (mapView.getHeight() * mapView.getHeight()));
        final double requiredMinimalGroundResolutionInMetersPerPixel = ((double) new GeoPoint(min.getLatitudeE6(), min.getLongitudeE6()).distanceTo(max)) / pixels;
        int zoom = calculateZoom(center.getLatitude(), requiredMinimalGroundResolutionInMetersPerPixel, tileProvider.getMaximumZoomLevel(), tileProvider.getMinimumZoomLevel());
        controller.setZoom(zoom);
        controller.setCenter(center);
    }

    private static int calculateZoom(double latitude, double requiredMinimalGroundResolutionInMetersPerPixel, int maximumZoomLevel, int minimumZoomLevel) {
        for (int zoom = maximumZoomLevel; zoom >= minimumZoomLevel; zoom--) {
            if (TileSystem.GroundResolution(latitude, zoom) > requiredMinimalGroundResolutionInMetersPerPixel)
                return zoom;
        }

        return NO_ZOOM;
    }
}

请注意,该代码仅在mapview完全初始化时有效,否则地面分辨率的计算失败或产生错误结果