| 1 | package org.openstreetmap.gui.jmapviewer;
|
|---|
| 2 |
|
|---|
| 3 | //License: GPL. Copyright 2008 by Jan Peter Stotz
|
|---|
| 4 |
|
|---|
| 5 | import java.awt.BorderLayout;
|
|---|
| 6 | import java.awt.event.ActionEvent;
|
|---|
| 7 | import java.awt.event.ActionListener;
|
|---|
| 8 | import java.awt.event.ItemEvent;
|
|---|
| 9 | import java.awt.event.ItemListener;
|
|---|
| 10 | import java.io.IOException;
|
|---|
| 11 |
|
|---|
| 12 | import javax.swing.JButton;
|
|---|
| 13 | import javax.swing.JCheckBox;
|
|---|
| 14 | import javax.swing.JComboBox;
|
|---|
| 15 | import javax.swing.JFrame;
|
|---|
| 16 | import javax.swing.JLabel;
|
|---|
| 17 | import javax.swing.JPanel;
|
|---|
| 18 |
|
|---|
| 19 | import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent;
|
|---|
| 20 | import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener;
|
|---|
| 21 | import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
|
|---|
| 22 | import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
|
|---|
| 23 | import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
|
|---|
| 24 | import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
|
|---|
| 25 |
|
|---|
| 26 | /**
|
|---|
| 27 | *
|
|---|
| 28 | * Demonstrates the usage of {@link JMapViewer}
|
|---|
| 29 | *
|
|---|
| 30 | * @author Jan Peter Stotz
|
|---|
| 31 | *
|
|---|
| 32 | */
|
|---|
| 33 | public class Demo extends JFrame implements JMapViewerEventListener {
|
|---|
| 34 |
|
|---|
| 35 | 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;
|
|---|
| 44 |
|
|---|
| 45 | public Demo() {
|
|---|
| 46 | super("JMapViewer Demo");
|
|---|
| 47 | setSize(400, 400);
|
|---|
| 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 |
|
|---|
| 55 | // final JMapViewer map = new JMapViewer(new MemoryTileCache(),4);
|
|---|
| 56 | // map.setTileLoader(new OsmFileCacheTileLoader(map));
|
|---|
| 57 | // new DefaultMapController(map);
|
|---|
| 58 |
|
|---|
| 59 | setLayout(new BorderLayout());
|
|---|
| 60 | setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
|---|
| 61 | setExtendedState(JFrame.MAXIMIZED_BOTH);
|
|---|
| 62 | JPanel panel = new JPanel();
|
|---|
| 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 |
|
|---|
| 71 | add(panel, BorderLayout.NORTH);
|
|---|
| 72 | add(helpPanel, BorderLayout.SOUTH);
|
|---|
| 73 | JLabel helpLabel = new JLabel("Use right mouse button to move,\n "
|
|---|
| 74 | + "left double click or mouse wheel to zoom.");
|
|---|
| 75 | helpPanel.add(helpLabel);
|
|---|
| 76 | JButton button = new JButton("setDisplayToFitMapMarkers");
|
|---|
| 77 | button.addActionListener(new ActionListener() {
|
|---|
| 78 |
|
|---|
| 79 | public void actionPerformed(ActionEvent e) {
|
|---|
| 80 | map.setDisplayToFitMapMarkers();
|
|---|
| 81 | }
|
|---|
| 82 | });
|
|---|
| 83 | JComboBox tileSourceSelector = new JComboBox(new TileSource[] { new OsmTileSource.Mapnik(),
|
|---|
| 84 | new OsmTileSource.TilesAtHome(), new OsmTileSource.CycleMap(), new BingAerialTileSource() });
|
|---|
| 85 | tileSourceSelector.addItemListener(new ItemListener() {
|
|---|
| 86 | public void itemStateChanged(ItemEvent e) {
|
|---|
| 87 | map.setTileSource((TileSource) e.getItem());
|
|---|
| 88 | }
|
|---|
| 89 | });
|
|---|
| 90 | JComboBox tileLoaderSelector;
|
|---|
| 91 | try {
|
|---|
| 92 | tileLoaderSelector = new JComboBox(new TileLoader[] { new OsmFileCacheTileLoader(map),
|
|---|
| 93 | new OsmTileLoader(map) });
|
|---|
| 94 | } catch (IOException e) {
|
|---|
| 95 | tileLoaderSelector = new JComboBox(new TileLoader[] { new OsmTileLoader(map) });
|
|---|
| 96 | }
|
|---|
| 97 | tileLoaderSelector.addItemListener(new ItemListener() {
|
|---|
| 98 | public void itemStateChanged(ItemEvent e) {
|
|---|
| 99 | map.setTileLoader((TileLoader) e.getItem());
|
|---|
| 100 | }
|
|---|
| 101 | });
|
|---|
| 102 | map.setTileLoader((TileLoader) tileLoaderSelector.getSelectedItem());
|
|---|
| 103 | panel.add(tileSourceSelector);
|
|---|
| 104 | panel.add(tileLoaderSelector);
|
|---|
| 105 | final JCheckBox showMapMarker = new JCheckBox("Map markers visible");
|
|---|
| 106 | showMapMarker.setSelected(map.getMapMarkersVisible());
|
|---|
| 107 | showMapMarker.addActionListener(new ActionListener() {
|
|---|
| 108 |
|
|---|
| 109 | public void actionPerformed(ActionEvent e) {
|
|---|
| 110 | map.setMapMarkerVisible(showMapMarker.isSelected());
|
|---|
| 111 | }
|
|---|
| 112 | });
|
|---|
| 113 | panel.add(showMapMarker);
|
|---|
| 114 | final JCheckBox showTileGrid = new JCheckBox("Tile grid visible");
|
|---|
| 115 | showTileGrid.setSelected(map.isTileGridVisible());
|
|---|
| 116 | showTileGrid.addActionListener(new ActionListener() {
|
|---|
| 117 |
|
|---|
| 118 | public void actionPerformed(ActionEvent e) {
|
|---|
| 119 | map.setTileGridVisible(showTileGrid.isSelected());
|
|---|
| 120 | }
|
|---|
| 121 | });
|
|---|
| 122 | panel.add(showTileGrid);
|
|---|
| 123 | final JCheckBox showZoomControls = new JCheckBox("Show zoom controls");
|
|---|
| 124 | showZoomControls.setSelected(map.getZoomContolsVisible());
|
|---|
| 125 | showZoomControls.addActionListener(new ActionListener() {
|
|---|
| 126 |
|
|---|
| 127 | public void actionPerformed(ActionEvent e) {
|
|---|
| 128 | map.setZoomContolsVisible(showZoomControls.isSelected());
|
|---|
| 129 | }
|
|---|
| 130 | });
|
|---|
| 131 | panel.add(showZoomControls);
|
|---|
| 132 | panel.add(button);
|
|---|
| 133 |
|
|---|
| 134 | panel.add(zoomLabel);
|
|---|
| 135 | panel.add(zoomValue);
|
|---|
| 136 | panel.add(mperpLabelName);
|
|---|
| 137 | panel.add(mperpLabelValue);
|
|---|
| 138 |
|
|---|
| 139 | add(map, BorderLayout.CENTER);
|
|---|
| 140 |
|
|---|
| 141 | //
|
|---|
| 142 | map.addMapMarker(new MapMarkerDot(49.814284999, 8.642065999));
|
|---|
| 143 | map.addMapMarker(new MapMarkerDot(49.91, 8.24));
|
|---|
| 144 | map.addMapMarker(new MapMarkerDot(49.71, 8.64));
|
|---|
| 145 | map.addMapMarker(new MapMarkerDot(48.71, -1));
|
|---|
| 146 | map.addMapMarker(new MapMarkerDot(49.8588, 8.643));
|
|---|
| 147 |
|
|---|
| 148 | // map.setDisplayPositionByLatLon(49.807, 8.6, 11);
|
|---|
| 149 | // map.setTileGridVisible(true);
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /**
|
|---|
| 153 | * @param args
|
|---|
| 154 | */
|
|---|
| 155 | public static void main(String[] args) {
|
|---|
| 156 | // java.util.Properties systemProperties = System.getProperties();
|
|---|
| 157 | // systemProperties.setProperty("http.proxyHost", "localhost");
|
|---|
| 158 | // systemProperties.setProperty("http.proxyPort", "8008");
|
|---|
| 159 | new Demo().setVisible(true);
|
|---|
| 160 | }
|
|---|
| 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 | updateZoomParameters();
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | }
|
|---|