JMapViewer add mouse listener to MapMarkerDot

2019-01-08 02:21发布

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条回答
何必那么认真
2楼-- · 2019-01-08 02:37

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()));
    }
};
查看更多
登录 后发表回答