JMapViewer:刚刚加载地图时未设置平移和缩放

时间:2016-08-14 16:34:53

标签: java zooming jmapviewer

我在JMapViewer上有两个自定义图像作为标记。我希望地图设置正确的平移和缩放,以便在地图显示上可以看到两个标记。我有一个按钮就可以了。所以,正在发生的事情是地图在加载时不会缩放到这些标记;但是当我点击按钮时,确实如此。有没有办法在加载时设置正确的平移和缩放?

public class PanelAcars extends javax.swing.JPanel implements JMapViewerEventListener  
{
    private final JMapViewerTree treeMap;

    public PanelAcars() 
    {
        super();
        initComponents(); // GUI components

        treeMap = new JMapViewerTree("Zones");

        // Listen to the map viewer for user operations so components will
        // receive events and update
        map().addJMVListener(this);

        setLayout(new BorderLayout());

        pnlAcarsMapView = new JPanel(new BorderLayout());
        JPanel panelTop = new JPanel();
        JPanel panelBottom = new JPanel();
        JPanel helpPanel = new JPanel();

        add(pnlAcarsMapView, BorderLayout.NORTH);
        add(helpPanel, BorderLayout.SOUTH);
        pnlAcarsMapView.add(panelTop, BorderLayout.NORTH);
        pnlAcarsMapView.add(panelBottom, BorderLayout.SOUTH);

        /* THIS BUTTON WORKS!!! */
        JButton button = new JButton("Zoom...");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                map().setDisplayToFitMapMarkers();
            }
        });


        final JCheckBox showZoomControls = new JCheckBox("Show zoom controls");
        showZoomControls.setSelected(map().getZoomControlsVisible());
        showZoomControls.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                map().setZoomContolsVisible(showZoomControls.isSelected());
            }
        });
        panelBottom.add(showZoomControls);
        final JCheckBox scrollWrapEnabled = new JCheckBox("Scrollwrap enabled");
        scrollWrapEnabled.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                map().setScrollWrapEnabled(scrollWrapEnabled.isSelected());
            }
        });
        panelBottom.add(scrollWrapEnabled);
        panelBottom.add(button);

        add(treeMap, BorderLayout.CENTER);

        /* Add Airports to the map */
        LayerGroup mapGroupAirports = new LayerGroup("MapAirports");
        Layer mapLayerDep = mapGroupAirports.addLayer("MapDep");
        Layer mapLayerDest = mapGroupAirports.addLayer("MapDest");
        Layer mapLayerAlt = mapGroupAirports.addLayer("MapAlt");


        /* Add Plane to the map */
        Layer mapLayerPlane = new Layer("MapPlane");

        /* Set coordinates */
        Coordinate coordinatesPlane = new Coordinate(49.814284999, 98.642065999);
        Coordinate coordinatesDep = new Coordinate(23.814284999, 45.642065999);

        JMapViewerMarkerCustomImage markerPlane;
        JMapViewerMarkerCustomImage markerDep;

        Image imageMarkerPlane;
        Image imageMarkerDep;

        try 
        {
            imageMarkerPlane = ImageIO.read(this.getClass().getResource("/images/image1.png"));
            markerPlane = new JMapViewerMarkerCustomImage(coordinatesPlane,imageMarkerPlane);
            map().addMapMarker(markerPlane);

            imageMarkerDep = ImageIO.read(this.getClass().getResource("/images/image2.png"));
            markerDep = new JMapViewerMarkerCustomImage(coordinatesDep,imageMarkerDep);
            map().addMapMarker(markerDep);

            // AND THIS IS NOT WORKING AT LOADING!!!
            map().setDisplayToFitMapElements(true, true, true);

        } catch (IOException ex) {
            ex.printStackTrace();
        }


        map().addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    map().getAttribution().handleAttribution(e.getPoint(), true);
                }
            }
        });

        map().addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseMoved(MouseEvent e) {
                Point p = e.getPoint();
                boolean cursorHand = map().getAttribution().handleAttributionCursor(p);
                if (cursorHand) {
                    map().setCursor(new Cursor(Cursor.HAND_CURSOR));
                } else {
                    map().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
                }
                //if (showToolTip.isSelected()) map().setToolTipText(map().getPosition(p).toString());
            }
        });

    }

非常感谢!

1 个答案:

答案 0 :(得分:2)

在地图的setDisplayToFitMapMarkers()实施中调用tileLoadingFinished()。使用boolean变量loaded,默认为false,下面的实现会在第一个tile加载后调用该方法;父实现将repaint()任何后续的tile。

private boolean loaded;

private class MyViewer extends JMapViewer {

    @Override
    public void tileLoadingFinished(Tile tile, boolean success) {
        super.tileLoadingFinished(tile, success);
        if (!loaded & success) {
            loaded = true;
            setDisplayToFitMapMarkers();
        }
    }
    …
}

要使用JMapViewerTree获得类似的结果,您需要更改map构造函数中实例化的JMapViewerTree。以下更改适用于Demo.java

…
private boolean loaded;
…
public JMapViewerTree(String name, boolean treeVisible) {
    …
    map = new JMapViewer() {
        @Override
        public void tileLoadingFinished(Tile tile, boolean success) {
            super.tileLoadingFinished(tile, success);
            if (!loaded & success) {
                loaded = true;
                setDisplayToFitMapMarkers();
            }
        }
    };
    …
}

顺便说一句,不如MouseAdapter,而是考虑按照建议here扩展或替换DefaultMapController