Ticket #6869: jmapviewer.3.diff
File jmapviewer.3.diff, 17.7 KB (added by , 14 years ago) |
---|
-
OsmMercator.java
18 18 private static int TILE_SIZE = 256; 19 19 public static final double MAX_LAT = 85.05112877980659; 20 20 public static final double MIN_LAT = -85.05112877980659; 21 private static double EARTH_RADIUS_KM = 6371; 22 23 private static double KMTOM=1000; 24 private static double MTOKM=1/1000; 25 26 public static double kmToMeters(double kmeters) { 27 return kmeters*KMTOM; 28 } 29 30 public static double metersToKm(double meters) { 31 return meters*MTOKM; 32 } 21 33 22 34 public static double radius(int aZoomlevel) { 23 35 return (TILE_SIZE * (1 << aZoomlevel)) / (2.0 * Math.PI); … … 43 55 } 44 56 45 57 /** 58 * Transform pixelspace to coordinates and get the distance. 59 * 60 * @param x1 the first x coordinate 61 * @param y1 the first y coordinate 62 * @param x2 the second x coordinate 63 * @param y2 the second y coordinate 64 * 65 * @param zoomLevel the zoom level 66 * @return the distance 67 * @author Jason Huntley 68 */ 69 public static double getDistance(int x1, int y1, int x2, int y2, int zoomLevel) { 70 double la1 = YToLat(y1, zoomLevel); 71 double lo1 = XToLon(x1, zoomLevel); 72 double la2 = YToLat(y2, zoomLevel); 73 double lo2 = XToLon(x2, zoomLevel); 74 75 return getDistance(la1, lo1, la2, lo2); 76 } 77 78 /** 79 * Gets the distance using Spherical law of cosines. 80 * 81 * @param la1 the Latitude in degrees 82 * @param lo1 the Longitude in degrees 83 * @param la2 the Latitude from 2nd coordinate in degrees 84 * @param lo2 the Longitude from 2nd coordinate in degrees 85 * @return the distance 86 * @author Jason Huntley 87 */ 88 public static double getDistance(double la1, double lo1, double la2, double lo2) { 89 double aStartLat = Math.toRadians(la1); 90 double aStartLong = Math.toRadians(lo1); 91 double aEndLat =Math.toRadians(la2); 92 double aEndLong = Math.toRadians(lo2); 93 94 double distance = Math.acos(Math.sin(aStartLat) * Math.sin(aEndLat) 95 + Math.cos(aStartLat) * Math.cos(aEndLat) 96 * Math.cos(aEndLong - aStartLong)); 97 98 return (EARTH_RADIUS_KM * distance); 99 } 100 101 /** 46 102 * Transform longitude to pixelspace 47 103 * 48 104 * <p> -
JMapViewer.java
25 25 import javax.swing.JSlider; 26 26 import javax.swing.event.ChangeEvent; 27 27 import javax.swing.event.ChangeListener; 28 import javax.swing.event.EventListenerList; 28 29 30 import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent; 31 import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent.COMMAND; 32 import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener; 29 33 import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker; 30 34 import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon; 31 35 import org.openstreetmap.gui.jmapviewer.interfaces.MapRectangle; … … 36 40 import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource; 37 41 38 42 /** 39 * 43 * 40 44 * Provides a simple panel that displays pre-rendered map tiles loaded from the 41 45 * OpenStreetMap project. 42 * 46 * 43 47 * @author Jan Peter Stotz 44 * 48 * 45 49 */ 46 50 public class JMapViewer extends JPanel implements TileLoaderListener { 47 51 … … 117 121 tileSource = new OsmTileSource.Mapnik(); 118 122 tileController = new TileController(tileSource, tileCache, this); 119 123 mapMarkerList = new LinkedList<MapMarker>(); 120 mapRectangleList = new LinkedList<MapRectangle>();121 124 mapPolygonList = new LinkedList<MapPolygon>(); 125 mapRectangleList = new LinkedList<MapRectangle>(); 122 126 mapMarkersVisible = true; 123 127 mapRectanglesVisible = true; 124 128 mapPolygonsVisible = true; … … 187 191 /** 188 192 * Changes the map pane so that it is centered on the specified coordinate 189 193 * at the given zoom level. 190 * 194 * 191 195 * @param lat 192 196 * latitude of the specified coordinate 193 197 * @param lon … … 203 207 * Changes the map pane so that the specified coordinate at the given zoom 204 208 * level is displayed on the map at the screen coordinate 205 209 * <code>mapPoint</code>. 206 * 210 * 207 211 * @param mapPoint 208 212 * point on the map denoted in pixels where the coordinate should 209 213 * be set … … 264 268 nbElemToCheck += mapPolygonList.size(); 265 269 if (nbElemToCheck == 0) 266 270 return; 267 271 268 272 int x_min = Integer.MAX_VALUE; 269 273 int y_min = Integer.MAX_VALUE; 270 274 int x_max = Integer.MIN_VALUE; 271 275 int y_max = Integer.MIN_VALUE; 272 276 int mapZoomMax = tileController.getTileSource().getMaxZoom(); 273 277 274 278 if (markers) { 275 279 for (MapMarker marker : mapMarkerList) { 276 280 int x = OsmMercator.LonToX(marker.getLon(), mapZoomMax); … … 281 285 y_min = Math.min(y_min, y); 282 286 } 283 287 } 284 288 285 289 if (rectangles) { 286 290 for (MapRectangle rectangle : mapRectangleList) { 287 291 x_max = Math.max(x_max, OsmMercator.LonToX(rectangle.getBottomRight().getLon(), mapZoomMax)); … … 290 294 y_min = Math.min(y_min, OsmMercator.LatToY(rectangle.getBottomRight().getLat(), mapZoomMax)); 291 295 } 292 296 } 293 297 294 298 if (polygons) { 295 299 for (MapPolygon polygon : mapPolygonList) { 296 300 for (Coordinate c : polygon.getPoints()) { … … 303 307 } 304 308 } 305 309 } 306 310 307 311 int height = Math.max(0, getHeight()); 308 312 int width = Math.max(0, getWidth()); 309 313 int newZoom = mapZoomMax; … … 321 325 y /= z; 322 326 setDisplayPosition(x, y, newZoom); 323 327 } 324 328 329 325 330 /** 326 331 * Sets the displayed map pane and zoom level so that all map markers are 327 332 * visible. … … 337 342 public void setDisplayToFitMapRectangles() { 338 343 setDisplayToFitMapElements(false, true, false); 339 344 } 340 345 341 346 /** 342 347 * Sets the displayed map pane and zoom level so that all map polygons are 343 348 * visible. … … 347 352 } 348 353 349 354 /** 355 * @return the center 356 */ 357 public Point getCenter() { 358 return center; 359 } 360 361 /** 362 * @param center the center to set 363 */ 364 public void setCenter(Point center) { 365 this.center = center; 366 } 367 368 /** 350 369 * Calculates the latitude/longitude coordinate of the center of the 351 370 * currently displayed map area. 352 * 371 * 353 372 * @return latitude / longitude 354 373 */ 355 374 public Coordinate getPosition() { … … 361 380 /** 362 381 * Converts the relative pixel coordinate (regarding the top left corner of 363 382 * the displayed map) into a latitude / longitude coordinate 364 * 383 * 365 384 * @param mapPoint 366 385 * relative pixel coordinate regarding the top left corner of the 367 386 * displayed map … … 374 393 /** 375 394 * Converts the relative pixel coordinate (regarding the top left corner of 376 395 * the displayed map) into a latitude / longitude coordinate 377 * 396 * 378 397 * @param mapPointX 379 398 * @param mapPointY 380 399 * @return … … 389 408 390 409 /** 391 410 * Calculates the position on the map of a given coordinate 392 * 411 * 393 412 * @param lat 394 413 * @param lon 395 414 * @param checkOutside … … 410 429 411 430 /** 412 431 * Calculates the position on the map of a given coordinate 413 * 432 * 414 433 * @param lat 415 434 * @param lon 416 435 * @return point on the map or <code>null</code> if the point is not visible … … 421 440 422 441 /** 423 442 * Calculates the position on the map of a given coordinate 424 * 443 * 425 444 * @param coord 426 445 * @return point on the map or <code>null</code> if the point is not visible 427 446 */ … … 434 453 435 454 /** 436 455 * Calculates the position on the map of a given coordinate 437 * 456 * 438 457 * @param coord 439 458 * @return point on the map or <code>null</code> if the point is not visible 440 459 * and checkOutside set to <code>true</code> … … 446 465 return null; 447 466 } 448 467 468 /** 469 * Gets the meter per pixel. 470 * 471 * @return the meter per pixel 472 * @author Jason Huntley 473 */ 474 public double getMeterPerPixel() { 475 Point origin=new Point(5,5); 476 Point center=new Point(getWidth()/2, getHeight()/2); 477 478 double pDistance=center.distance(origin); 479 480 Coordinate originCoord=getPosition(origin); 481 Coordinate centerCoord=getPosition(center); 482 483 double kmDistance=OsmMercator.getDistance(originCoord.getLat(), originCoord.getLon(), 484 centerCoord.getLat(), centerCoord.getLon()); 485 486 double mDistance=OsmMercator.kmToMeters(kmDistance); 487 488 return mDistance/pDistance; 489 } 490 449 491 @Override 450 492 protected void paintComponent(Graphics g) { 451 493 super.paintComponent(g); … … 542 584 paintMarker(g, marker); 543 585 } 544 586 } 587 545 588 paintAttribution(g); 546 589 } 547 590 … … 587 630 polygon.paint(g, points); 588 631 } 589 632 } 590 633 591 634 /** 592 635 * Moves the visible map pane. 593 * 636 * 594 637 * @param x 595 638 * horizontal movement in pixel. 596 639 * @param y … … 600 643 center.x += x; 601 644 center.y += y; 602 645 repaint(); 646 this.fireJMVEvent(new JMVCommandEvent(COMMAND.MOVE, this)); 603 647 } 604 648 605 649 /** … … 645 689 tileController.cancelOutstandingJobs(); // Clearing outstanding load 646 690 // requests 647 691 setDisplayPositionByLatLon(mapPoint, zoomPos.getLat(), zoomPos.getLon(), zoom); 692 693 this.fireJMVEvent(new JMVCommandEvent(COMMAND.ZOOM, this)); 648 694 } 649 695 650 696 public void setZoom(int zoom) { … … 655 701 * Every time the zoom level changes this method is called. Override it in 656 702 * derived implementations for adapting zoom dependent values. The new zoom 657 703 * level can be obtained via {@link #getZoom()}. 658 * 704 * 659 705 * @param oldZoom 660 706 * the previous zoom level 661 707 */ … … 682 728 683 729 /** 684 730 * Enables or disables painting of the {@link MapMarker} 685 * 731 * 686 732 * @param mapMarkersVisible 687 733 * @see #addMapMarker(MapMarker) 688 734 * @see #getMapMarkerList() … … 763 809 mapPolygonList.clear(); 764 810 repaint(); 765 811 } 766 812 767 813 public void setZoomContolsVisible(boolean visible) { 768 814 zoomSlider.setVisible(visible); 769 815 zoomInButton.setVisible(visible); … … 808 854 809 855 /** 810 856 * Enables or disables painting of the {@link MapRectangle} 811 * 857 * 812 858 * @param mapRectanglesVisible 813 859 * @see #addMapRectangle(MapRectangle) 814 860 * @see #getMapRectangleList() … … 824 870 825 871 /** 826 872 * Enables or disables painting of the {@link MapPolygon} 827 * 873 * 828 874 * @param mapPolygonsVisible 829 875 * @see #addMapPolygon(MapPolygon) 830 876 * @see #getMapPolygonList() … … 836 882 837 883 /* 838 884 * (non-Javadoc) 839 * 885 * 840 886 * @see 841 887 * org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener#getTileCache 842 888 * () … … 898 944 899 945 g.setFont(font); 900 946 } 947 948 protected EventListenerList listenerList = new EventListenerList(); 949 950 /** 951 * @param listener to set 952 */ 953 public void addJMVListener(JMapViewerEventListener listener) { 954 listenerList.add(JMapViewerEventListener.class, listener); 955 } 956 957 /** 958 * @param listener to remove 959 */ 960 public void removeJMVListener(JMapViewerEventListener listener) { 961 listenerList.remove(JMapViewerEventListener.class, listener); 962 } 963 964 /** 965 * Send an update to all objects registered with viewer 966 * 967 * @param event to dispatch 968 */ 969 void fireJMVEvent(JMVCommandEvent evt) { 970 Object[] listeners = listenerList.getListenerList(); 971 for (int i=0; i<listeners.length; i+=2) { 972 if (listeners[i]==JMapViewerEventListener.class) { 973 ((JMapViewerEventListener)listeners[i+1]).processCommand(evt); 974 } 975 } 976 } 901 977 } -
Demo.java
16 16 import javax.swing.JLabel; 17 17 import javax.swing.JPanel; 18 18 19 import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent; 20 import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener; 19 21 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader; 20 22 import org.openstreetmap.gui.jmapviewer.interfaces.TileSource; 21 23 import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource; … … 28 30 * @author Jan Peter Stotz 29 31 * 30 32 */ 31 public class Demo extends JFrame {33 public class Demo extends JFrame implements JMapViewerEventListener { 32 34 33 35 private static final long serialVersionUID = 1L; 34 36 37 private JMapViewer map = null; 38 39 private JLabel zoomLabel=null; 40 private JLabel zoomValue=null; 41 42 private JLabel mperpLabelName=null; 43 private JLabel mperpLabelValue = null; 44 35 45 public Demo() { 36 46 super("JMapViewer Demo"); 37 47 setSize(400, 400); 38 final JMapViewer map = new JMapViewer(); 48 49 map = new JMapViewer(); 50 51 // Listen to the map viewer for user operations so components will 52 // recieve events and update 53 map.addJMVListener(this); 54 39 55 // final JMapViewer map = new JMapViewer(new MemoryTileCache(),4); 40 56 // map.setTileLoader(new OsmFileCacheTileLoader(map)); 41 57 // new DefaultMapController(map); 58 42 59 setLayout(new BorderLayout()); 43 60 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 44 61 setExtendedState(JFrame.MAXIMIZED_BOTH); 45 62 JPanel panel = new JPanel(); 46 63 JPanel helpPanel = new JPanel(); 64 65 mperpLabelName=new JLabel("Meters/Pixels: "); 66 mperpLabelValue=new JLabel(String.format("%s",map.getMeterPerPixel())); 67 68 zoomLabel=new JLabel("Zoom: "); 69 zoomValue=new JLabel(String.format("%s", map.getZoom())); 70 47 71 add(panel, BorderLayout.NORTH); 48 72 add(helpPanel, BorderLayout.SOUTH); 49 73 JLabel helpLabel = new JLabel("Use right mouse button to move,\n " … … 106 130 }); 107 131 panel.add(showZoomControls); 108 132 panel.add(button); 133 134 panel.add(zoomLabel); 135 panel.add(zoomValue); 136 panel.add(mperpLabelName); 137 panel.add(mperpLabelValue); 138 109 139 add(map, BorderLayout.CENTER); 110 140 111 141 // … … 129 159 new Demo().setVisible(true); 130 160 } 131 161 162 private void updateZoomParameters() { 163 if (mperpLabelValue!=null) 164 mperpLabelValue.setText(String.format("%s",map.getMeterPerPixel())); 165 if (zoomValue!=null) 166 zoomValue.setText(String.format("%s", map.getZoom())); 167 } 168 169 @Override 170 public void processCommand(JMVCommandEvent command) { 171 if (command.getCommand().equals(JMVCommandEvent.COMMAND.ZOOM) || 172 command.getCommand().equals(JMVCommandEvent.COMMAND.MOVE)) { 173 updateZoomParameters(); 174 } 175 } 176 132 177 } -
events/JMVCommandEvent.java
1 /** 2 * 3 */ 4 package org.openstreetmap.gui.jmapviewer.events; 5 6 import java.util.EventObject; 7 8 /** 9 * Used for passing events between UI components and other 10 * objects that register as a JMapViewerEventListener 11 * 12 * @author Jason Huntley 13 * 14 */ 15 public class JMVCommandEvent extends EventObject { 16 public static enum COMMAND { 17 MOVE, 18 ZOOM 19 } 20 21 private COMMAND command; 22 /** 23 * 24 */ 25 private static final long serialVersionUID = 8701544867914969620L; 26 27 public JMVCommandEvent(COMMAND cmd, Object source) { 28 super(source); 29 30 setCommand(cmd); 31 } 32 33 public JMVCommandEvent(Object source) { 34 super(source); 35 } 36 37 /** 38 * @return the command 39 */ 40 public COMMAND getCommand() { 41 return command; 42 } 43 44 /** 45 * @param command the command to set 46 */ 47 public void setCommand(COMMAND command) { 48 this.command = command; 49 } 50 } -
interfaces/JMapViewerEventListener.java
Property changes on: events\JMVCommandEvent.java ___________________________________________________________________ Added: svn:eol-style + native
1 /** 2 * 3 */ 4 package org.openstreetmap.gui.jmapviewer.interfaces; 5 6 import java.util.EventListener; 7 8 import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent; 9 10 /** 11 * Must be implemented for processing commands while user 12 * interacts with map viewer. 13 * 14 * @author Jason Huntley 15 * 16 */ 17 public interface JMapViewerEventListener extends EventListener { 18 public void processCommand(JMVCommandEvent command); 19 }