osmdroid 當前地圖不是全屏時候(縮放全圖測試)

  1、當你MapView 不是全屏時候,需要縮放到全圖或者固定位置又或者是圖層的點。你會發現MapView裏面的設置中心是錯的,你的設置縮放範圍也是錯的zoomToBoundingBox
  2、看源碼是因爲this.getWidth() 和  this.getHeight(),因爲裏面裏面使用的是全屏的窗口的值。就是說在windows裏面位置的值。所以不一樣
  3、所以你需要根據裏面方法,自已重寫就好

以下是源碼的zoomToBoundingBox

    public void zoomToBoundingBox(BoundingBox boundingBox, boolean animated, int borderSizeInPixels) {
        double nextZoom = mTileSystem.getBoundingBoxZoom(boundingBox, this.getWidth() - 2 * borderSizeInPixels, this.getHeight() - 2 * borderSizeInPixels);
        if (nextZoom != 4.9E-324D) {
            nextZoom = Math.min(this.getMaxZoomLevel(), Math.max(nextZoom, this.getMinZoomLevel()));
            IGeoPoint center = boundingBox.getCenterWithDateLine();
            if (animated) {
                this.getController().setCenter(center);
                this.getController().zoomTo(nextZoom);
            } else {
                this.getController().setZoom(nextZoom);
                this.getController().setCenter(center);
            }

        }
    }

自已重寫後

        double nextZoom = mapView.getTileSystem().getBoundingBoxZoom(simpleFastPointOverlay.getBoundingBox(), mapView.getWidth() - 2 * dip2px(10) , mapView.getHeight() - 2 * dip2px(10));
        if (nextZoom != 4.9E-324D) {
            nextZoom = Math.min(mapView.getMaxZoomLevel(), Math.max(nextZoom, mapView.getMinZoomLevel()));
            IGeoPoint center = simpleFastPointOverlay.getBoundingBox().getCenterWithDateLine();
            mapView.getController().setCenter(center);
            mapView.getController().zoomTo(nextZoom);
        }else {
            mapView.getController().setZoom(18.0);
            //移動到某個位置
            mapView.getController().animateTo(new GeoPoint(x, y));
        }
        mapView.invalidate();
     public static TileSystem getTileSystem() {
        return mTileSystem;
    }

    public static void setTileSystem(TileSystem pTileSystem) {
        mTileSystem = pTileSystem;
    }

這兩個方法在MapView

最後提示:

如果設置縮放到中心(setCenter)不行,就用
mapView.getController().animateTo(new GeoPoint(x, y));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章