@Override public void windowOpened(WindowEvent e) { final Model model = mapView.getModel(); model.init(preferencesFacade); if (model.mapViewPosition.getZoomLevel() == 0 || !boundingBox.contains(model.mapViewPosition.getCenter())) { byte zoomLevel = LatLongUtils.zoomForBounds(model.mapViewDimension.getDimension(), boundingBox, model.displayModel.getTileSize()); model.mapViewPosition.setMapPosition(new MapPosition(boundingBox.getCenterPoint(), zoomLevel)); } } });
@Test public void zoomForBoundsTest() { // TODO rewrite this unit tests to make it easier to understand Dimension[] dimensions = {new Dimension(200, 300), new Dimension(500, 400), new Dimension(1000, 600), new Dimension(3280, 1780), new Dimension(100, 200), new Dimension(500, 200)}; BoundingBox[] boundingBoxes = {new BoundingBox(12.2, 0, 34.3, 120), new BoundingBox(-30, 20, 30, 30), new BoundingBox(20.3, 100, 30.4, 120), new BoundingBox(4.4, 2, 4.5, 2.2), new BoundingBox(50.43, 12.23, 50.44, 12.24), new BoundingBox(50.43, 12, 50.44, 40)}; int[] tileSizes = {256, 512, 500, 620, 451}; byte[] results = {1, 0, 0, 0, 0, 2, 1, 1, 1, 1, 3, 2, 2, 2, 2, 10, 9, 9, 9, 9, 14, 13, 13, 13, 13, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 3, 2, 2, 1, 2, 5, 4, 4, 3, 4, 11, 10, 10, 10, 10, 15, 14, 14, 13, 14, 4, 3, 3, 3, 3, 3, 2, 2, 2, 2, 3, 2, 2, 2, 2, 6, 5, 5, 4, 5, 12, 11, 11, 11, 11, 15, 14, 14, 14, 14, 5, 4, 4, 4, 4, 5, 4, 4, 3, 4, 5, 4, 4, 4, 4, 7, 6, 6, 6, 6, 14, 13, 13, 13, 13, 17, 16, 16, 16, 16, 7, 6, 6, 6, 6, 0, 0, 0, 0, 0, 2, 1, 1, 0, 1, 2, 1, 1, 1, 1, 9, 8, 8, 8, 8, 13, 12, 12, 12, 12, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 0, 1, 4, 3, 3, 3, 3, 11, 10, 10, 10, 10, 14, 13, 13, 12, 13, 4, 3, 3, 3, 3}; int i = 0; for (Dimension dimension : dimensions) { for (BoundingBox boundingBox : boundingBoxes) { for (int tileSize : tileSizes) { Assert.assertEquals(results[i], LatLongUtils.zoomForBounds(dimension, boundingBox, tileSize)); ++i; } } } } }
private void zoomToBounds(org.mapsforge.core.model.BoundingBox boundingBox) { Dimension dimension = mapView.getModel().mapViewDimension.getDimension(); if (dimension == null) return; byte zoom = zoomForBounds(dimension, boundingBox, getTileSize()); setZoom(zoom); }
private void limitZoomLevel() { LocalMap map = mapsToLayers.keySet().iterator().next(); byte zoomLevelMin = map.isVector() ? MINIMUM_ZOOM_LEVEL : map.getTileSource().getZoomLevelMin(); // limit minimum zoom to prevent zooming out too much and losing the map MapViewDimension mapViewDimension = mapView.getModel().mapViewDimension; if (map.isVector() && mapViewDimension.getDimension() != null) zoomLevelMin = (byte) max(0, zoomForBounds(mapViewDimension.getDimension(), asBoundingBox(map.getBoundingBox()), getTileSize()) - 3); IMapViewPosition mapViewPosition = mapView.getModel().mapViewPosition; mapViewPosition.setZoomLevelMin(zoomLevelMin); byte zoomLevelMax = map.isVector() ? MAXIMUM_ZOOM_LEVEL : map.getTileSource().getZoomLevelMax(); // limit maximum to prevent zooming in to grey area mapViewPosition.setZoomLevelMax(zoomLevelMax); }
@Override protected MapPosition getInitialPosition() { int tileSize = this.mapView.getModel().displayModel.getTileSize(); byte zoomLevel = LatLongUtils.zoomForBounds(new Dimension(tileSize * 4, tileSize * 4), getMapFile().boundingBox(), tileSize); return new MapPosition(getMapFile().boundingBox().getCenterPoint(), zoomLevel); }
@Override protected MapPosition getInitialPosition() { int tileSize = this.mapView.getModel().displayModel.getTileSize(); byte zoomLevel = LatLongUtils.zoomForBounds(new Dimension(tileSize * 4, tileSize * 4), getMapFile().boundingBox(), tileSize); return new MapPosition(getMapFile().boundingBox().getCenterPoint(), zoomLevel); }
@Override public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); if (hasFocus) { BoundingBox bb = new BoundingBox(latLong2.latitude, latLong3.longitude, latLong3.latitude, latLong2.longitude); Dimension dimension = this.mapView.getModel().mapViewDimension.getDimension(); this.mapView.getModel().mapViewPosition.setMapPosition(new MapPosition( bb.getCenterPoint(), LatLongUtils.zoomForBounds( dimension, bb, this.mapView.getModel().displayModel.getTileSize()))); } }