how can i get the mouse click position from my JMapViewer world map

烈酒焚心 提交于 2019-12-07 12:51:49

问题


Im using the JMapViewer jar to show the world map on a JPanel.

On the map im adding MapMarkerDot's which are gps points.

The problem is when i click a MapMarkerDot on the map i cannot
find an interface or listener to catch the click and give me the
clicked MapMarkerDot identity.

has anyone here worked with the code or can give me some ideas what to do.

I would not like to modify the jar source but maybe i have to input an interface.

I know this is kind of an abstract question but hoping for help


回答1:


Answering my own question.
Basically solved this by raw x/y calculation comparing the
MapMarker position against mouse click position.

if (e.getButton() == MouseEvent.BUTTON1) {
    Point p = e.getPoint();
    int X = p.x+3;
    int Y = p.y+3;
    List<MapMarker> ar = map.getMapMarkerList();
    Iterator<MapMarker> i = ar.iterator();
    while (i.hasNext()) {

        MyMapMarkerDot mapMarker = (MyMapMarkerDot) i.next();

        if(mapMarker.position != null){

            int centerX =  mapMarker.position.x;
            int centerY = mapMarker.position.y;

            // calculate the radius from the touch to the center of the dot
            double radCircle  = Math.sqrt( (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));

            // if the radius is smaller then 23 (radius of a ball is 5), then it must be on the dot
            if (radCircle < 8){
                ShowClickedUser(mapMarker.Tag);
            }

        }
    }
}



回答2:


You can edit the code of DefaultMapController.java :

 public void mouseClicked(MouseEvent e) {

    if(e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON1){

         Point p = e.getPoint();
            int X = p.x+3;
            int Y = p.y+3;
            List<MapMarker> ar = map.getMapMarkerList();
            Iterator<MapMarker> i = ar.iterator();
            while (i.hasNext()) {

                MapMarker mapMarker = (MapMarker) i.next();

                Point MarkerPosition = map.getMapPosition(mapMarker.getLat(), mapMarker.getLon());
                if( MarkerPosition != null){

                    int centerX =  MarkerPosition.x;
                    int centerY = MarkerPosition.y;

                    // calculate the radius from the touch to the center of the dot
                    double radCircle  = Math.sqrt( (((centerX-X)*(centerX-X)) + (centerY-Y)*(centerY-Y)));

                    // if the radius is smaller then 23 (radius of a ball is 5), then it must be on the dot
                    if (radCircle < 8){
                        System.out.println(mapMarker.toString() + " is clicked");                       }

                }
            }
    }

    else if (doubleClickZoomEnabled && e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
        map.zoomIn(e.getPoint());
    }
}

Hope this will help! Welcome to further discussion.



来源:https://stackoverflow.com/questions/8780882/how-can-i-get-the-mouse-click-position-from-my-jmapviewer-world-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!