JMapViewer add mouse listener to MapMarkerDot

2019-01-08 02:10发布

问题:

I am trying to add a mouse listener to a MapMarker, so that when a mouse will hover a MapMarker, I could react with an event.

I implemented the mouseListener, but I can't really add a listener. The issue is that I did not find a way the MapMarker will addMouseListener, due to the fact that non of the hierarchy implements JPanel.

Any help appreciated

回答1:

As noted here, the default JMapViewer constructor uses a DefaultMapController, "which implements map moving by pressing the right mouse button and zooming by double click or by mouse wheel." To see map coordinates, extend DefaultMapController, override mouseClicked() and use one of the viewer's getPosition() methods. You can use your controller as shown in comments at line 65 of Demo.java, r30377.

 new DefaultMapController(map){

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println(map.getPosition(e.getPoint()));
    }
};