Skip to content

Commit

Permalink
For #85 - implemented drawing icons on map
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalidze committed May 28, 2015
1 parent 188ad4a commit 57eaabc
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.traccar.web.client.GeoFenceDrawing;
import org.traccar.web.client.Track;
import org.traccar.web.client.view.MapView;
import org.traccar.web.client.view.MarkerIcon;
import org.traccar.web.shared.model.*;

import java.util.Arrays;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void onSuccess(List<Position> result) {
Device device = position.getDevice();
boolean isOffline = currentTime - position.getTime().getTime() > position.getDevice().getTimeout() * 1000;
position.setStatus(isOffline ? Position.Status.OFFLINE : Position.Status.LATEST);
position.setIconType(device.getIconType().getPositionIconType(position.getStatus()));
position.setIcon(MarkerIcon.create(position));
if (position.getSpeed() != null) {
if (position.getSpeed().doubleValue() > position.getDevice().getIdleSpeedThreshold()) {
latestNonIdlePositionMap.put(device.getId(), position);
Expand Down Expand Up @@ -183,8 +184,10 @@ public void selectDevice(Device device) {

public void showArchivePositions(Track track) {
List<Position> positions = track.getPositions();
PositionIcon icon = new PositionIcon(track.getStyle().getIconType() == null ?
PositionIconType.dotArchive : track.getStyle().getIconType());
for (Position position : positions) {
position.setIconType(track.getStyle().getIconType() == null ? PositionIconType.dotArchive : track.getStyle().getIconType());
position.setIcon(icon);
}
mapView.showArchiveTrack(track);

Expand Down
6 changes: 1 addition & 5 deletions src/main/java/org/traccar/web/client/view/DeviceDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ public interface DeviceHandler {

public DeviceDialog(Device device, DeviceHandler deviceHandler) {
this.deviceHandler = deviceHandler;
if (device.getIconType() == null) {
selectedIcon = new MarkerIcon.Database(device.getIcon());
} else {
selectedIcon = new MarkerIcon.BuiltIn(device.getIconType());
}
this.selectedIcon = MarkerIcon.create(device);

uiBinder.createAndBindUi(this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public void showPositions(List<Position> positions) {
for (Position position : positions) {
Marker marker = new Marker(
mapView.createLonLat(position.getLongitude(), position.getLatitude()),
MarkerIconFactory.getIcon(position.getIconType(), false));
MarkerIconFactory.getIcon(position.getIcon(), false));
deviceData.markerMap.put(position.getId(), marker);
addSelectEvent(marker, position);
addMouseEvent(marker, position);
Expand Down Expand Up @@ -446,13 +446,13 @@ private boolean selectPosition(Position oldPosition, Position newPosition, boole
if (oldPosition != null) {
DeviceData deviceData = getDeviceData(oldPosition.getDevice());
if (deviceData.markerMap.containsKey(oldPosition.getId())) {
changeMarkerIcon(oldPosition, MarkerIconFactory.getIcon(oldPosition.getIconType(), false));
changeMarkerIcon(oldPosition, MarkerIconFactory.getIcon(oldPosition.getIcon(), false));
}
}
if (newPosition != null) {
DeviceData deviceData = getDeviceData(newPosition.getDevice());
if (deviceData.markerMap.containsKey(newPosition.getId())) {
changeMarkerIcon(newPosition, MarkerIconFactory.getIcon(newPosition.getIconType(), true));
changeMarkerIcon(newPosition, MarkerIconFactory.getIcon(newPosition.getIcon(), true));
if (center) {
mapView.getMap().panTo(deviceData.markerMap.get(newPosition.getId()).getLonLat());
}
Expand Down Expand Up @@ -493,9 +493,9 @@ public void updateIcon(Device device) {
Position position = deviceData.positions == null || deviceData.positions.size() != 1 ? null : deviceData.positions.get(0);
if (position != null) {
position.setDevice(device);
position.setIconType(device.getIconType().getPositionIconType(position.getStatus()));
position.setIcon(MarkerIcon.create(position));
boolean selected = selectedPosition != null && selectedPosition.getId() == position.getId();
changeMarkerIcon(position, MarkerIconFactory.getIcon(position.getIconType(), selected));
changeMarkerIcon(position, MarkerIconFactory.getIcon(position.getIcon(), selected));
}
}
}
87 changes: 84 additions & 3 deletions src/main/java/org/traccar/web/client/view/MarkerIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,31 @@
*/
package org.traccar.web.client.view;

import org.traccar.web.shared.model.DeviceIcon;
import org.traccar.web.shared.model.DeviceIconType;
import org.traccar.web.shared.model.Position;
import org.traccar.web.shared.model.*;

public abstract class MarkerIcon {
abstract String getKey();

abstract String getDefaultURL();
abstract int getDefaultWidth();
abstract int getDefaultHeight();

abstract String getSelectedURL();
int getSelectedWidth() {
return getDefaultWidth();
}
int getSelectedHeight() {
return getDefaultHeight();
}

abstract String getOfflineURL();
int getOfflineWidth() {
return getDefaultWidth();
}
int getOfflineHeight() {
return getDefaultHeight();
}

DeviceIconType getBuiltInIcon() {
return null;
}
Expand Down Expand Up @@ -53,6 +69,16 @@ String getDefaultURL() {
return icon.getPositionIconType(Position.Status.LATEST).getURL(false);
}

@Override
int getDefaultWidth() {
return icon.getPositionIconType(Position.Status.LATEST).getWidth();
}

@Override
int getDefaultHeight() {
return icon.getPositionIconType(Position.Status.LATEST).getHeight();
}

@Override
String getSelectedURL() {
return icon.getPositionIconType(Position.Status.LATEST).getURL(true);
Expand Down Expand Up @@ -81,19 +107,74 @@ String getDefaultURL() {
return icon.defaultURL();
}

@Override
int getDefaultWidth() {
return getWidth(icon.getDefaultIcon());
}

@Override
int getDefaultHeight() {
return getHeight(icon.getDefaultIcon());
}

@Override
String getSelectedURL() {
return icon.selectedURL();
}

@Override
int getSelectedWidth() {
return getWidth(icon.getSelectedIcon());
}

@Override
int getSelectedHeight() {
return getHeight(icon.getSelectedIcon());
}

@Override
String getOfflineURL() {
return icon.offlineURL();
}

@Override
int getOfflineWidth() {
return getWidth(icon.getOfflineIcon());
}

@Override
int getOfflineHeight() {
return getHeight(icon.getOfflineIcon());
}

@Override
DeviceIcon getDatabaseIcon() {
return icon;
}

private int getWidth(Picture pic) {
return pic == null ? 0 : pic.getWidth();
}

private int getHeight(Picture pic) {
return pic == null ? 0 : pic.getHeight();
}
}

public static MarkerIcon create(Device device) {
if (device.getIconType() == null) {
return new MarkerIcon.Database(device.getIcon());
} else {
return new MarkerIcon.BuiltIn(device.getIconType());
}
}

public static PositionIcon create(Position position) {
MarkerIcon deviceIcon = create(position.getDevice());
String url = position.getStatus() == Position.Status.OFFLINE ? deviceIcon.getOfflineURL() : deviceIcon.getDefaultURL();
int width = position.getStatus() == Position.Status.OFFLINE ? deviceIcon.getOfflineWidth() : deviceIcon.getDefaultWidth();
int height = position.getStatus() == Position.Status.OFFLINE ? deviceIcon.getOfflineHeight() : deviceIcon.getDefaultHeight();
return new PositionIcon(url, width, height,
deviceIcon.getSelectedURL(), deviceIcon.getSelectedWidth(), deviceIcon.getSelectedHeight());
}
}
22 changes: 8 additions & 14 deletions src/main/java/org/traccar/web/client/view/MarkerIconFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,20 @@
import org.gwtopenmaps.openlayers.client.Icon;
import org.gwtopenmaps.openlayers.client.Pixel;
import org.gwtopenmaps.openlayers.client.Size;
import org.traccar.web.shared.model.PositionIcon;
import org.traccar.web.shared.model.PositionIconType;

import java.util.HashMap;
import java.util.Map;

public class MarkerIconFactory {
private static final Map<PositionIconType, Size> sizes = new HashMap<PositionIconType, Size>();
private static final Map<PositionIconType, Pixel> offsets = new HashMap<PositionIconType, Pixel>();

public static Icon getIcon(PositionIconType type, boolean selected) {
Size size = sizes.get(type);
if (size == null) {
size = new Size(type.getWidth(), type.getHeight());
sizes.put(type, size);
}
Pixel offset = offsets.get(type);
if (offset == null) {
offset = new Pixel(-type.getWidth() / 2f, -type.getHeight());
offsets.put(type, offset);
public static Icon getIcon(PositionIcon icon, boolean selected) {
if (icon == null) {
return null;
}
return type == null ? null : new Icon(type.getURL(selected), size, offset);
Size size = new Size(selected ? icon.getSelectedWidth() : icon.getWidth(),
selected ? icon.getSelectedHeight() : icon.getHeight());
Pixel offset = new Pixel(-size.getWidth() / 2f, -size.getHeight());
return new Icon(selected ? icon.getSelectedURL() : icon.getURL(), size, offset);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import org.traccar.web.client.i18n.Messages;
import org.traccar.web.shared.model.GeoFence;
import org.traccar.web.shared.model.Position;
import org.traccar.web.shared.model.PositionIconType;
import org.traccar.web.shared.model.PositionIcon;

public class PositionInfoPopup {
private final static Messages i18n = GWT.create(Messages.class);
Expand Down Expand Up @@ -82,12 +82,12 @@ public void show(int x, int y, final Position position) {

ToolTipConfig config = new ToolTipConfig();

PositionIconType iconType = position.getIconType() == null ? position.getDevice().getIconType().getPositionIconType(position.getStatus()) : position.getIconType();
PositionIcon icon = position.getIcon() == null ? MarkerIcon.create(position) : position.getIcon();
String deviceTitle = position.getDevice().getName() + (position.getStatus() == Position.Status.OFFLINE ? " (" + i18n.offline() + ")" : "");

config.setTitleHtml(
"<table height=\"100%\"><tr>" +
"<td>" +"<img src=\"" + iconType.getURL(false) + "\">&nbsp;</td>" +
"<td>" +"<img src=\"" + icon.getURL() + "\">&nbsp;</td>" +
"<td valign=\"center\">" + deviceTitle + "</td>" +
"</tr></table>");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ public Device updateDevice(Device device) throws TraccarException {
tmp_device.setTimeout(device.getTimeout());
tmp_device.setIdleSpeedThreshold(device.getIdleSpeedThreshold());
tmp_device.setIconType(device.getIconType());
tmp_device.setIcon(device.getIcon() == null ? null : entityManager.find(DeviceIcon.class, device.getIcon().getId()));
return tmp_device;
} else {
throw new DeviceExistsException();
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/traccar/web/shared/model/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public Device(Device device) {
timeout = device.timeout;
idleSpeedThreshold = device.idleSpeedThreshold;
iconType = device.iconType;
icon = device.getIcon();
}

@Expose
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/traccar/web/shared/model/Position.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,14 @@ public void setStatus(Status status) {
}

@GwtTransient
private transient PositionIconType iconType;
private transient PositionIcon icon;

public PositionIconType getIconType() {
return iconType;
public PositionIcon getIcon() {
return icon;
}

public void setIconType(PositionIconType iconType) {
this.iconType = iconType;
public void setIcon(PositionIcon icon) {
this.icon = icon;
}

@GwtTransient
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/org/traccar/web/shared/model/PositionIcon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2015 Vitaly Litvak ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.traccar.web.shared.model;

public class PositionIcon {
final String URL;
final int width;
final int height;

final String selectedURL;
final int selectedWidth;
final int selectedHeight;

public PositionIcon(String url,
int width,
int height,
String selectedURL,
int selectedWidth,
int selectedHeight) {
URL = url;
this.width = width;
this.height = height;
this.selectedURL = selectedURL;
this.selectedWidth = selectedWidth;
this.selectedHeight = selectedHeight;
}

public PositionIcon(PositionIconType iconType) {
this(iconType.getURL(true), iconType.getWidth(), iconType.getHeight(),
iconType.getURL(false), iconType.getWidth(), iconType.getHeight());
}

public String getURL() {
return URL;
}

public String getSelectedURL() {
return selectedURL;
}

public int getWidth() {
return width;
}

public int getHeight() {
return height;
}

public int getSelectedWidth() {
return selectedWidth;
}

public int getSelectedHeight() {
return selectedHeight;
}
}

0 comments on commit 57eaabc

Please sign in to comment.