简体   繁体   English

如何从我的JMapViewer世界地图中获取鼠标点击位置

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

Im using the JMapViewer jar to show the world map on a JPanel. 我使用JMapViewer jar在JPanel上显示世界地图。

On the map im adding MapMarkerDot's which are gps points. 在地图上我添加了MapMarkerDot's gps点。

The problem is when i click a MapMarkerDot on the map i cannot 问题是当我点击地图上的MapMarkerDot我不能
find an interface or listener to catch the click and give me the 找到一个界面或听众来抓住点击并给我
clicked MapMarkerDot identity. 点击MapMarkerDot标识。

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. 我不想修改jar源,但也许我必须输入一个接口。

I know this is kind of an abstract question but hoping for help 我知道这是一个抽象的问题,但希望得到帮助

You can edit the code of DefaultMapController.java : 您可以编辑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. 欢迎进一步讨论。

Answering my own question. 回答我自己的问题。
Basically solved this by raw x/y calculation comparing the 基本上通过原始x / y计算比较来解决这个问题
MapMarker position against mouse click position. MapMarker对鼠标点击位置的位置。

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

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM