Changeset 26701 in osm for applications/viewer/jmapviewer


Ignore:
Timestamp:
2011-09-24T10:29:43+02:00 (13 years ago)
Author:
bastik
Message:

applied josm 6869 - scale and event listeners for JMapViewer (patch by Jason Huntley)

Location:
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer
Files:
3 added
3 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java

    r25369 r26701  
    1717import javax.swing.JPanel;
    1818
     19import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent;
     20import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener;
    1921import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
    2022import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
     
    2931 *
    3032 */
    31 public class Demo extends JFrame {
     33public class Demo extends JFrame implements JMapViewerEventListener  {
    3234
    3335    private static final long serialVersionUID = 1L;
     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;
    3444
    3545    public Demo() {
    3646        super("JMapViewer Demo");
    3747        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
    3955        // final JMapViewer map = new JMapViewer(new MemoryTileCache(),4);
    4056        // map.setTileLoader(new OsmFileCacheTileLoader(map));
    4157        // new DefaultMapController(map);
     58
    4259        setLayout(new BorderLayout());
    4360        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
    4562        JPanel panel = new JPanel();
    4663        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
    4771        add(panel, BorderLayout.NORTH);
    4872        add(helpPanel, BorderLayout.SOUTH);
     
    107131        panel.add(showZoomControls);
    108132        panel.add(button);
     133
     134        panel.add(zoomLabel);
     135        panel.add(zoomValue);
     136        panel.add(mperpLabelName);
     137        panel.add(mperpLabelValue);
     138
    109139        add(map, BorderLayout.CENTER);
    110140
     
    130160    }
    131161
     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
    132177}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/JMapViewer.java

    r26652 r26701  
    2626import javax.swing.event.ChangeEvent;
    2727import javax.swing.event.ChangeListener;
    28 
     28import javax.swing.event.EventListenerList;
     29
     30import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent;
     31import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent.COMMAND;
     32import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener;
    2933import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
    3034import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;
     
    3741
    3842/**
    39  * 
     43 *
    4044 * Provides a simple panel that displays pre-rendered map tiles loaded from the
    4145 * OpenStreetMap project.
    42  * 
     46 *
    4347 * @author Jan Peter Stotz
    44  * 
     48 *
    4549 */
    4650public class JMapViewer extends JPanel implements TileLoaderListener {
     
    118122        tileController = new TileController(tileSource, tileCache, this);
    119123        mapMarkerList = new LinkedList<MapMarker>();
     124        mapPolygonList = new LinkedList<MapPolygon>();
    120125        mapRectangleList = new LinkedList<MapRectangle>();
    121         mapPolygonList = new LinkedList<MapPolygon>();
    122126        mapMarkersVisible = true;
    123127        mapRectanglesVisible = true;
     
    188192     * Changes the map pane so that it is centered on the specified coordinate
    189193     * at the given zoom level.
    190      * 
     194     *
    191195     * @param lat
    192196     *            latitude of the specified coordinate
     
    204208     * level is displayed on the map at the screen coordinate
    205209     * <code>mapPoint</code>.
    206      * 
     210     *
    207211     * @param mapPoint
    208212     *            point on the map denoted in pixels where the coordinate should
     
    265269        if (nbElemToCheck == 0)
    266270            return;
    267        
     271
    268272        int x_min = Integer.MAX_VALUE;
    269273        int y_min = Integer.MAX_VALUE;
     
    271275        int y_max = Integer.MIN_VALUE;
    272276        int mapZoomMax = tileController.getTileSource().getMaxZoom();
    273        
     277
    274278        if (markers) {
    275279            for (MapMarker marker : mapMarkerList) {
     
    282286            }
    283287        }
    284        
     288
    285289        if (rectangles) {
    286290            for (MapRectangle rectangle : mapRectangleList) {
     
    291295            }
    292296        }
    293        
     297
    294298        if (polygons) {
    295299            for (MapPolygon polygon : mapPolygonList) {
     
    304308            }
    305309        }
    306        
     310
    307311        int height = Math.max(0, getHeight());
    308312        int width = Math.max(0, getWidth());
     
    322326        setDisplayPosition(x, y, newZoom);
    323327    }
    324    
     328
     329
    325330    /**
    326331     * Sets the displayed map pane and zoom level so that all map markers are
     
    338343        setDisplayToFitMapElements(false, true, false);
    339344    }
    340    
     345
    341346    /**
    342347     * Sets the displayed map pane and zoom level so that all map polygons are
     
    348353
    349354    /**
     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    /**
    350369     * Calculates the latitude/longitude coordinate of the center of the
    351370     * currently displayed map area.
    352      * 
     371     *
    353372     * @return latitude / longitude
    354373     */
     
    362381     * Converts the relative pixel coordinate (regarding the top left corner of
    363382     * the displayed map) into a latitude / longitude coordinate
    364      * 
     383     *
    365384     * @param mapPoint
    366385     *            relative pixel coordinate regarding the top left corner of the
     
    375394     * Converts the relative pixel coordinate (regarding the top left corner of
    376395     * the displayed map) into a latitude / longitude coordinate
    377      * 
     396     *
    378397     * @param mapPointX
    379398     * @param mapPointY
     
    390409    /**
    391410     * Calculates the position on the map of a given coordinate
    392      * 
     411     *
    393412     * @param lat
    394413     * @param lon
     
    411430    /**
    412431     * Calculates the position on the map of a given coordinate
    413      * 
     432     *
    414433     * @param lat
    415434     * @param lon
     
    422441    /**
    423442     * Calculates the position on the map of a given coordinate
    424      * 
     443     *
    425444     * @param coord
    426445     * @return point on the map or <code>null</code> if the point is not visible
     
    435454    /**
    436455     * Calculates the position on the map of a given coordinate
    437      * 
     456     *
    438457     * @param coord
    439458     * @return point on the map or <code>null</code> if the point is not visible
     
    445464        else
    446465            return null;
     466    }
     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 mDistance=OsmMercator.getDistance(originCoord.getLat(), originCoord.getLon(),
     484                centerCoord.getLat(), centerCoord.getLon());
     485
     486        return mDistance/pDistance;
    447487    }
    448488
     
    543583            }
    544584        }
     585
    545586        paintAttribution(g);
    546587    }
     
    588629        }
    589630    }
    590    
     631
    591632    /**
    592633     * Moves the visible map pane.
    593      * 
     634     *
    594635     * @param x
    595636     *            horizontal movement in pixel.
     
    601642        center.y += y;
    602643        repaint();
     644        this.fireJMVEvent(new JMVCommandEvent(COMMAND.MOVE, this));
    603645    }
    604646
     
    646688        // requests
    647689        setDisplayPositionByLatLon(mapPoint, zoomPos.getLat(), zoomPos.getLon(), zoom);
     690
     691        this.fireJMVEvent(new JMVCommandEvent(COMMAND.ZOOM, this));
    648692    }
    649693
     
    656700     * derived implementations for adapting zoom dependent values. The new zoom
    657701     * level can be obtained via {@link #getZoom()}.
    658      * 
     702     *
    659703     * @param oldZoom
    660704     *            the previous zoom level
     
    683727    /**
    684728     * Enables or disables painting of the {@link MapMarker}
    685      * 
     729     *
    686730     * @param mapMarkersVisible
    687731     * @see #addMapMarker(MapMarker)
     
    764808        repaint();
    765809    }
    766    
     810
    767811    public void setZoomContolsVisible(boolean visible) {
    768812        zoomSlider.setVisible(visible);
     
    809853    /**
    810854     * Enables or disables painting of the {@link MapRectangle}
    811      * 
     855     *
    812856     * @param mapRectanglesVisible
    813857     * @see #addMapRectangle(MapRectangle)
     
    825869    /**
    826870     * Enables or disables painting of the {@link MapPolygon}
    827      * 
     871     *
    828872     * @param mapPolygonsVisible
    829873     * @see #addMapPolygon(MapPolygon)
     
    837881    /*
    838882     * (non-Javadoc)
    839      * 
     883     *
    840884     * @see
    841885     * org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener#getTileCache
     
    899943        g.setFont(font);
    900944    }
     945
     946    protected EventListenerList listenerList = new EventListenerList();
     947
     948    /**
     949     * @param listener to set
     950     */
     951    public void addJMVListener(JMapViewerEventListener listener) {
     952        listenerList.add(JMapViewerEventListener.class, listener);
     953    }
     954
     955    /**
     956     * @param listener to remove
     957     */
     958    public void removeJMVListener(JMapViewerEventListener listener) {
     959        listenerList.remove(JMapViewerEventListener.class, listener);
     960    }
     961
     962    /**
     963     * Send an update to all objects registered with viewer
     964     *
     965     * @param event to dispatch
     966     */
     967    void fireJMVEvent(JMVCommandEvent evt) {
     968        Object[] listeners = listenerList.getListenerList();
     969        for (int i=0; i<listeners.length; i+=2) {
     970            if (listeners[i]==JMapViewerEventListener.class) {
     971                ((JMapViewerEventListener)listeners[i+1]).processCommand(evt);
     972            }
     973        }
     974    }
    901975}
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/OsmMercator.java

    r18772 r26701  
    1919    public static final double MAX_LAT = 85.05112877980659;
    2020    public static final double MIN_LAT = -85.05112877980659;
     21    private static double EARTH_RADIUS = 6378137; // equatorial earth radius for EPSG:3857 (Mercator)
    2122
    2223    public static double radius(int aZoomlevel) {
     
    4142    public static int falseNorthing(int aZoomlevel) {
    4243        return (-1 * getMaxPixels(aZoomlevel) / 2);
     44    }
     45
     46    /**
     47     * Transform pixelspace to coordinates and get the distance.
     48     *
     49     * @param x1 the first x coordinate
     50     * @param y1 the first y coordinate
     51     * @param x2 the second x coordinate
     52     * @param y2 the second y coordinate
     53     *
     54     * @param zoomLevel the zoom level
     55     * @return the distance
     56     * @author Jason Huntley
     57     */
     58    public static double getDistance(int x1, int y1, int x2, int y2, int zoomLevel) {
     59        double la1 = YToLat(y1, zoomLevel);
     60        double lo1 = XToLon(x1, zoomLevel);
     61        double la2 = YToLat(y2, zoomLevel);
     62        double lo2 = XToLon(x2, zoomLevel);
     63
     64        return getDistance(la1, lo1, la2, lo2);
     65    }
     66
     67    /**
     68     * Gets the distance using Spherical law of cosines.
     69     *
     70     * @param la1 the Latitude in degrees
     71     * @param lo1 the Longitude in degrees
     72     * @param la2 the Latitude from 2nd coordinate in degrees
     73     * @param lo2 the Longitude from 2nd coordinate in degrees
     74     * @return the distance
     75     * @author Jason Huntley
     76     */
     77    public static double getDistance(double la1, double lo1, double la2, double lo2) {
     78        double aStartLat = Math.toRadians(la1);
     79        double aStartLong = Math.toRadians(lo1);
     80        double aEndLat =Math.toRadians(la2);
     81        double aEndLong = Math.toRadians(lo2);
     82
     83        double distance = Math.acos(Math.sin(aStartLat) * Math.sin(aEndLat)
     84                + Math.cos(aStartLat) * Math.cos(aEndLat)
     85                * Math.cos(aEndLong - aStartLong));
     86
     87        return (EARTH_RADIUS * distance);               
    4388    }
    4489
Note: See TracChangeset for help on using the changeset viewer.