3

I am trying to figure out which mapping tools work best for my java swing based OSM map. Earlier I made a functioning map on the basis of MapPanel. Later I could almost clone the application and merely replace MapPanel with JMapViewer to get this working as well. I prefer the latter as it has the functionality of putting markers and polygons on the map. The drawback however is that for some reason I cannot drag the map with the mouse. In the package of JMapViewer there is a Demo class which displays the same behaviour. How can I make the OSM map draggable within JMapViewer?

Edit: This code will draw a map that can be displayed in a panel and make the map draggable by adding a mapController and setMovementMouseButton().

private JMapViewer drawOpenStreetMap(double lon, double lat, int zoom) {
    mapViewer = new JMapViewer();
    mapViewer.setZoom(zoom);
    mapViewer.setDisplayPositionByLatLon(lat, lon, zoom);
    mapViewer.repaint();
    DefaultMapController mapController = new DefaultMapController(mapViewer);
    mapController.setMovementMouseButton(MouseEvent.BUTTON1);
    mapViewer.addMapMarker(new MapMarkerDot(52, 5.5));
    return mapViewer;
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
jhulst
  • 353
  • 1
  • 3
  • 18

1 Answers1

3

While running Demo.java, control-click or right-click on the map to pan the view. Use setMovementMouseButton() to control the behavior.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Excellent, that works! This code will draw a map that can be displayed in a panel AND make the map draggable by adding a mapController and setMovementMouseButton(). – jhulst Jul 26 '12 at 09:21
  • : JMapViewer drawOpenStreetMap(double lon, double lat, int zoom) { mapViewer = new JMapViewer(); mapViewer.setZoom(zoom); mapViewer.setDisplayPositionByLatLon(lat, lon, zoom); mapViewer.repaint(); DefaultMapController mapController = new DefaultMapController(mapViewer); mapController.setMovementMouseButton(MouseEvent.BUTTON1); mapViewer.addMapMarker(new MapMarkerDot(52, 5.5)); return mapViewer; } – jhulst Jul 26 '12 at 09:25