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

2.6k views Asked by At

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

2

There are 2 answers

0
Erik On BEST ANSWER

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);
            }

        }
    }
}
0
Leung Ying Ying On

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.