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
|
package org.traccar.web.client.view;
import java.util.AbstractMap;
import java.util.HashMap;
import java.util.List;
import org.gwtopenmaps.openlayers.client.Icon;
import org.gwtopenmaps.openlayers.client.LonLat;
import org.gwtopenmaps.openlayers.client.Map;
import org.gwtopenmaps.openlayers.client.MapOptions;
import org.gwtopenmaps.openlayers.client.MapWidget;
import org.gwtopenmaps.openlayers.client.Marker;
import org.gwtopenmaps.openlayers.client.Pixel;
import org.gwtopenmaps.openlayers.client.Projection;
import org.gwtopenmaps.openlayers.client.Size;
import org.gwtopenmaps.openlayers.client.control.ScaleLine;
import org.gwtopenmaps.openlayers.client.layer.Markers;
import org.gwtopenmaps.openlayers.client.layer.OSM;
import org.traccar.web.shared.model.Device;
import org.traccar.web.shared.model.Position;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.user.client.Command;
import com.sencha.gxt.widget.core.client.ContentPanel;
class MarkerIconFactory {
private static final Size iconSize = new Size(21, 25);
private static final Pixel iconOffset = new Pixel(-10.5f, -25.0f);
private static final String iconUrl = "http://www.openlayers.org/api/img/";
private static final String iconRed = iconUrl + "marker.png";
private static final String iconBlue = iconUrl + "marker-blue.png";
private static final String iconGreen = iconUrl + "marker-green.png";
private static final String iconGold = iconUrl + "marker-gold.png";
public static Icon getLocationIcon() {
return new Icon(iconRed, iconSize, iconOffset);
}
public static Icon getSelectedLocationIcon() {
return new Icon(iconGreen, iconSize, iconOffset);
}
public static Icon getArchiveIcon() {
return new Icon(iconBlue, iconSize, iconOffset);
}
public static Icon getSelectedArchiveIcon() {
return new Icon(iconGold, iconSize, iconOffset);
}
}
public class MapView {
private ContentPanel contentPanel;
public ContentPanel getView() {
return contentPanel;
}
private MapWidget mapWidget;
private Map map;
private Markers markerLayer;
private LonLat createPoint(double longitude, double latitude) {
LonLat point = new LonLat(longitude, latitude);
point.transform(new Projection("EPSG:4326").getProjectionCode(), map.getProjection());
return point;
}
public MapView() {
contentPanel = new ContentPanel();
contentPanel.setHeadingText("Map");
MapOptions defaultMapOptions = new MapOptions();
defaultMapOptions.setNumZoomLevels(16);
mapWidget = new MapWidget("100%", "100%", defaultMapOptions);
map = mapWidget.getMap();
OSM mapLayer = OSM.Mapnik("Mapnik");
mapLayer.setIsBaseLayer(true);
markerLayer = new Markers("Markers");
map.addLayer(mapLayer);
map.addLayer(markerLayer);
map.addControl(new ScaleLine());
map.setCenter(createPoint(30, 60), 1);
contentPanel.add(mapWidget);
// Update map size
contentPanel.addResizeHandler(new ResizeHandler() {
@Override
public void onResize(ResizeEvent event) {
Scheduler.get().scheduleDeferred(new Command() {
@Override
public void execute() {
map.updateSize();
}
});
}
});
}
private AbstractMap<Long, Marker> markerMap = new HashMap<Long, Marker>();
private Device selectedDevice;
public void showPositions(List<Position> positions) {
markerMap.clear();
markerLayer.clearMarkers();
for (Position position : positions) {
Marker marker = new Marker(
createPoint(position.getLongitude(), position.getLatitude()), MarkerIconFactory.getLocationIcon());
markerMap.put(position.getDevice().getId(), marker);
markerLayer.addMarker(marker);
}
if (selectedDevice != null) {
select(selectedDevice, false);
}
}
private void changeMarkerIcon(Marker marker, Icon icon) {
Marker newMarker = new Marker(marker.getLonLat(), icon);
markerLayer.removeMarker(marker);
markerLayer.addMarker(newMarker);
}
public void select(Device device, boolean center) {
if (selectedDevice != null) {
changeMarkerIcon(markerMap.get(selectedDevice.getId()), MarkerIconFactory.getLocationIcon());
selectedDevice = null;
}
if (device != null && markerMap.containsKey(device.getId())) {
Marker marker = markerMap.get(device.getId());
if (center) {
map.panTo(marker.getLonLat());
}
changeMarkerIcon(marker, MarkerIconFactory.getSelectedLocationIcon());
selectedDevice = device;
}
}
}
|