Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

248
LINES

< > BotCompany Repo | #1015765 // JMapViewer Demo [WORKS]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (7034L/46K).

1  
!7
2  
3  
lib 1400033 // JMapViewer 2.7
4  
5  
import org.openstreetmap.gui.jmapviewer.*;
6  
import org.openstreetmap.gui.jmapviewer.events.JMVCommandEvent;
7  
import org.openstreetmap.gui.jmapviewer.interfaces.JMapViewerEventListener;
8  
import org.openstreetmap.gui.jmapviewer.interfaces.MapPolygon;
9  
import org.openstreetmap.gui.jmapviewer.interfaces.TileLoader;
10  
import org.openstreetmap.gui.jmapviewer.interfaces.TileSource;
11  
import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
12  
import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;
13  
14  
p-substance { callMain(Demo); hideConsole(); }
15  
16  
/**
17  
 * Demonstrates the usage of {@link JMapViewer}
18  
 *
19  
 * @author Jan Peter Stotz
20  
 *
21  
 */
22  
sclass Demo extends JFrame implements JMapViewerEventListener {
23  
24  
    private static final long serialVersionUID = 1L;
25  
26  
    private final JMapViewerTree treeMap;
27  
28  
    private final JLabel zoomLabel;
29  
    private final JLabel zoomValue;
30  
31  
    private final JLabel mperpLabelName;
32  
    private final JLabel mperpLabelValue;
33  
34  
    /**
35  
     * Constructs the {@code Demo}.
36  
     */
37  
    public Demo() {
38  
        super("JMapViewer Demo");
39  
        setSize(400, 400);
40  
41  
        treeMap = new JMapViewerTree("Zones");
42  
43  
        // Listen to the map viewer for user operations so components will
44  
        // receive events and update
45  
        map().addJMVListener(this);
46  
47  
        setLayout(new BorderLayout());
48  
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
49  
        setExtendedState(JFrame.MAXIMIZED_BOTH);
50  
        JPanel panel = new JPanel(new BorderLayout());
51  
        JPanel panelTop = new JPanel();
52  
        JPanel panelBottom = new JPanel();
53  
        JPanel helpPanel = new JPanel();
54  
55  
        mperpLabelName = new JLabel("Meters/Pixels: ");
56  
        mperpLabelValue = new JLabel(String.format("%s", map().getMeterPerPixel()));
57  
58  
        zoomLabel = new JLabel("Zoom: ");
59  
        zoomValue = new JLabel(String.format("%s", map().getZoom()));
60  
61  
        add(panel, BorderLayout.NORTH);
62  
        add(helpPanel, BorderLayout.SOUTH);
63  
        panel.add(panelTop, BorderLayout.NORTH);
64  
        panel.add(panelBottom, BorderLayout.SOUTH);
65  
        JLabel helpLabel = new JLabel("Use right mouse button to move,\n "
66  
                + "left double click or mouse wheel to zoom.");
67  
        helpPanel.add(helpLabel);
68  
        JButton button = new JButton("setDisplayToFitMapMarkers");
69  
        button.addActionListener(new ActionListener() {
70  
            @Override
71  
            public void actionPerformed(ActionEvent e) {
72  
                map().setDisplayToFitMapMarkers();
73  
            }
74  
        });
75  
        JComboBox<TileSource> tileSourceSelector = new JComboBox<>(new TileSource[] {
76  
                new OsmTileSource.Mapnik(),
77  
                new OsmTileSource.CycleMap(),
78  
                new BingAerialTileSource(),
79  
        });
80  
        tileSourceSelector.addItemListener(new ItemListener() {
81  
            @Override
82  
            public void itemStateChanged(ItemEvent e) {
83  
                map().setTileSource((TileSource) e.getItem());
84  
            }
85  
        });
86  
        JComboBox<TileLoader> tileLoaderSelector;
87  
        tileLoaderSelector = new JComboBox<>(new TileLoader[] {new OsmTileLoader(map())});
88  
        tileLoaderSelector.addItemListener(new ItemListener() {
89  
            @Override
90  
            public void itemStateChanged(ItemEvent e) {
91  
                map().setTileLoader((TileLoader) e.getItem());
92  
            }
93  
        });
94  
        map().setTileLoader((TileLoader) tileLoaderSelector.getSelectedItem());
95  
        panelTop.add(tileSourceSelector);
96  
        panelTop.add(tileLoaderSelector);
97  
        final JCheckBox showMapMarker = new JCheckBox("Map markers visible");
98  
        showMapMarker.setSelected(map().getMapMarkersVisible());
99  
        showMapMarker.addActionListener(new ActionListener() {
100  
            @Override
101  
            public void actionPerformed(ActionEvent e) {
102  
                map().setMapMarkerVisible(showMapMarker.isSelected());
103  
            }
104  
        });
105  
        panelBottom.add(showMapMarker);
106  
        ///
107  
        final JCheckBox showTreeLayers = new JCheckBox("Tree Layers visible");
108  
        showTreeLayers.addActionListener(new ActionListener() {
109  
            @Override
110  
            public void actionPerformed(ActionEvent e) {
111  
                treeMap.setTreeVisible(showTreeLayers.isSelected());
112  
            }
113  
        });
114  
        panelBottom.add(showTreeLayers);
115  
        ///
116  
        final JCheckBox showToolTip = new JCheckBox("ToolTip visible");
117  
        showToolTip.addActionListener(new ActionListener() {
118  
            @Override
119  
            public void actionPerformed(ActionEvent e) {
120  
                map().setToolTipText(null);
121  
            }
122  
        });
123  
        panelBottom.add(showToolTip);
124  
        ///
125  
        final JCheckBox showTileGrid = new JCheckBox("Tile grid visible");
126  
        showTileGrid.setSelected(map().isTileGridVisible());
127  
        showTileGrid.addActionListener(new ActionListener() {
128  
            @Override
129  
            public void actionPerformed(ActionEvent e) {
130  
                map().setTileGridVisible(showTileGrid.isSelected());
131  
            }
132  
        });
133  
        panelBottom.add(showTileGrid);
134  
        final JCheckBox showZoomControls = new JCheckBox("Show zoom controls");
135  
        showZoomControls.setSelected(map().getZoomControlsVisible());
136  
        showZoomControls.addActionListener(new ActionListener() {
137  
            @Override
138  
            public void actionPerformed(ActionEvent e) {
139  
                map().setZoomContolsVisible(showZoomControls.isSelected());
140  
            }
141  
        });
142  
        panelBottom.add(showZoomControls);
143  
        final JCheckBox scrollWrapEnabled = new JCheckBox("Scrollwrap enabled");
144  
        scrollWrapEnabled.addActionListener(new ActionListener() {
145  
            @Override
146  
            public void actionPerformed(ActionEvent e) {
147  
                map().setScrollWrapEnabled(scrollWrapEnabled.isSelected());
148  
            }
149  
        });
150  
        panelBottom.add(scrollWrapEnabled);
151  
        panelBottom.add(button);
152  
153  
        panelTop.add(zoomLabel);
154  
        panelTop.add(zoomValue);
155  
        panelTop.add(mperpLabelName);
156  
        panelTop.add(mperpLabelValue);
157  
158  
        add(treeMap, BorderLayout.CENTER);
159  
160  
        //
161  
        LayerGroup germanyGroup = new LayerGroup("Germany");
162  
        Layer germanyWestLayer = germanyGroup.addLayer("Germany West");
163  
        Layer germanyEastLayer = germanyGroup.addLayer("Germany East");
164  
        MapMarkerDot eberstadt = new MapMarkerDot(germanyEastLayer, "Eberstadt", 49.814284999, 8.642065999);
165  
        MapMarkerDot ebersheim = new MapMarkerDot(germanyWestLayer, "Ebersheim", 49.91, 8.24);
166  
        MapMarkerDot empty = new MapMarkerDot(germanyEastLayer, 49.71, 8.64);
167  
        MapMarkerDot darmstadt = new MapMarkerDot(germanyEastLayer, "Darmstadt", 49.8588, 8.643);
168  
        map().addMapMarker(eberstadt);
169  
        map().addMapMarker(ebersheim);
170  
        map().addMapMarker(empty);
171  
        Layer franceLayer = treeMap.addLayer("France");
172  
        map().addMapMarker(new MapMarkerDot(franceLayer, "La Gallerie", 48.71, -1));
173  
        map().addMapMarker(new MapMarkerDot(43.604, 1.444));
174  
        map().addMapMarker(new MapMarkerCircle(53.343, -6.267, 0.666));
175  
        map().addMapRectangle(new MapRectangleImpl(new Coordinate(53.343, -6.267), new Coordinate(43.604, 1.444)));
176  
        map().addMapMarker(darmstadt);
177  
        treeMap.addLayer(germanyWestLayer);
178  
        treeMap.addLayer(germanyEastLayer);
179  
180  
        MapPolygon bermudas = new MapPolygonImpl(c(49, 1), c(45, 10), c(40, 5));
181  
        map().addMapPolygon(bermudas);
182  
        map().addMapPolygon(new MapPolygonImpl(germanyEastLayer, "Riedstadt", ebersheim, darmstadt, eberstadt, empty));
183  
184  
        map().addMapMarker(new MapMarkerCircle(germanyWestLayer, "North of Suisse", new Coordinate(48, 7), .5));
185  
        Layer spain = treeMap.addLayer("Spain");
186  
        map().addMapMarker(new MapMarkerCircle(spain, "La Garena", new Coordinate(40.4838, -3.39), .002));
187  
        spain.setVisible(Boolean.FALSE);
188  
189  
        Layer wales = treeMap.addLayer("UK");
190  
        map().addMapRectangle(new MapRectangleImpl(wales, "Wales", c(53.35, -4.57), c(51.64, -2.63)));
191  
192  
        // map.setDisplayPosition(new Coordinate(49.807, 8.6), 11);
193  
        // map.setTileGridVisible(true);
194  
195  
        map().addMouseListener(new MouseAdapter() {
196  
            @Override
197  
            public void mouseClicked(MouseEvent e) {
198  
                if (e.getButton() == MouseEvent.BUTTON1) {
199  
                    map().getAttribution().handleAttribution(e.getPoint(), true);
200  
                }
201  
            }
202  
        });
203  
204  
        map().addMouseMotionListener(new MouseAdapter() {
205  
            @Override
206  
            public void mouseMoved(MouseEvent e) {
207  
                Point p = e.getPoint();
208  
                boolean cursorHand = map().getAttribution().handleAttributionCursor(p);
209  
                if (cursorHand) {
210  
                    map().setCursor(new Cursor(Cursor.HAND_CURSOR));
211  
                } else {
212  
                    map().setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
213  
                }
214  
                if (showToolTip.isSelected()) map().setToolTipText(map().getPosition(p).toString());
215  
            }
216  
        });
217  
    }
218  
219  
    private JMapViewer map() {
220  
        return treeMap.getViewer();
221  
    }
222  
223  
    private static Coordinate c(double lat, double lon) {
224  
        return new Coordinate(lat, lon);
225  
    }
226  
227  
    /**
228  
     * @param args Main program arguments
229  
     */
230  
    public static void main(String[] args) {
231  
        new Demo().setVisible(true);
232  
    }
233  
234  
    private void updateZoomParameters() {
235  
        if (mperpLabelValue != null)
236  
            mperpLabelValue.setText(String.format("%s", map().getMeterPerPixel()));
237  
        if (zoomValue != null)
238  
            zoomValue.setText(String.format("%s", map().getZoom()));
239  
    }
240  
241  
    @Override
242  
    public void processCommand(JMVCommandEvent command) {
243  
        if (command.getCommand().equals(JMVCommandEvent.COMMAND.ZOOM) ||
244  
                command.getCommand().equals(JMVCommandEvent.COMMAND.MOVE)) {
245  
            updateZoomParameters();
246  
        }
247  
    }
248  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1015765
Snippet name: JMapViewer Demo [WORKS]
Eternal ID of this version: #1015765/5
Text MD5: 58efe68704b59405b6f7db991e7eebd8
Transpilation MD5: 46f1dbd79c5247c27e8b8064ed409d7e
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-27 15:25:06
Source code size: 10217 bytes / 248 lines
Pitched / IR pitched: No / No
Views / Downloads: 525 / 1129
Version history: 4 change(s)
Referenced in: [show references]