JMapViewer禁用缩放

时间:2014-02-25 01:18:14

标签: java google-maps mouseevent zooming jmapviewer

有没有办法阻止地图从certian缩放级别缩放到外面? 我试图取消鼠标滚轮滚动事件(如果它缩小了),并隐藏放大/缩小控制。

map.addMouseWheelListener(new MouseWheelListener() {
@Override
public void mouseWheelMoved(MouseWheelEvent arg0) {
    int newZoom = map.getZoom();
    if (lastZoom < newZoom) {
        zoomIn = true;
    } else {
        arg0.consume();
        zoomIn = false;
    }

只是为了确保:我想允许缩放,但直到只有特定的缩放级别。

谢谢,

1 个答案:

答案 0 :(得分:1)

您可以将JMapViewer扩展为覆盖setZoom()方法。例如:

import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import org.openstreetmap.gui.jmapviewer.JMapViewer;

public class TestMapZoom {

    static class CustomMap extends JMapViewer {
        private int maxZoomLevel;

        public CustomMap() {
            this.maxZoomLevel = 7;
        }

        public int getMaxZoomLevel() {
            return maxZoomLevel;
        }

        public void setMaxZoomLevel(int maxZoom) {
            this.maxZoomLevel = maxZoom;
        }

        @Override
        public void setZoom(int zoom, Point mapPoint) {
            if (zoom < getMaxZoomLevel())
                super.setZoom(zoom, mapPoint);
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Demo");
        CustomMap viewer = new CustomMap();

        frame.add(viewer);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}

编辑没有JMapViewer分机的版本

另一种不需要扩展JMapViewer的略微更清晰的方法可能是地图使用的控制器的替代品。默认JMapViewer构造函数安装DefaultMapController,此示例使用另一个构造函数和控制器的扩展版本:

import java.awt.event.MouseWheelEvent;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import org.openstreetmap.gui.jmapviewer.DefaultMapController;
import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.MemoryTileCache;

public class TestMapZoom2 {

    static class CustomMapController extends DefaultMapController {
        private int maxZoomLevel;

        public CustomMapController(JMapViewer map) {
            super(map);
            this.maxZoomLevel = 7;
        }

        public int getMaxZoomLevel() {
            return maxZoomLevel;
        }

        public void setMaxZoomLevel(int maxZoom) {
            this.maxZoomLevel = maxZoom;
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) { 
            if (e.getWheelRotation() < 0 && map.getZoom() >= getMaxZoomLevel())
                return;
            super.mouseWheelMoved(e);   
        }
    }

    private static void createAndShowUI() {
        JFrame frame = new JFrame("Demo");
        JMapViewer viewer = new JMapViewer(new MemoryTileCache(), 8);
        new CustomMapController(viewer);

        frame.add(viewer);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowUI();
            }
        });
    }
}