将mouseClicked事件侦听器添加到JXMapViewer上的路标点

时间:2013-10-16 15:03:59

标签: java swing mouseclick-event jxmapkit jxmapviewer

我通过为每个航点创建mouseClicked事件,为JXMapViewers上的Github添加一些示例代码的功能。最终,每个航路点将显示唯一的信息,例如它的坐标。

使用我当前的实现,只有添加到Set<MyWaypoint>的最后一个元素会向控制台显示以下内容。

  

在航点的10个像素内检测到mapViewer鼠标点击

有关检测Set中其他航点的mouseClicked事件的想法吗?

    public class Sample4 {

    private static JXMapViewer mapViewer;

    /**
     * @param args the program args (ignored)
     */
    public static void main(String[] args) {
        // Create a TileFactoryInfo for Virtual Earth
        TileFactoryInfo info = new VirtualEarthTileFactoryInfo(VirtualEarthTileFactoryInfo.MAP);
        DefaultTileFactory tileFactory = new DefaultTileFactory(info);
        tileFactory.setThreadPoolSize(8);

        // Setup local file cache
        File cacheDir = new File(System.getProperty("user.home") + File.separator + ".jxmapviewer2");
        LocalResponseCache.installResponseCache(info.getBaseURL(), cacheDir, false);

        // Setup JXMapViewer
        mapViewer = new JXMapViewer();
        mapViewer.setTileFactory(tileFactory);

        GeoPosition frankfurt = new GeoPosition(50, 7, 0, 8, 41, 0);
        GeoPosition wiesbaden = new GeoPosition(50, 5, 0, 8, 14, 0);
        GeoPosition mainz = new GeoPosition(50, 0, 0, 8, 16, 0);
        GeoPosition darmstadt = new GeoPosition(49, 52, 0, 8, 39, 0);
        GeoPosition offenbach = new GeoPosition(50, 6, 0, 8, 46, 0);

        // Set the focus
        mapViewer.setZoom(10);
        mapViewer.setAddressLocation(frankfurt);

        // Add interactions
        MouseInputListener mia = new PanMouseInputListener(mapViewer);
        mapViewer.addMouseListener(mia);
        mapViewer.addMouseMotionListener(mia);
        mapViewer.addMouseListener(new CenterMapListener(mapViewer));
        mapViewer.addMouseWheelListener(new ZoomMouseWheelListenerCenter(mapViewer));
        mapViewer.addKeyListener(new PanKeyListener(mapViewer));


        // Create a track from the geo-positions
        final List<GeoPosition> track = Arrays.asList(frankfurt, wiesbaden, mainz, darmstadt, offenbach);
        RoutePainter routePainter = new RoutePainter(track);

        // Create waypoints from the geo-positions
        Set<MyWaypoint> waypoints = new HashSet<MyWaypoint>(Arrays.asList(
                new MyWaypoint("1", Color.ORANGE, frankfurt),
                new MyWaypoint("2", Color.CYAN, wiesbaden),
                new MyWaypoint("3", Color.GRAY, mainz),
                new MyWaypoint("4", Color.MAGENTA, darmstadt),
                new MyWaypoint("5", Color.GREEN, offenbach)));

        // Create a waypoint painter that takes all the waypoints
        WaypointPainter<MyWaypoint> waypointPainter = new WaypointPainter<MyWaypoint>();
        waypointPainter.setWaypoints(waypoints);
        waypointPainter.setRenderer(new FancyWaypointRenderer());

        // Create a compound painter that uses both the route-painter and the waypoint-painter
        List<Painter<JXMapViewer>> painters = new ArrayList<Painter<JXMapViewer>>();
        painters.add(routePainter);
        painters.add(waypointPainter);

        CompoundPainter<JXMapViewer> painter = new CompoundPainter<JXMapViewer>(painters);
        mapViewer.setOverlayPainter(painter);


        mapViewer.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent me) {
                Point2D gp_pt = null;

                for (GeoPosition waypoint : track) {
                    //convert to world bitmap
                    gp_pt = mapViewer.getTileFactory().geoToPixel(waypoint, mapViewer.getZoom());
                }

                //convert to screen
                Rectangle rect = mapViewer.getViewportBounds();
                Point converted_gp_pt = new Point((int) gp_pt.getX() - rect.x,
                        (int) gp_pt.getY() - rect.y);
                //check if near the mouse
                if (converted_gp_pt.distance(me.getPoint()) < 10) {
                    System.out.println("mapViewer mouse click has been detected within 10 pixels of a waypoint");
                } else {
                    System.out.println("mapViewer mouse click has been dected but NOT with 10 pixels of a waypoint ");
                }

            }
        }); // end MouseAdapter

        // Display the viewer in a JFrame
        JFrame frame = new JFrame("JXMapviewer2 Example 4");
        frame.getContentPane().add(mapViewer);
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public static JXMapViewer getMapViewer() {
        return mapViewer;
    }
}

1 个答案:

答案 0 :(得分:1)

我认为如果你想检查所有航路点,你应该添加&#34; //转换到屏幕&#34;阻止和&#34; //检查是否靠近鼠标&#34;进入for,即:

       public void mouseClicked(MouseEvent me) {
            Point2D gp_pt = null;

            for (GeoPosition waypoint : track) {
                //convert to world bitmap
                gp_pt = mapViewer.getTileFactory().geoToPixel(waypoint, mapViewer.getZoom());

                //convert to screen
                Rectangle rect = mapViewer.getViewportBounds();
                Point converted_gp_pt = new Point((int) gp_pt.getX() - rect.x,
                    (int) gp_pt.getY() - rect.y);
                //check if near the mouse
                if (converted_gp_pt.distance(me.getPoint()) < 10) {
                    System.out.println("mapViewer mouse click has been detected within 10 pixels of a waypoint");
                } else {
                    System.out.println("mapViewer mouse click has been dected but NOT with 10 pixels of a waypoint ");
                }
            }

        }