diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2013-01-09 20:32:40 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2013-01-09 20:32:40 +1300 |
commit | f0b99fd68cb3b6b484d50c1f9ebc0e77997fd2dd (patch) | |
tree | cd9beb89c07dbeacbb26d9df8a1a42502d206c74 /src/org/traccar/web/client/view/MapView.java | |
parent | a7c6a5f03c0ac94603b5238c59a275a239d16b73 (diff) | |
download | trackermap-web-f0b99fd68cb3b6b484d50c1f9ebc0e77997fd2dd.tar.gz trackermap-web-f0b99fd68cb3b6b484d50c1f9ebc0e77997fd2dd.tar.bz2 trackermap-web-f0b99fd68cb3b6b484d50c1f9ebc0e77997fd2dd.zip |
Completely rewritten project
Diffstat (limited to 'src/org/traccar/web/client/view/MapView.java')
-rw-r--r-- | src/org/traccar/web/client/view/MapView.java | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/org/traccar/web/client/view/MapView.java b/src/org/traccar/web/client/view/MapView.java new file mode 100644 index 00000000..af38c443 --- /dev/null +++ b/src/org/traccar/web/client/view/MapView.java @@ -0,0 +1,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; + } + } + +} |