/** * Converts a tile X number at a certain zoom level to a longitude coordinate. * * @param tileX the tile X number that should be converted. * @param zoomLevel the zoom level at which the number should be converted. * @return the longitude value of the tile X number. */ public static double tileXToLongitude(long tileX, byte zoomLevel) { return pixelXToLongitude(tileX * DUMMY_TILE_SIZE, getMapSize(zoomLevel, DUMMY_TILE_SIZE)); }
@Test public void pixelXToLongitudeTest() { for (int tileSize : TILE_SIZES) { for (byte zoomLevel = ZOOM_LEVEL_MIN; zoomLevel <= ZOOM_LEVEL_MAX; ++zoomLevel) { long mapSize = MercatorProjection.getMapSize(zoomLevel, tileSize); double longitude = MercatorProjection.pixelXToLongitude(0, mapSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MIN, longitude, 0); longitude = MercatorProjection.pixelXToLongitudeWithScaleFactor(0, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MIN, longitude, 0); longitude = MercatorProjection.pixelXToLongitude((float) mapSize / 2, mapSize); Assert.assertEquals(0, longitude, 0); mapSize = MercatorProjection.getMapSizeWithScaleFactor(MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); longitude = MercatorProjection.pixelXToLongitudeWithScaleFactor((float) mapSize / 2, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(0, longitude, 0); longitude = MercatorProjection.pixelXToLongitude(mapSize, mapSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MAX, longitude, 0); longitude = MercatorProjection.pixelXToLongitudeWithScaleFactor(mapSize, MercatorProjection.zoomLevelToScaleFactor(zoomLevel), tileSize); Assert.assertEquals(LatLongUtils.LONGITUDE_MAX, longitude, 0); } verifyInvalidPixelXToLongitude(-1, (byte) 0, tileSize); verifyInvalidPixelXToLongitude(tileSize + 1, (byte) 0, tileSize); } }
/** * Get LatLong from Pixels. */ public static LatLong fromPixels(double pixelX, double pixelY, long mapSize) { return new LatLong(pixelYToLatitude(pixelY, mapSize), pixelXToLongitude(pixelX, mapSize)); }
public static BoundingBox getBoundingBox(MapPosition mapPosition, Dimension canvasDimension, int tileSize) { long mapSize = MercatorProjection.getMapSize(mapPosition.zoomLevel, tileSize); double pixelX = MercatorProjection.longitudeToPixelX(mapPosition.latLong.longitude, mapSize); double pixelY = MercatorProjection.latitudeToPixelY(mapPosition.latLong.latitude, mapSize); int halfCanvasWidth = canvasDimension.width / 2; int halfCanvasHeight = canvasDimension.height / 2; double pixelXMin = Math.max(0, pixelX - halfCanvasWidth); double pixelYMin = Math.max(0, pixelY - halfCanvasHeight); double pixelXMax = Math.min(mapSize, pixelX + halfCanvasWidth); double pixelYMax = Math.min(mapSize, pixelY + halfCanvasHeight); double minLatitude = MercatorProjection.pixelYToLatitude(pixelYMax, mapSize); double minLongitude = MercatorProjection.pixelXToLongitude(pixelXMin, mapSize); double maxLatitude = MercatorProjection.pixelYToLatitude(pixelYMin, mapSize); double maxLongitude = MercatorProjection.pixelXToLongitude(pixelXMax, mapSize); return new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude); }
/** * Moves the center position of the map by the given amount of pixels. * * @param moveHorizontal the amount of pixels to move this MapViewPosition horizontally. * @param moveVertical the amount of pixels to move this MapViewPosition vertically. * @param zoomLevelDiff the difference in desired zoom level. * @param animated whether the move should be animated. */ public void moveCenterAndZoom(double moveHorizontal, double moveVertical, byte zoomLevelDiff, boolean animated) { synchronized (this) { long mapSize = MercatorProjection.getMapSize(this.zoomLevel, this.displayModel.getTileSize()); double pixelX = MercatorProjection.longitudeToPixelX(this.longitude, mapSize) - moveHorizontal; double pixelY = MercatorProjection.latitudeToPixelY(this.latitude, mapSize) - moveVertical; pixelX = Math.min(Math.max(0, pixelX), mapSize); pixelY = Math.min(Math.max(0, pixelY), mapSize); double newLatitude = MercatorProjection.pixelYToLatitude(pixelY, mapSize); double newLongitude = MercatorProjection.pixelXToLongitude(pixelX, mapSize); setCenterInternal(newLatitude, newLongitude); setZoomLevelInternal(this.zoomLevel + zoomLevelDiff, animated); } notifyObservers(); }
private static void verifyInvalidPixelXToLongitude(double pixelX, byte zoomLevel, int tileSize) { try { MercatorProjection.pixelXToLongitude(pixelX, MercatorProjection.getMapSize(zoomLevel, tileSize)); Assert.fail("pixelX: " + pixelX + ", zoomLevel: " + zoomLevel); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } }
double maptileLeftLng = MercatorProjection.pixelXToLongitude((long) origin.x, tile.mapSize); double maptileRightLng = MercatorProjection.pixelXToLongitude((long) origin.x + tile.tileSize, tile.mapSize);
MercatorProjection.pixelXToLongitude(pixelX + x, mapSize)); } catch (Exception e) { return null;
/** * Converts a tile X number at a certain zoom level to a longitude coordinate. * * @param tileX the tile X number that should be converted. * @param zoomLevel the zoom level at which the number should be converted. * @return the longitude value of the tile X number. */ public static double tileXToLongitude(long tileX, byte zoomLevel) { return pixelXToLongitude(tileX * DUMMY_TILE_SIZE, getMapSize(zoomLevel, DUMMY_TILE_SIZE)); }
/** * Get LatLong from Pixels. */ public static LatLong fromPixels(double pixelX, double pixelY, long mapSize) { return new LatLong(pixelYToLatitude(pixelY, mapSize), pixelXToLongitude(pixelX, mapSize)); }
public static BoundingBox getBoundingBox(MapPosition mapPosition, Dimension canvasDimension, int tileSize) { long mapSize = MercatorProjection.getMapSize(mapPosition.zoomLevel, tileSize); double pixelX = MercatorProjection.longitudeToPixelX(mapPosition.latLong.longitude, mapSize); double pixelY = MercatorProjection.latitudeToPixelY(mapPosition.latLong.latitude, mapSize); int halfCanvasWidth = canvasDimension.width / 2; int halfCanvasHeight = canvasDimension.height / 2; double pixelXMin = Math.max(0, pixelX - halfCanvasWidth); double pixelYMin = Math.max(0, pixelY - halfCanvasHeight); double pixelXMax = Math.min(mapSize, pixelX + halfCanvasWidth); double pixelYMax = Math.min(mapSize, pixelY + halfCanvasHeight); double minLatitude = MercatorProjection.pixelYToLatitude(pixelYMax, mapSize); double minLongitude = MercatorProjection.pixelXToLongitude(pixelXMin, mapSize); double maxLatitude = MercatorProjection.pixelYToLatitude(pixelYMin, mapSize); double maxLongitude = MercatorProjection.pixelXToLongitude(pixelXMax, mapSize); return new BoundingBox(minLatitude, minLongitude, maxLatitude, maxLongitude); }
/** * Moves the center position of the map by the given amount of pixels. * * @param moveHorizontal the amount of pixels to move this MapViewPosition horizontally. * @param moveVertical the amount of pixels to move this MapViewPosition vertically. * @param zoomLevelDiff the difference in desired zoom level. * @param animated whether the move should be animated. */ public void moveCenterAndZoom(double moveHorizontal, double moveVertical, byte zoomLevelDiff, boolean animated) { synchronized (this) { long mapSize = MercatorProjection.getMapSize(this.zoomLevel, this.displayModel.getTileSize()); double pixelX = MercatorProjection.longitudeToPixelX(this.longitude, mapSize) - moveHorizontal; double pixelY = MercatorProjection.latitudeToPixelY(this.latitude, mapSize) - moveVertical; pixelX = Math.min(Math.max(0, pixelX), mapSize); pixelY = Math.min(Math.max(0, pixelY), mapSize); double newLatitude = MercatorProjection.pixelYToLatitude(pixelY, mapSize); double newLongitude = MercatorProjection.pixelXToLongitude(pixelX, mapSize); setCenterInternal(newLatitude, newLongitude); setZoomLevelInternal(this.zoomLevel + zoomLevelDiff, animated); } notifyObservers(); }
double maptileLeftLng = MercatorProjection.pixelXToLongitude((long) origin.x, tile.mapSize); double maptileRightLng = MercatorProjection.pixelXToLongitude((long) origin.x + tile.tileSize, tile.mapSize);
MercatorProjection.pixelXToLongitude(pixelX + x, mapSize)); } catch (Exception e) { return null;