Changeset 9537 in osm for applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java
- Timestamp:
- 2008-08-07T15:33:50+02:00 (17 years ago)
- Location:
- applications/editors/josm/plugins/slippy_map_chooser
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
src/SlippyMapChooser.java (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/slippy_map_chooser
-
Property svn:ignore
set to
build
-
Property svn:ignore
set to
-
applications/editors/josm/plugins/slippy_map_chooser/src/SlippyMapChooser.java
r9159 r9537 1 1 // This code has been adapted and copied from code that has been written by Immanuel Scholz and others for JOSM. 2 2 // License: GPL. Copyright 2007 by Tim Haussmann 3 4 import static org.openstreetmap.josm.tools.I18n.tr; 3 5 4 6 import java.awt.BorderLayout; … … 9 11 import java.awt.Point; 10 12 import java.awt.Toolkit; 11 import java. util.Enumeration;12 13 import javax.swing.JComponent; 13 import java.awt.geom.Point2D; 14 import java.util.Vector; 15 14 16 import javax.swing.JLabel; 15 17 import javax.swing.JPanel; 16 18 17 import org.openstreetmap.josm.data.coor.LatLon; 19 import org.openstreetmap.gui.jmapviewer.JMapViewer; 20 import org.openstreetmap.gui.jmapviewer.MapMarkerDot; 21 import org.openstreetmap.gui.jmapviewer.OsmMercator; 22 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 18 23 import org.openstreetmap.josm.gui.download.DownloadDialog; 19 24 import org.openstreetmap.josm.gui.download.DownloadSelection; 20 import static org.openstreetmap.josm.tools.I18n.tr;21 25 22 26 /** 23 * JComponent that displays the slippy map tiles 27 * JComponent that displays the slippy map tiles 28 * 24 29 * @author Tim Haussmann 25 * 30 * 26 31 */ 27 public class SlippyMapChooser extends JComponent implements DownloadSelection{ 28 29 private static final int MAX_ZOOMLEVEL = 20; 30 private static final int MIN_ZOOMLEVEL = 1; 31 32 private TileDB iTileDB; 33 32 public class SlippyMapChooser extends JMapViewer implements DownloadSelection { 33 34 34 private DownloadDialog iGui; 35 36 //the upper left and lower right corners of the selection rectangle 35 36 // upper left and lower right corners of the selection rectangle (x/y on 37 // ZOOM_MAX) 37 38 Point iSelectionRectStart; 38 39 Point iSelectionRectEnd; 39 40 //Offsets for x and y (i.e. to center the first tile) 41 int iOffsetX = 0; 42 int iOffsetY = 0; 43 44 //the zoom level of the currently shown tiles 45 static int iZoomlevel = 3; 46 47 private boolean iFirstPaint = true; 48 private LatLon iFirstPaintCenter = new LatLon(51,7); 49 40 50 41 private SizeButton iSizeButton = new SizeButton(); 51 52 //standard dimension 42 43 // standard dimension 53 44 private Dimension iDownloadDialogDimension; 54 //screen size 45 // screen size 55 46 private Dimension iScreenSize; 56 57 private LatLon iScreenCenterBeforeResize; 58 private LatLon iSelectionStartBeforeResize; 59 private LatLon iSelectionEndBeforeResize; 60 private boolean isJustResized = false; 61 62 private int iVisibleTilesX = 2; 63 private int iVisibleTilesY = 3; 64 47 65 48 /** 66 49 * Create the chooser component. 67 50 */ 68 public SlippyMapChooser() { 69 70 //create the tile db 71 iTileDB = new TileDB(this); 72 73 74 75 setMinimumSize(new Dimension(350, 350/2)); 51 public SlippyMapChooser() { 52 super(); 53 setZoomContolsVisible(false); 54 setMapMarkerVisible(false); 55 setMinimumSize(new Dimension(350, 350 / 2)); 76 56 } 77 57 … … 81 61 temp.setLayout(new BorderLayout()); 82 62 temp.add(this, BorderLayout.CENTER); 83 temp.add(new JLabel((tr("Zoom: Mousewheel or double click. Move map: Hold right mousebutton and move mouse. Select: Click."))), BorderLayout.SOUTH); 63 temp.add(new JLabel((tr("Zoom: Mousewheel or double click. " 64 + "Move map: Hold right mousebutton and move mouse. Select: Click."))), 65 BorderLayout.SOUTH); 84 66 iGui.tabpane.add(temp, tr("Slippy map")); 85 86 new OsmMapControl(this,temp, iSizeButton); 67 68 new OsmMapControl(this, temp, iSizeButton); 69 boundingBoxChanged(gui); 70 } 71 72 protected Point getTopLeftCoordinates() { 73 return new Point(center.x - (getWidth() / 2), center.y - (getHeight() / 2)); 74 } 75 76 /** 77 * Draw the map. 78 */ 79 @Override 80 public void paint(Graphics g) { 81 try { 82 super.paint(g); 83 84 // draw selection rectangle 85 if (iSelectionRectStart != null && iSelectionRectEnd != null) { 86 87 int zoomDiff = MAX_ZOOM - zoom; 88 Point tlc = getTopLeftCoordinates(); 89 int x_min = (iSelectionRectStart.x >> zoomDiff) - tlc.x; 90 int y_min = (iSelectionRectStart.y >> zoomDiff) - tlc.y; 91 int x_max = (iSelectionRectEnd.x >> zoomDiff) - tlc.x; 92 int y_max = (iSelectionRectEnd.y >> zoomDiff) - tlc.y; 93 94 int w = x_max - x_min; 95 int h = y_max - y_min; 96 g.setColor(new Color(0.9f, 0.7f, 0.7f, 0.6f)); 97 g.fillRect(x_min, y_min, w, h); 98 99 g.setColor(Color.BLACK); 100 g.drawRect(x_min, y_min, w, h); 101 102 } 103 104 iSizeButton.paint(g); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 } 109 110 public void boundingBoxChanged(DownloadDialog gui) { 111 112 // test if a bounding box has been set set 113 if (gui.minlat == 0.0 && gui.minlon == 0.0 && gui.maxlat == 0.0 && gui.maxlon == 0.0) 114 return; 115 116 int y1 = OsmMercator.LatToY(gui.minlat, MAX_ZOOM); 117 int y2 = OsmMercator.LatToY(gui.maxlat, MAX_ZOOM); 118 int x1 = OsmMercator.LonToX(gui.minlon, MAX_ZOOM); 119 int x2 = OsmMercator.LonToX(gui.maxlon, MAX_ZOOM); 120 121 iSelectionRectStart = new Point(Math.min(x1, x2), Math.min(y1, y2)); 122 iSelectionRectEnd = new Point(Math.max(x1, x2), Math.max(y1, y2)); 123 124 // calc the screen coordinates for the new selection rectangle 125 MapMarkerDot xmin_ymin = new MapMarkerDot(gui.minlat, gui.minlon); 126 MapMarkerDot xmax_ymax = new MapMarkerDot(gui.maxlat, gui.maxlon); 127 128 Vector<MapMarker> marker = new Vector<MapMarker>(2); 129 marker.add(xmin_ymin); 130 marker.add(xmax_ymax); 131 setMapMarkerList(marker); 132 setDisplayToFitMapMarkers(); 133 zoomOut(); 134 } 135 136 /** 137 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the 138 * selection rectangle. 139 * 140 * @param aStart 141 * @param aEnd 142 */ 143 public void setSelection(Point aStart, Point aEnd) { 144 if (aStart == null || aEnd == null) 145 return; 146 Point p_max = new Point(Math.max(aEnd.x, aStart.x), Math.max(aEnd.y, aStart.y)); 147 Point p_min = new Point(Math.min(aEnd.x, aStart.x), Math.min(aEnd.y, aStart.y)); 148 149 Point tlc = getTopLeftCoordinates(); 150 int zoomDiff = MAX_ZOOM - zoom; 151 Point pEnd = new Point(p_max.x + tlc.x, p_max.y + tlc.y); 152 Point pStart = new Point(p_min.x + tlc.x, p_min.y + tlc.y); 153 154 pEnd.x <<= zoomDiff; 155 pEnd.y <<= zoomDiff; 156 pStart.x <<= zoomDiff; 157 pStart.y <<= zoomDiff; 158 159 iSelectionRectStart = pStart; 160 iSelectionRectEnd = pEnd; 161 162 Point2D.Double l1 = getPosition(p_max); 163 Point2D.Double l2 = getPosition(p_min); 164 iGui.minlat = Math.min(l2.x, l1.x); 165 iGui.minlon = Math.min(l1.y, l2.y); 166 iGui.maxlat = Math.max(l2.x, l1.x); 167 iGui.maxlon = Math.max(l1.y, l2.y); 168 169 iGui.boundingBoxChanged(this); 87 170 repaint(); 88 } 89 90 /** 91 * Performs resizing of the DownloadDialog in order to enlarge or shrink the map. 92 */ 93 public void resizeSlippyMap(){ 94 if(iScreenSize == null){ 95 Component c = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent(); 96 //remember the initial set screen dimensions 171 } 172 173 /** 174 * Performs resizing of the DownloadDialog in order to enlarge or shrink the 175 * map. 176 */ 177 public void resizeSlippyMap() { 178 if (iScreenSize == null) { 179 Component c = 180 iGui.getParent().getParent().getParent().getParent().getParent().getParent() 181 .getParent().getParent().getParent(); 182 // remember the initial set screen dimensions 97 183 iDownloadDialogDimension = c.getSize(); 98 //retrive the size of the display 184 // retrive the size of the display 99 185 iScreenSize = Toolkit.getDefaultToolkit().getScreenSize(); 100 186 } 101 102 //remember the screen center (we want to have the same center after resizing) 103 iScreenCenterBeforeResize = getLatLonOfScreenPoint(new Point(getWidth()/2, getHeight()/2)); 104 105 //remember lat/lon of the selection rectangle 106 if(iSelectionRectEnd != null && iSelectionRectStart != null){ 107 iSelectionStartBeforeResize = getLatLonOfScreenPoint(iSelectionRectStart); 108 iSelectionEndBeforeResize = getLatLonOfScreenPoint(iSelectionRectEnd); 109 } 110 111 //resize 112 Component co = iGui.getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent().getParent(); 187 188 // resize 189 Component co = 190 iGui.getParent().getParent().getParent().getParent().getParent().getParent() 191 .getParent().getParent().getParent(); 113 192 Dimension currentDimension = co.getSize(); 114 115 //enlarge 116 if(currentDimension.equals(iDownloadDialogDimension)){ 117 //make the each dimension 90% of the absolute display size and center the DownloadDialog 118 int w = iScreenSize.width*90/100; 119 int h = iScreenSize.height*90/100; 120 co.setBounds((iScreenSize.width-w)/2, (iScreenSize.height-h)/2, w, h); 121 } 122 //shrink 123 else{ 124 //set the size back to the initial dimensions and center the DownloadDialog 193 194 // enlarge 195 if (currentDimension.equals(iDownloadDialogDimension)) { 196 // make the each dimension 90% of the absolute display size and 197 // center the DownloadDialog 198 int w = iScreenSize.width * 90 / 100; 199 int h = iScreenSize.height * 90 / 100; 200 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h); 201 } 202 // shrink 203 else { 204 // set the size back to the initial dimensions and center the 205 // DownloadDialog 125 206 int w = iDownloadDialogDimension.width; 126 207 int h = iDownloadDialogDimension.height; 127 co.setBounds((iScreenSize.width-w)/2, (iScreenSize.height-h)/2, w, h); 128 } 129 130 //the new dimension are 'available' after (or while) the next repaint 131 isJustResized = true; 132 208 co.setBounds((iScreenSize.width - w) / 2, (iScreenSize.height - h) / 2, w, h); 209 } 133 210 repaint(); 134 211 } 135 212 136 /**137 * Draw the map.138 */139 @Override public void paint(Graphics g) {140 141 if(iFirstPaint){142 //calculate numbers of visible tiles143 calcVisibleTiles();144 145 //save selection146 LatLon selStart = null;147 LatLon selEnd = null;148 if(iSelectionRectEnd != null && iSelectionRectStart != null){149 selStart = getLatLonOfScreenPoint(iSelectionRectStart);150 selEnd = getLatLonOfScreenPoint(iSelectionRectEnd);151 }152 centerOnLatLon(iFirstPaintCenter);153 //restore selection154 if(selStart != null && selEnd != null){155 iSelectionRectStart = getScreenPointForLatLon(selStart);156 iSelectionRectEnd = getScreenPointForLatLon(selEnd);157 }158 159 loadVisibleTiles();160 iFirstPaint = false;161 repaint();162 }163 164 if(isJustResized){165 centerOnLatLon(iScreenCenterBeforeResize);166 //restore selection167 if(iSelectionEndBeforeResize != null && iSelectionStartBeforeResize != null){168 iSelectionRectStart = getScreenPointForLatLon(iSelectionStartBeforeResize);169 iSelectionRectEnd = getScreenPointForLatLon(iSelectionEndBeforeResize);170 }171 172 //calculate numbers of visible tiles173 calcVisibleTiles();174 175 loadVisibleTiles();176 isJustResized = false;177 }178 179 //translate origin to draw map tiles in 'map space'180 g.translate(iOffsetX, iOffsetY);181 182 //draw tiles of the current zoomlevel183 for(int y=0; y<iVisibleTilesY; y++){184 for(int x=0; x<iVisibleTilesX; x++){185 OsmTile t = iTileDB.get(OsmTile.key(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH, ((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT)));186 if(t != null){187 t.paint(g,iTileDB);188 }189 }190 }191 192 //translate origin back193 g.translate(-iOffsetX, -iOffsetY);194 195 //draw selection rectangle196 if(iSelectionRectStart != null && iSelectionRectEnd != null){197 198 g.setColor(Color.black);199 g.drawRect(iSelectionRectStart.x, iSelectionRectStart.y, iSelectionRectEnd.x -iSelectionRectStart.x, iSelectionRectEnd.y -iSelectionRectStart.y);200 201 g.setColor(new Color(0.9f,0.7f,0.7f,0.6f));202 g.fillRect(iSelectionRectStart.x+1, iSelectionRectStart.y+1, iSelectionRectEnd.x -iSelectionRectStart.x-1, iSelectionRectEnd.y -iSelectionRectStart.y-1);203 }204 205 iSizeButton.paint(g);206 207 208 if(SlippyMapChooserPlugin.DEBUG_MODE){209 g.setColor(Color.black);210 g.drawString("Free Memory: " + Runtime.getRuntime().freeMemory()/1024 + "/" + Runtime.getRuntime().totalMemory()/1024 + "kB" , 5, 50);211 g.drawString("Tiles in DB: " + iTileDB.getCachedTilesSize(), 5, 65);212 g.drawString("Loading Queue Size: " + iTileDB.getLoadingQueueSize(), 5, 80);213 214 }215 }216 217 218 public void boundingBoxChanged(DownloadDialog gui) {219 220 //calc the screen coordinates for the new selection rectangle221 int x1 = OsmMercator.LonToX(gui.minlon, iZoomlevel);222 int x2 = OsmMercator.LonToX(gui.maxlon, iZoomlevel);223 int y1 = OsmMercator.LatToY(gui.minlat, iZoomlevel);224 int y2 = OsmMercator.LatToY(gui.maxlat, iZoomlevel);225 226 iSelectionRectStart = new Point(Math.min(x1, x2)+iOffsetX, Math.min(y1, y2)+iOffsetY);227 iSelectionRectEnd = new Point(Math.max(x1, x2)+iOffsetX, Math.max(y1, y2)+iOffsetY);228 229 //calc zoom level230 double dLat = Math.abs(gui.maxlat - gui.minlat);231 double dLon = Math.abs(gui.maxlon - gui.minlon);232 int zoomLat = (int) (Math.log(90 / dLat)/Math.log(2));233 int zoomLon = (int) (Math.log(90 / dLon)/Math.log(2));234 iZoomlevel = Math.max(zoomLat, zoomLon);235 236 //center on the rectangle237 if(gui.minlat != 0 && gui.maxlat != 0 && gui.minlon != 0 && gui.maxlat != 0){238 iFirstPaintCenter = new LatLon((gui.minlat + gui.maxlat)/2, (gui.minlon + gui.maxlon)/2);239 iFirstPaint = true;240 }241 repaint();242 }243 244 /**245 * Loads all tiles that are visible246 */247 void loadVisibleTiles(){248 for(int y=iVisibleTilesY-1; y>=0; y--){249 for(int x=0; x<iVisibleTilesX; x++){250 if(y > 0 && y < iVisibleTilesX-2){251 iTileDB.loadTile(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH,((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT), TileDB.PRIO_HIGH );252 }else{253 iTileDB.loadTile(iZoomlevel, (-iOffsetX + x*OsmTile.WIDTH)/OsmTile.WIDTH,((-iOffsetY+ y*OsmTile.HEIGHT)/OsmTile.HEIGHT), TileDB.PRIO_LOW );254 }255 }256 }257 }258 259 /**260 * Callback for the OsmMapControl. (Re-)Sets the start and end point of the selection rectangle.261 * @param aStart262 * @param aEnd263 */264 void setSelection(Point aStart, Point aEnd){265 if(aStart == null || aEnd == null)266 return;267 iSelectionRectEnd = new Point(Math.max(aEnd.x , aStart.x ) ,Math.max(aEnd.y, aStart.y ));268 iSelectionRectStart = new Point(Math.min(aEnd.x , aStart.x ) ,Math.min(aEnd.y , aStart.y ));269 270 LatLon l1 = getLatLonOfScreenPoint(aStart);271 LatLon l2 = getLatLonOfScreenPoint(aEnd);272 273 iGui.minlat = Math.min(l1.lat(), l2.lat());274 iGui.minlon = Math.min(l1.lon(), l2.lon());275 iGui.maxlat = Math.max(l1.lat(), l2.lat());276 iGui.maxlon = Math.max(l1.lon(), l2.lon());277 278 iGui.boundingBoxChanged(this);279 280 repaint();281 }282 283 /**284 * Callback for OsmMapControll. Moves the map and the selection rectangle.285 * @param x number of pixels to move along the x-axis (longitude)286 * @param y number of pixels to move along the y axis (latitude)287 */288 void moveMap(int x, int y) {289 int moveX = x;290 int moveY = y;291 292 int tempOffsetX = iOffsetX - x;293 int tempOffsetY = iOffsetY - y;294 295 296 int maxPixels = OsmMercator.getMaxPixels(iZoomlevel);297 298 if(moveX != 0){299 //deactivate dragging if the map is smaller than the DownloadDialog300 if(maxPixels < getWidth()){301 //center map horizontally302 iOffsetX = (getWidth()-maxPixels)/2;303 moveX = 0;304 }else{305 //don't allow the map to hide outside the JComponent drawing area306 if(tempOffsetX > 30){307 if(moveX < 0){308 moveX = 0;309 iOffsetX = 30;310 }311 }else if(-tempOffsetX > maxPixels + 30 - getWidth()){312 if(moveX > 0){313 moveX = 0;314 iOffsetX = -(maxPixels + 30 - getWidth());315 }316 }317 }318 }319 320 if(moveY != 0){321 //deactivate dragging if the map is smaller than the DownloadDialog322 if(maxPixels < getHeight()){323 //center map vertically324 iOffsetY = (getHeight()-maxPixels)/2;325 moveY = 0;326 }else{327 //don't allow the map to hide outside the JComponent drawing area328 if(tempOffsetY > 30){329 if(moveY < 0){330 moveY = 0;331 iOffsetY = 30;332 }333 }else if(-tempOffsetY > maxPixels + 30 - getHeight()){334 if(moveY > 0){335 moveY = 0;336 iOffsetY = -(maxPixels + 30 - getHeight());337 }338 }339 }340 }341 342 343 344 345 346 //execute the movement347 iOffsetX -= moveX;348 iOffsetY -= moveY;349 350 //also move the selection rect351 if(iSelectionRectEnd != null && iSelectionRectStart != null){352 iSelectionRectEnd.x -= moveX;353 iSelectionRectEnd.y -= moveY;354 iSelectionRectStart.x -= moveX;355 iSelectionRectStart.y -= moveY;356 }357 358 loadVisibleTiles();359 360 repaint();361 }362 363 /**364 * Zoom in one level365 * Callback for OsmMapControl. Zoom out one level366 */367 void zoomIn(Point curPos){368 369 //cache center of screen and the selection rectangle370 LatLon l = getLatLonOfScreenPoint(curPos);371 LatLon selStart = null;372 LatLon selEnd = null;373 if(iSelectionRectEnd != null && iSelectionRectStart != null){374 selStart = getLatLonOfScreenPoint(iSelectionRectStart);375 selEnd = getLatLonOfScreenPoint(iSelectionRectEnd);376 }377 378 //increment zoom level379 iZoomlevel += 1;380 if(iZoomlevel > MAX_ZOOMLEVEL){381 iZoomlevel = MAX_ZOOMLEVEL;382 return;383 }384 385 setLatLonAtPoint(l, curPos);386 387 //restore selection388 if(selStart != null && selEnd != null){389 iSelectionRectStart = getScreenPointForLatLon(selStart);390 iSelectionRectEnd = getScreenPointForLatLon(selEnd);391 }392 393 loadVisibleTiles();394 395 centerMap();396 397 repaint();398 }399 400 /**401 * Zoom out one level.402 * Callback for OsmMapControl.403 */404 void zoomOut(Point curPos){405 //cache center of screen and the selction rect406 LatLon l = getLatLonOfScreenPoint(curPos);407 LatLon selStart = null;408 LatLon selEnd = null;409 if(iSelectionRectEnd != null && iSelectionRectStart != null){410 selStart = getLatLonOfScreenPoint(iSelectionRectStart);411 selEnd = getLatLonOfScreenPoint(iSelectionRectEnd);412 }413 414 //decrement zoom level415 iZoomlevel -= 1;416 if(iZoomlevel < MIN_ZOOMLEVEL){417 iZoomlevel = MIN_ZOOMLEVEL;418 return;419 }420 421 setLatLonAtPoint(l, curPos);422 423 //restore selection424 if(selStart != null && selEnd != null){425 iSelectionRectStart = getScreenPointForLatLon(selStart);426 iSelectionRectEnd = getScreenPointForLatLon(selEnd);427 }428 429 loadVisibleTiles();430 431 centerMap();432 433 repaint();434 }435 436 /**437 * Calculates the latitude and longitude of a Point given in map space438 * @param aPoint in pixels on the map439 * @return the LatLon of the given Point440 */441 private LatLon getLatLonOfPoint(Point aPoint){442 return new LatLon(OsmMercator.YToLat(aPoint.y, iZoomlevel),OsmMercator.XToLon(aPoint.x, iZoomlevel));443 }444 445 /**446 * Returns the map coordinates for a LatLon447 * @param aLatLon448 * @return449 */450 private Point getPointForLatLon(LatLon aLatLon){451 Point p = new Point();452 p.y = OsmMercator.LatToY(aLatLon.lat(), iZoomlevel);453 p.x = OsmMercator.LonToX(aLatLon.lon(), iZoomlevel);454 return p;455 }456 457 /**458 * Calculates the latitude and longitude of a Point given in screen space (in the JComponent)459 * @param aPoint in Pixels on the screen (the JComponent)460 * @return the LatLon of the given Point461 */462 LatLon getLatLonOfScreenPoint(Point aPoint){463 Point mapCoordinates = new Point(aPoint);464 mapCoordinates.x -= iOffsetX;465 mapCoordinates.y -= iOffsetY;466 return getLatLonOfPoint(mapCoordinates);467 }468 469 /**470 * Calculates the screen coordinates of a LatLon.471 * @param aLatLon472 * @return the coordinates as Point in the JComponent473 */474 Point getScreenPointForLatLon(LatLon aLatLon){475 Point p = getPointForLatLon(aLatLon);476 p.x += iOffsetX;477 p.y += iOffsetY;478 return p;479 }480 481 482 /**483 * Centers the map on a Point484 * @param aPoint in screen coordinates (JComponent)485 */486 void centerOnScreenPoint(Point aPoint){487 moveMap(aPoint.x - getWidth()/2, aPoint.y-getHeight()/2);488 }489 490 /**491 * Centers the map on the location given by LatLon492 * @param aLatLon the location to center on493 */494 private void centerOnLatLon(LatLon aLatLon){495 setLatLonAtPoint(aLatLon, new Point(getWidth()/2,getHeight()/2));496 }497 498 /**499 * Moves the map that the specified latLon is shown at the point on screen500 * given501 * @param aLatLon a position502 * @param p a point on the screen503 */504 private void setLatLonAtPoint(LatLon aLatLon, Point p){505 int x = OsmMercator.LonToX(aLatLon.lon(), iZoomlevel);506 int y = OsmMercator.LatToY(aLatLon.lat(), iZoomlevel);507 iOffsetX = - x + p.x;508 iOffsetY = - y + p.y;509 repaint();510 }511 512 /**513 * Caclulates the visible tiles for each dimension514 */515 private void calcVisibleTiles(){516 if(iGui != null){517 iVisibleTilesX = iGui.getWidth()/256 + 2;518 iVisibleTilesY = iGui.getHeight()/256 + 2;519 }520 }521 522 /**523 * Centers the map after zooming. I.e. when the map is smaller than the524 * component it is shown in.525 */526 private void centerMap(){527 528 int maxPixels = OsmMercator.getMaxPixels(iZoomlevel);529 530 if(maxPixels < getWidth()){531 //center map horizontally532 iOffsetX = (getWidth()-maxPixels)/2;533 }534 if(maxPixels < getHeight()){535 //center map vertically536 iOffsetY = (getHeight()-maxPixels)/2;537 }538 }539 213 }
Note:
See TracChangeset
for help on using the changeset viewer.
