Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example to show how to call a Dijkstra algorithm using JMapViewer
package de.sebastianhesse.pbf.viewer;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.JMapViewerTree;
import org.openstreetmap.gui.jmapviewer.Layer;
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;
import java.util.List;
/**
* A demo class to start a Swing application which shows a map and has some pre-defined options set.
* <p>
* Based on: http://svn.openstreetmap.org/applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/Demo.java by Jan Peter Stotz
*/
public class OsmMapViewer2 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 OsmMapViewer2() {
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);
map().addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON1) {
map().getAttribution().handleAttribution(e.getPoint(), true);
ICoordinate position = map().getPosition(e.getPoint());
// save point by using:
// position.getLat();
// position.getLon();
// add a map marker to the map
map().addMapMarker(new MapMarkerDot(position.getLat(), position.getLon()));
if (...) {
// if you have two points, then call your Dijkstra
// dijkstra.getShortestPath(startPoint, endPoint);
List shortestPath = ; // get list of single nodes between start and end point from dijkstra
Layer routeLayer = new Layer("Name of your path, so you can hide it later in the map.");
for (Node node : shortestPath) {
// add a marker for each point, so you can visualise the shortest path
MapMarkerDot marker = new MapMarkerDot(routeLayer, node.getLat(), node.getLon());
map().addMapMarker(marker);
}
}
}
}
});
}
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();
}
}
}
@seeebiii

This comment has been minimized.

Copy link
Owner Author

commented Jan 27, 2017

This is the example code for the blogpost @ sebastianhesse.de

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.