Last active
January 27, 2017 19:09
-
-
Save seeebiii/9dce688186248644ebb373a266539437 to your computer and use it in GitHub Desktop.
Java class to startup a basic JMapViewer window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package de.sebastianhesse.gist.demo; | |
import org.openstreetmap.gui.jmapviewer.JMapViewer; | |
import org.openstreetmap.gui.jmapviewer.JMapViewerTree; | |
import org.openstreetmap.gui.jmapviewer.MapMarkerDot; | |
import org.openstreetmap.gui.jmapviewer.OsmTileLoader; | |
import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent; | |
import org.openstreetmap.gui.jmapviewer.interfaces.ICoordinate; | |
import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener; | |
import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import java.awt.BorderLayout; | |
import java.awt.event.MouseAdapter; | |
import java.awt.event.MouseEvent; | |
/** | |
* A demo class to start a Swing application which shows a map and has some pre-defined options set. | |
* | |
* Based on: http://svn.openstreetmap.org/applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java by Jan Peter Stotz | |
*/ | |
public class OsmMapViewer extends JFrame implements JMapViewerEventListener { | |
private static final long serialVersionUID = 1L; | |
private JMapViewerTree treeMap; | |
private JLabel zoomLabel; | |
private JLabel zoomValue; | |
private JLabel mperpLabelName; | |
private JLabel mperpLabelValue; | |
/** | |
* Setups the JFrame layout, sets some default options for the JMapViewerTree and displays a map in the window. | |
*/ | |
public OsmMapViewer() { | |
super("JMapViewer Demo"); | |
treeMap = new JMapViewerTree("Zones"); | |
setupJFrame(); | |
setupPanels(); | |
// Listen to the map viewer for user operations so components will | |
// receive events and updates | |
map().addJMVListener(this); | |
// Set some options, e.g. tile source and that markers are visible | |
map().setTileSource(new OsmTileSource.Mapnik()); | |
map().setTileLoader(new OsmTileLoader(map())); | |
map().setMapMarkerVisible(true); | |
map().setZoomContolsVisible(true); | |
// activate map in window | |
treeMap.setTreeVisible(true); | |
add(treeMap, BorderLayout.CENTER); | |
} | |
private void setupJFrame() { | |
setSize(400, 400); | |
setLayout(new BorderLayout()); | |
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
setExtendedState(JFrame.MAXIMIZED_BOTH); | |
} | |
private void setupPanels() { | |
JPanel panel = new JPanel(new BorderLayout()); | |
JPanel panelTop = new JPanel(); | |
JPanel panelBottom = new JPanel(); | |
JPanel helpPanel = new JPanel(); | |
mperpLabelName = new JLabel("Meters/Pixels: "); | |
mperpLabelValue = new JLabel(String.format("%s", map().getMeterPerPixel())); | |
zoomLabel = new JLabel("Zoom: "); | |
zoomValue = new JLabel(String.format("%s", map().getZoom())); | |
add(panel, BorderLayout.NORTH); | |
add(helpPanel, BorderLayout.SOUTH); | |
panel.add(panelTop, BorderLayout.NORTH); | |
panel.add(panelBottom, BorderLayout.SOUTH); | |
JLabel helpLabel = new JLabel("Use right mouse button to move,\n " | |
+ "left double click or mouse wheel to zoom."); | |
helpPanel.add(helpLabel); | |
panelTop.add(zoomLabel); | |
panelTop.add(zoomValue); | |
panelTop.add(mperpLabelName); | |
panelTop.add(mperpLabelValue); | |
} | |
private JMapViewer map() { | |
return treeMap.getViewer(); | |
} | |
/** | |
* @param args Main program arguments | |
*/ | |
public static void main(String[] args) { | |
new OsmMapViewer().setVisible(true); | |
} | |
private void updateZoomParameters() { | |
if (mperpLabelValue != null) | |
mperpLabelValue.setText(String.format("%s", map().getMeterPerPixel())); | |
if (zoomValue != null) | |
zoomValue.setText(String.format("%s", map().getZoom())); | |
} | |
@Override | |
public void processCommand(JMVCommandEvent command) { | |
if (command.getCommand().equals(JMVCommandEvent.COMMAND.ZOOM) || | |
command.getCommand().equals(JMVCommandEvent.COMMAND.MOVE)) { | |
updateZoomParameters(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the example code for the blogpost @ sebastianhesse.de