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;
}
While running
Demo.java
, control-click or right-click on the map to pan the view. UsesetMovementMouseButton()
to control the behavior.