/
TrafficLayerExample.java
185 lines (159 loc) · 7.21 KB
/
TrafficLayerExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
* Copyright (c) 2000-2017 TeamDev Ltd. All rights reserved.
* Use is subject to Apache 2.0 license terms.
*/
package com.teamdev.jxmaps.examples;
import com.teamdev.jxmaps.*;
import com.teamdev.jxmaps.swing.MapView;
import javax.swing.*;
import javax.swing.plaf.basic.BasicButtonUI;
import javax.swing.plaf.basic.BasicTextFieldUI;
import java.awt.*;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* This example demonstrates how to display traffic layer on the map.
*
* @author Vitaly Eremenko
* @author Sergei Piletsky
*/
public class TrafficLayerExample extends MapView {
private static final String INITIAL_LOCATION = "New York";
private OptionsWindow optionsWindow;
public TrafficLayerExample() {
// Setting of a ready handler to MapView object. onMapReady will be called when map initialization is done and
// the map object is ready to use. Current implementation of onMapReady customizes the map object.
setOnMapReadyHandler(new MapReadyHandler() {
@Override
public void onMapReady(MapStatus status) {
// Getting the associated map object
final Map map = getMap();
// Setting initial zoom value
map.setZoom(13.0);
// Creating a map options object
MapOptions options = new MapOptions();
// Creating a map type control options object
MapTypeControlOptions controlOptions = new MapTypeControlOptions();
// Changing position of the map type control
controlOptions.setPosition(ControlPosition.TOP_RIGHT);
// Setting map type control options
options.setMapTypeControlOptions(controlOptions);
// Setting map options
map.setOptions(options);
// Enabling of traffic layer on the map
new TrafficLayer(map);
performGeocode(INITIAL_LOCATION);
}
});
}
@Override
public void addNotify() {
super.addNotify();
optionsWindow = new OptionsWindow(this, new Dimension(350, 40)) {
@Override
public void initContent(JWindow contentWindow) {
JPanel content = new JPanel(new GridBagLayout());
content.setBackground(Color.white);
Font robotoPlain13 = new Font("Roboto", 0, 13);
final JTextField searchField = new JTextField();
searchField.setText(INITIAL_LOCATION);
searchField.setToolTipText("Enter address or coordinates...");
searchField.setBorder(BorderFactory.createEmptyBorder());
searchField.setFont(robotoPlain13);
searchField.setForeground(new Color(0x21, 0x21, 0x21));
searchField.setUI(new SearchFieldUI(searchField));
final JButton searchButton = new JButton();
searchButton.setIcon(new ImageIcon(MapOptionsExample.class.getResource("res/search.png")));
searchButton.setRolloverIcon(new ImageIcon(MapOptionsExample.class.getResource("res/search_hover.png")));
searchButton.setBorder(BorderFactory.createEmptyBorder());
searchButton.setUI(new BasicButtonUI());
searchButton.setOpaque(false);
ActionListener searchActionListener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
performGeocode(searchField.getText());
}
};
searchButton.addActionListener(searchActionListener);
searchField.addActionListener(searchActionListener);
content.add(searchField, new GridBagConstraints(0, 0, 1, 1, 1.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(11, 11, 11, 0), 0, 0));
content.add(searchButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.NORTHWEST, GridBagConstraints.NONE, new Insets(11, 0, 11, 11), 0, 0));
contentWindow.getContentPane().add(content);
}
@Override
protected void updatePosition() {
if (parentFrame.isVisible()) {
Point newLocation = parentFrame.getContentPane().getLocationOnScreen();
newLocation.translate(56, 11);
contentWindow.setLocation(newLocation);
contentWindow.setSize(340, 40);
}
}
};
}
class SearchFieldUI extends BasicTextFieldUI {
private final JTextField textField;
public SearchFieldUI(JTextField textField) {
this.textField = textField;
}
@Override
protected void paintBackground(Graphics g) {
super.paintBackground(g);
String toolTipText = textField.getToolTipText();
String text = textField.getText();
if (toolTipText != null && text.isEmpty()) {
paintPlaceholderText(g, textField);
}
}
protected void paintPlaceholderText(Graphics g, JComponent c) {
g.setColor(new Color(0x75, 0x75, 0x75));
g.setFont(c.getFont());
String text = textField.getToolTipText();
if (g instanceof Graphics2D) {
Graphics2D graphics2D = (Graphics2D) g;
graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
}
g.drawString(text, 0, 14);
}
}
@Override
public void removeNotify() {
super.removeNotify();
optionsWindow.dispose();
}
private void performGeocode(String text) {
// Getting the associated map object
final Map map = getMap();
// Creating a geocode request
GeocoderRequest request = new GeocoderRequest();
// Setting address to the geocode request
request.setAddress(text);
// Geocoding position by the entered address
getServices().getGeocoder().geocode(request, new GeocoderCallback(map) {
@Override
public void onComplete(GeocoderResult[] results, GeocoderStatus status) {
// Checking operation status
if ((status == GeocoderStatus.OK) && (results.length > 0)) {
// Getting the first result
GeocoderResult result = results[0];
// Getting a location of the result
LatLng location = result.getGeometry().getLocation();
// Setting the map center to result location
map.setCenter(location);
}
}
});
}
public static void main(String[] args) {
final TrafficLayerExample mapView = new TrafficLayerExample();
JFrame frame = new JFrame("Traffic layer");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(mapView, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}