Repository navigation
GeoPositionFigure
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not kept in the working file GeoPositionFigure.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official.geo;
import com.velitask.plugin.official.GeoMapIndicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.data.GeoSensorAtom;
import com.velitask.sdk.figures.Figure;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DisplayHint;
import com.velitask.sdk.properties.GeoSensorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SizeProperty;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
/* The "Position marker" figure: a circle at the current position. */
public class GeoPositionFigure extends Figure<GeoFigureContext> {
/* Figure name (the type field in the figure JSON). */
public static final String NAME = "position";
/* Prefix of the localization keys. */
private static final String KEY = GeoMapIndicator.KEY + "." + NAME;
/* Geo sensor of the figure; the current point is taken from it. */
private final GeoSensorProperty mGeoSensor = new GeoSensorProperty();
/* Name of the fill property. */
private static final String FILL_COLOR = "fillColor";
/* Marker fill, blue by default (0, 0, 200). */
private final ColorProperty mFillColor = new ColorProperty(
new Color(0, 0, 200),
FILL_COLOR,
localized(KEY + "." + FILL_COLOR + ".title")
);
/* Name of the outline property. */
private static final String BORDER_COLOR = "borderColor";
/* Marker outline, white by default. */
private final ColorProperty mBorderColor = new ColorProperty(
Color.WHITE,
BORDER_COLOR,
localized(KEY + "." + BORDER_COLOR + ".title")
);
/* Marker diameter in pixels: 4..100, 30 by default. */
private final SizeProperty mSize = new SizeProperty() {
{
setRange(4, 100);
}
@Override
public Integer getDefault() {
return 30;
}
};
@Override
public String getName() {
return NAME;
}
@Override
public String getTitle() {
return localized(KEY + ".title");
}
@Override
/* Geo, fillColor, borderColor, size are saved. */
public IProperty[] defineProperties() {
return new IProperty[]{mGeoSensor, mFillColor, mBorderColor, mSize};
}
@Override
/* The size is shown as a counter with arrows. */
public void configureDisplay(DisplayConfig config) {
config.set(mFillColor, PropertyGroup.APPEARANCE);
config.set(mBorderColor, PropertyGroup.APPEARANCE);
config.set(mSize, PropertyGroup.APPEARANCE, DisplayHint.SPINNER);
}
@Override
/* Takes the current point, converts it to a screen point and draws a circle with an outline. */
public void render(GeoFigureContext baseCtx) {
PositionContext ctx = (PositionContext) baseCtx;
if (ctx.zoom == null || ctx.position.sensorId == 0) {
return;
}
/* The atom for the frame time; in a recording pause, the last atom before the pause. */
GeoSensorAtom atom = ctx.position.queryAtomOrBefore();
if (atom == null) {
return;
}
/* The coordinates are interpolated to the frame time. */
double lat = atom.calcLat(ctx.position.rawTime);
double lon = atom.calcLon(ctx.position.rawTime);
Point2D.Double pt = ctx.toScreen(lat, lon);
int cx = (int) pt.x;
int cy = (int) pt.y;
/* The diameter is multiplied by the layer scale; the outline has width 2 taking the scale into account. */
int size = Math.max(1, (int) Math.round(ctx.size * ctx.scale));
int x = cx - size / 2;
int y = cy - size / 2;
Graphics2D g = ctx.graphics;
g.setColor(ctx.fill);
g.fillOval(x, y, size, size);
g.setColor(ctx.border);
g.setStroke(new BasicStroke((float) Math.max(1, 2 * ctx.scale)));
g.drawOval(x, y, size, size);
}
@Override
/* Creating a context from the indicator context. */
public GeoFigureContext createContext(IndicatorContext indCtx) {
PositionContext ctx = new PositionContext((GeoMapIndicator.GeoMapContext) indCtx);
return ctx;
}
/* Figure context: the property values on the frame. */
public class PositionContext extends GeoFigureContext {
public final GeoSensorProperty.GeoSensorContext position;
public final Color fill;
public final Color border;
public final int size;
public PositionContext(GeoMapIndicator.GeoMapContext indCtx) {
super(indCtx);
position = mGeoSensor.createContext(indCtx);
fill = mFillColor.get(indCtx);
border = mBorderColor.get(indCtx);
size = mSize.get(indCtx);
}
}
}GeoPositionFigure draws on the map a round marker of the current position: a filled circle with an outline. Together with GeoTrackFigure it is part of the default set of figures of GeoMapIndicator. If the map center follows the vehicle, the marker stands in the center of the frame (or in the bottom third if that position is selected).
Data. The GeoSensorAtom of the current moment is read from GeoSensorProperty through ctx.position.queryAtomOrBefore(): the atom that covers the frame time, and in a recording pause (a stop, a device auto-pause) the last atom before the pause, so the marker does not disappear and stands at the last point before the pause. If the indicator zoom is null, sensorId == 0 or there is no atom (outside the recording), the figure draws nothing. The latitude and the longitude are calculated as atom.calcLat(rawTime) and atom.calcLon(rawTime): an interpolation inside the atom window to the frame time.
Settings. Four properties in the figure JSON: Geo, fillColor (blue (0, 0, 200)), borderColor (white) and size (SizeProperty, range 4..100, default 30). The colors are read as mFillColor.get(indCtx), the size as mSize.get(indCtx).
Drawing. toScreen(lat, lon) with the interpolated coordinates (GeoFigureContext) gives the center of the marker. The diameter equals size × scale (not less than 1 pixel). First fillOval fills the circle, then drawOval draws an outline with the thickness max(1, 2 × scale).
- The marker is always a circle; the rotation of the map does not act on it, and it does not show the direction of movement.
- The coordinates of the marker are interpolated by
rawTimein the same way as the map center in GeoMapIndicator (calcLat/calcLon), so the marker does not lag behind the center between track points. - The marker is not clipped by the rounding of the map border: it is drawn after the indicator, over the border.
- The
Geoproperty is not declared inconfigureDisplay(), so it is not shown as a separate row in the figure settings list.