Repository navigation
GeoMapIndicator
Up | Previous | Next | Русский
Below is the full source code of the class in documentation form. Comments inside the block explain the purpose of class parts, but they are not stored in the working file GeoMapIndicator.java: production code stays clean, and explanations live in the wiki.
package com.velitask.plugin.official;
import com.velitask.plugin.official.geo.GeoPlaceBoundaryFigure;
import com.velitask.plugin.official.geo.GeoPlaceLabelsFigure;
import com.velitask.plugin.official.geo.GeoPositionFigure;
import com.velitask.plugin.official.geo.GeoTrackFigure;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.IndicatorSkin;
import com.velitask.sdk.data.GeoSensorAtom;
import com.velitask.sdk.data.LandBounds;
import com.velitask.sdk.db.DataCacheRule;
import com.velitask.sdk.db.DataParams;
import com.velitask.sdk.figures.IFigure;
import com.velitask.sdk.osm.OSMTile;
import com.velitask.sdk.properties.ArrayProperty;
import com.velitask.sdk.properties.BorderProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DisplayHint;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.FitTrackProperty;
import com.velitask.sdk.properties.GeoSensorProperty;
import com.velitask.sdk.properties.GeoZoomProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerProperty;
import com.velitask.sdk.properties.MapTilesProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.TransparencyProperty;
import com.velitask.sdk.properties.VisibleProperty;
import com.velitask.units.angle.TrackBearing;
import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.util.List;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;
import org.abricos.geo.GeoUtils;
import org.abricos.geo.MapZoom;
import org.abricos.geo.tile.TilePixel;
import org.json.JSONObject;
/*
* OSM tile map with overlaid figures (place boundaries, track, position, labels).
* Centered on the current position (from the geo sensor) or on the whole track
* bounds (fitTrack). Supports background transparency, hiding the map, a rounded
* border, a 3D view with tilt, rotation by heading / to north / manually, and
* shifting the "vehicle" into the bottom third of the frame.
*/
public class GeoMapIndicator extends Indicator {
public static final String NAME = "geoMap";
public static final String KEY = _KEY + "." + NAME;
/* Map rotation mode: north up / heading up / manual bearing. */
public enum RotationMode {
NORTH_UP, HEADING_UP, MANUAL
}
/* Where the "vehicle" (position point) is anchored: center or bottom third. */
public enum VehicleAnchor {
CENTER, BOTTOM_THIRD
}
/*
* Geo data source. For heading computation (HEADING_UP) it requests the track
* of points sorted by time, with a cache keyed by parameters.
*/
private final GeoSensorProperty mGeoSensor = new GeoSensorProperty();
{
mGeoSensor.query("track")
.where("zoom = {zoom}")
.orderBy("timeRaw")
.cache(DataCacheRule.byParams())
.cacheSize(2)
.buildList();
}
/* OSM tile source (URL template, tile cache). */
private final MapTilesProperty mTiles = new MapTilesProperty();
/* Map zoom level, used when fitTrack=false. */
private final GeoZoomProperty mGeoZoom = new GeoZoomProperty();
/* OSM tile transparency 0..100; does not affect figures. */
private final TransparencyProperty mMapTransparency = new TransparencyProperty();
/* Whether to show the OSM background. False = empty canvas, figures only. */
private final VisibleProperty mShowMap = new VisibleProperty();
/* Layer border: color, thickness, corner radius. */
private final BorderProperty mBorder = new BorderProperty();
/* Camera tilt angle for the 3D view, 0..80°. Active only when view3D is on. */
private final IntegerProperty mTilt = new IntegerProperty() {
{
setRange(0, 80);
}
@Override
public String getName() {
return "tilt";
}
@Override
public Integer getDefault() {
return 50;
}
@Override
public String getTitle() {
return localized(KEY + ".tilt.title");
}
};
/*
* 3D view (off by default). Controls the availability of tilt: when turned
* off, tilt is disabled. fitTrack ignores 3D.
*/
private final VisibleProperty mView3D = new VisibleProperty() {
{
setDefault(false);
set(false);
}
@Override
public String getName() {
return "view3D";
}
@Override
public String getTitle() {
return localized(KEY + ".view3D.title");
}
@Override
public IProperty[] dependents() {
return new IProperty[]{mTilt};
}
@Override
protected void onChanged(Object oldVal, Object newVal) {
mTilt.setEnabled(Boolean.TRUE.equals(newVal));
}
@Override
public void fromJSON(JSONObject json) {
super.fromJSON(json);
mTilt.setEnabled(get());
}
};
/* Fixed map bearing for MANUAL mode, 0..359°. */
private final IntegerProperty mManualBearing = new IntegerProperty() {
{
setRange(0, 359);
}
@Override
public String getName() {
return "manualBearing";
}
@Override
public Integer getDefault() {
return 0;
}
@Override
public String getTitle() {
return localized(KEY + ".manualBearing.title");
}
};
/*
* Minimum speed (km/h) to recompute heading in HEADING_UP.
* Below the threshold the map holds the last valid bearing — it does not
* "spin" from GPS noise while stopped.
*/
private final IntegerProperty mMinSpeedKmh = new IntegerProperty() {
{
setRange(0, 50);
}
@Override
public String getName() {
return "minSpeedKmh";
}
@Override
public Integer getDefault() {
return 2;
}
@Override
public String getTitle() {
return localized(KEY + ".minSpeedKmh.title");
}
};
/* Vehicle anchor: centered or in the bottom third of the frame. */
private final EnumArrayProperty<VehicleAnchor> mVehicleAnchor
= new EnumArrayProperty<>(VehicleAnchor.class) {
@Override
public String getName() {
return "vehicleAnchor";
}
@Override
public String getTitle() {
return localized(KEY + ".vehicleAnchor.title");
}
@Override
protected String[] defineTitles() {
return new String[]{
localized(KEY + ".vehicleAnchor.center"),
localized(KEY + ".vehicleAnchor.bottomThird")
};
}
};
/*
* Rotation mode. Controls the availability of dependent settings:
* manualBearing is active only in MANUAL, minSpeedKmh — only in HEADING_UP.
*/
private final EnumArrayProperty<RotationMode> mRotation
= new EnumArrayProperty<>(RotationMode.class) {
@Override
public String getName() {
return "rotation";
}
@Override
public String getTitle() {
return localized(KEY + ".rotation.title");
}
@Override
protected String[] defineTitles() {
return new String[]{
localized(KEY + ".rotation.northUp"),
localized(KEY + ".rotation.headingUp"),
localized(KEY + ".rotation.manual")
};
}
@Override
public IProperty[] dependents() {
return new IProperty[]{mManualBearing, mMinSpeedKmh};
}
@Override
protected void onChanged(Object oldVal, Object newVal) {
applyRotationDependents();
}
@Override
public void fromJSON(JSONObject json) {
super.fromJSON(json);
applyRotationDependents();
}
};
/*
* Auto-zoom: when enabled, computes a zoom so the whole track fits into the
* layer; disables the manual geoZoom. In fitTrack, rotation and 3D are not
* applied.
*/
private final FitTrackProperty mFitTrack = new FitTrackProperty() {
@Override
public IProperty[] dependents() {
return new IProperty[]{mGeoZoom};
}
@Override
protected void onChanged(Object oldVal, Object newVal) {
mGeoZoom.setEnabled(!Boolean.TRUE.equals(newVal));
}
@Override
public void fromJSON(JSONObject json) {
super.fromJSON(json);
mGeoZoom.setEnabled(!get());
}
};
/* Last valid heading — held while stopped. */
private double mLastValidBearingDeg = 0;
private void applyRotationDependents() {
RotationMode m = mRotation.get();
mManualBearing.setEnabled(m == RotationMode.MANUAL);
mMinSpeedKmh.setEnabled(m == RotationMode.HEADING_UP);
}
/* Access from figures: they read this sensor. */
public GeoSensorProperty getGeo() {
return mGeoSensor;
}
public FitTrackProperty getFitTrack() {
return mFitTrack;
}
@Override public String getName() { return NAME; }
@Override public String getTitle() { return localized(KEY + ".title"); }
@Override public String getDescription() { return localized(KEY + ".description"); }
@Override public String getTags() { return localized(KEY + ".tags"); }
/* Initial layout: bottom-left, margins 50/50, size 700x400. */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.BOTTOM);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(50d, null, null, 50d);
maket.setLayerSize(700d, 400d);
}
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mGeoSensor, mTiles, mShowMap, mFitTrack, mGeoZoom, mMapTransparency, mBorder,
mView3D, mTilt, mRotation, mManualBearing, mMinSpeedKmh, mVehicleAnchor
};
}
@Override
public void configureDisplay(DisplayConfig config) {
config.set(mShowMap, PropertyGroup.APPEARANCE);
config.set(mFitTrack, PropertyGroup.APPEARANCE);
config.set(mGeoZoom, PropertyGroup.APPEARANCE);
config.set(mMapTransparency, PropertyGroup.APPEARANCE);
config.set(mBorder, PropertyGroup.APPEARANCE);
config.set(mView3D, PropertyGroup.APPEARANCE);
config.set(mTilt, PropertyGroup.APPEARANCE, DisplayHint.SLIDER);
config.set(mRotation, PropertyGroup.APPEARANCE);
config.set(mManualBearing, PropertyGroup.APPEARANCE, DisplayHint.SLIDER);
config.set(mMinSpeedKmh, PropertyGroup.APPEARANCE);
config.set(mVehicleAnchor, PropertyGroup.APPEARANCE);
}
/*
* Supported figures: settlement boundaries, track, position marker, place
* labels. defaultFigures = supportedFigures — all are created by default.
* The order defines drawing layers (boundaries at the bottom, labels on top).
*/
@Override
@SuppressWarnings("unchecked")
public Class<? extends IFigure>[] supportedFigures() {
return (Class<? extends IFigure>[]) new Class<?>[]{
GeoPlaceBoundaryFigure.class,
GeoTrackFigure.class,
GeoPositionFigure.class,
GeoPlaceLabelsFigure.class
};
}
@Override
@SuppressWarnings("unchecked")
public Class<? extends IFigure>[] defaultFigures() {
return (Class<? extends IFigure>[]) new Class<?>[]{
GeoPlaceBoundaryFigure.class,
GeoTrackFigure.class,
GeoPositionFigure.class,
GeoPlaceLabelsFigure.class
};
}
/*
* Two built-in skins:
* faded — map shown, but at 60% transparency (overlay on video);
* hidden — map hidden, only figures remain.
*/
@Override
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("faded", localized(KEY + ".skin.faded"))
.set(mShowMap.skin(true))
.set(mMapTransparency.skin(60))
.build(),
IndicatorSkin.builder("hidden", localized(KEY + ".skin.hidden"))
.set(mShowMap.skin(false))
.build()
};
}
/*
* Render: draws OSM tiles only. The figures (boundaries, track, position,
* labels) are drawn separately by the SDK on top. They read the same context
* (center, zoom, rotation, tilt) through GeoMapContext.
*/
@Override
public void render(IndicatorContext indicatorContext) {
GeoMapContext ctx = (GeoMapContext) indicatorContext;
Graphics2D g = ctx.graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
if (!ctx.showMap.value) {
return;
}
renderMap(ctx);
}
/*
* Tile render. Applies mapTransform (rotation + tilt + anchor shift) and
* computes the tile range in pixel coordinates. When tilted, it widens the
* vertical coverage and culls tiles beyond the "horizon". The rounded border
* is implemented via a RoundRectangle clip.
*/
private void renderMap(GeoMapContext ctx) {
Graphics2D g = ctx.graphics;
int w = ctx.width, h = ctx.height;
MapZoom zoom = ctx.zoom;
if (zoom == null) {
return;
}
Shape oldClip = g.getClip();
int radius = (int) (ctx.border.radius * ctx.scale);
if (radius > 0) {
g.setClip(new RoundRectangle2D.Double(0, 0, w, h, radius * 2.0, radius * 2.0));
}
AffineTransform oldTransform = g.getTransform();
g.transform(ctx.mapTransform);
/* Half-coverage in world pixels, adjusted for extraScale and tilt. */
double invExtra = 1.0 / Math.max(0.0001, ctx.extraScale);
double halfW = w / 2.0 * invExtra;
double halfH;
if (ctx.tiltRad > 0) {
double cosTilt = Math.cos(ctx.tiltRad);
halfH = (h / 2.0 + ctx.anchorOffsetY) / Math.max(0.05, cosTilt) * invExtra;
} else {
halfH = h / 2.0 * invExtra;
}
double centerX = ctx.centerPx;
double centerY = ctx.centerPy;
/* With rotation/tilt take a circular coverage (hypot), otherwise rectangular. */
int xIndexMin, yIndexMin, xIndexMax, yIndexMax;
if (ctx.bearingRad == 0 && ctx.tiltRad == 0) {
xIndexMin = Math.max(
(int) Math.floor((centerX - halfW) / GeoUtils.TILE_SIZE) - 1, 0);
yIndexMin = Math.max(
(int) Math.floor((centerY - halfH) / GeoUtils.TILE_SIZE) - 1, 0);
xIndexMax = (int) Math.floor((centerX + halfW) / GeoUtils.TILE_SIZE) + 1;
yIndexMax = (int) Math.floor((centerY + halfH) / GeoUtils.TILE_SIZE) + 1;
} else {
double radiusPx = Math.hypot(halfW, halfH) + GeoUtils.TILE_SIZE;
xIndexMin = Math.max(
(int) Math.floor((centerX - radiusPx) / GeoUtils.TILE_SIZE) - 1, 0);
yIndexMin = Math.max(
(int) Math.floor((centerY - radiusPx) / GeoUtils.TILE_SIZE) - 1, 0);
xIndexMax = (int) Math.floor((centerX + radiusPx) / GeoUtils.TILE_SIZE) + 1;
yIndexMax = (int) Math.floor((centerY + radiusPx) / GeoUtils.TILE_SIZE) + 1;
}
double horizonCullY = -halfH * 1.5;
Composite oldComp = g.getComposite();
float alpha = Math.max(0, Math.min(1, 1f - ctx.mapTransparency.value / 100f));
if (alpha < 1f) {
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
}
int zoomValue = zoom.getLevel();
for (int tx = xIndexMin; tx <= xIndexMax; tx++) {
for (int ty = yIndexMin; ty <= yIndexMax; ty++) {
/* Cull tiles above the "horizon" in 3D. */
double localY = ty * GeoUtils.TILE_SIZE - centerY;
if (localY + GeoUtils.TILE_SIZE < horizonCullY) {
continue;
}
/* preview — fast getTile; final — getTileBlocking (waits). */
OSMTile tile = ctx.player.isPreview
? mTiles.getTile(zoomValue, tx, ty)
: mTiles.getTileBlocking(zoomValue, tx, ty, 5_000);
if (tile == null || tile.getImage() == null) {
continue;
}
g.drawImage(tile.getImage(),
tx * GeoUtils.TILE_SIZE, ty * GeoUtils.TILE_SIZE, null);
}
}
g.setComposite(oldComp);
g.setTransform(oldTransform);
if (radius > 0) {
g.setClip(oldClip);
}
drawBorder(g, ctx, w, h, radius);
}
/* Layer border on top of the map: straight or rounded. */
private static void drawBorder(Graphics2D g, GeoMapContext ctx, int w, int h, int radius) {
int bw = (int) (ctx.border.thickness * ctx.scale);
if (bw <= 0) {
return;
}
g.setColor(ctx.border.color);
g.setStroke(new BasicStroke(bw));
int bx = bw / 2, by = bw / 2;
int bw2 = Math.max(1, w - bw);
int bh2 = Math.max(1, h - bw);
if (radius > 0) {
g.drawRoundRect(bx, by, bw2, bh2, radius * 2, radius * 2);
} else {
g.drawRect(bx, by, bw2, bh2);
}
}
/* Result of fitTrack zoom selection: level + extra scale. */
private record FitZoom(int intZoom, double extraScale) {}
private static final double FIT_THRESHOLD = Math.sqrt(2.0);
/*
* fitTrack zoom selection: finds the maximum level at which the track bounds
* still fit into the layer (with a FIT_THRESHOLD tolerance), and tunes the
* fractional extraScale so the track fits exactly with a margin.
*/
private static FitZoom computeFitZoom(int baseWidth, int baseHeight, LandBounds b) {
int padding = Math.max(4, Math.min(40,
(int) Math.round(Math.min(baseWidth, baseHeight) * 0.05)));
int innerW = Math.max(1, baseWidth - 2 * padding);
int innerH = Math.max(1, baseHeight - 2 * padding);
int intZoom = GeoUtils.ZOOM_MIN;
double fitW = 0, fitH = 0;
for (int i = GeoUtils.ZOOM_MIN; i <= GeoUtils.ZOOM_MAX; i++) {
TilePixel px1 = GeoUtils.getTileX(b.lonMin(), i);
TilePixel py1 = GeoUtils.getTileY(b.latMin(), i);
TilePixel px2 = GeoUtils.getTileX(b.lonMax(), i);
TilePixel py2 = GeoUtils.getTileY(b.latMax(), i);
double figureWidth = Math.abs(px2.getPixelGlobal() - px1.getPixelGlobal());
double figureHeight = Math.abs(py2.getPixelGlobal() - py1.getPixelGlobal());
if (figureWidth > innerW * FIT_THRESHOLD
|| figureHeight > innerH * FIT_THRESHOLD) {
if (i == GeoUtils.ZOOM_MIN) {
double extra = Math.min(innerW / figureWidth, innerH / figureHeight);
return new FitZoom(GeoUtils.ZOOM_MIN, extra);
}
break;
}
intZoom = i;
fitW = figureWidth;
fitH = figureHeight;
}
if (fitW <= 0 || fitH <= 0) {
return new FitZoom(intZoom, 1.0);
}
double extra = Math.min(innerW / fitW, innerH / fitH);
return new FitZoom(intZoom, extra);
}
/* Track point window size for heading estimation. */
private static final int BEARING_WINDOW = 16;
/*
* Heading computation (HEADING_UP): takes a window of BEARING_WINDOW track
* points starting from the current time and averages the direction via
* TrackBearing.
*/
private double computeHeadingBearing(MapZoom zm, long rawTime) {
int actualZoom = mGeoSensor.approximateZoom(zm.getLevel());
DataParams params = DataParams.of("sensorId", mGeoSensor.getSensorId())
.set("zoom", actualZoom);
List<GeoSensorAtom> points = mGeoSensor.queryList("track", params);
if (points == null || points.size() < 2) {
return Double.NaN;
}
int from = lowerBoundByTimeRaw(points, rawTime);
if (from >= points.size() - 1) {
from = points.size() - 2;
}
int to = Math.min(from + BEARING_WINDOW, points.size());
if (to - from < 2) {
return Double.NaN;
}
double[] lats = new double[to - from];
double[] lons = new double[to - from];
for (int i = from; i < to; i++) {
GeoSensorAtom p = points.get(i);
lats[i - from] = p.lat;
lons[i - from] = p.lon;
}
return TrackBearing.fromPoints(lats, lons);
}
/* Binary search of a track point by time. */
private static int lowerBoundByTimeRaw(List<GeoSensorAtom> points, long t) {
int lo = 0;
int hi = points.size();
while (lo < hi) {
int mid = (lo + hi) >>> 1;
if (points.get(mid).timeRaw < t) {
lo = mid + 1;
} else {
hi = mid;
}
}
return Math.max(0, lo - 1);
}
/* Speed estimation from the atom deltas (for the minSpeedKmh threshold). */
private static double estimateSpeedKmh(GeoSensorAtom atom) {
if (atom.duration <= 0) {
return 0.0;
}
double latRad = Math.toRadians(atom.lat);
double dyM = atom.latDelta * 111_320.0;
double dxM = atom.lonDelta * 111_320.0 * Math.cos(latRad);
double distM = Math.hypot(dxM, dyM);
double seconds = atom.duration / 1000.0;
return distM / seconds * 3.6;
}
/*
* The context computes the center, zoom, heading, tilt, anchor shift and the
* final AffineTransform directly in the constructor. posLat/posLon — the
* current position, read by the boundary figures to highlight the "active"
* city.
*/
public class GeoMapContext extends IndicatorContext {
public final IntegerProperty.IntegerContext mapTransparency;
public final BorderProperty.BorderContext border;
public final VisibleProperty.BooleanContext showMap;
public final FitTrackProperty.BooleanContext fitTrack;
public final VisibleProperty.BooleanContext view3D;
public final IntegerProperty.IntegerContext tilt;
public final IntegerProperty.IntegerContext manualBearing;
public final IntegerProperty.IntegerContext minSpeedKmh;
public final ArrayProperty<RotationMode>.ArrayContext rotation;
public final ArrayProperty<VehicleAnchor>.ArrayContext vehicleAnchor;
public final MapZoom zoom;
public final double centerPx;
public final double centerPy;
public final double bearingDeg;
public final double bearingRad;
public final double tiltRad;
public final double anchorOffsetY;
public final double extraScale;
public final double posLat;
public final double posLon;
public final AffineTransform mapTransform;
public GeoMapContext(Player player, Canvas canvas) {
super(player, canvas);
fitTrack = mFitTrack.createContext();
mapTransparency = mMapTransparency.createContext();
border = mBorder.createContext();
showMap = mShowMap.createContext();
view3D = mView3D.createContext();
tilt = mTilt.createContext();
manualBearing = mManualBearing.createContext();
minSpeedKmh = mMinSpeedKmh.createContext();
rotation = mRotation.createContext();
vehicleAnchor = mVehicleAnchor.createContext();
MapZoom zm = null;
double cPx = 0;
double cPy = 0;
double br = 0;
double extra = 1.0;
if (mGeoSensor.getSensorId() > 0) {
if (fitTrack.value) {
/* fitTrack: center on bounds, auto-select zoom + extraScale. */
LandBounds b = mGeoSensor.queryLandBounds();
if (b != null) {
FitZoom fz = computeFitZoom(width, height, b);
zm = new MapZoom(fz.intZoom());
extra = fz.extraScale();
double centerLat = (b.latMin() + b.latMax()) / 2.0;
double centerLon = (b.lonMin() + b.lonMax()) / 2.0;
cPx = zm.pixelX(centerLon);
cPy = zm.pixelY(centerLat);
}
} else {
/* Manual mode: center on the current position, zoom from the user. */
long rawTime = mGeoSensor.convertToRawTime(player.time);
if (player.isPreview) {
rawTime = mGeoSensor.clampToSensorRange(rawTime);
}
GeoSensorAtom atom = mGeoSensor.queryAtom(rawTime);
if (atom != null) {
zm = new MapZoom(mGeoZoom.createContext().value);
double lat = atom.calcLat(rawTime);
double lon = atom.calcLon(rawTime);
cPx = zm.pixelX(lon);
cPy = zm.pixelY(lat);
/* HEADING_UP: recompute heading only above the speed threshold. */
if (rotation.value == RotationMode.HEADING_UP) {
double speedKmh = estimateSpeedKmh(atom);
if (speedKmh >= minSpeedKmh.value) {
double deg = computeHeadingBearing(zm, rawTime);
if (!Double.isNaN(deg)) {
mLastValidBearingDeg = deg;
}
}
br = mLastValidBearingDeg;
}
}
}
}
/* Current position for the figures (highlighting the active city). */
double pLat = Double.NaN;
double pLon = Double.NaN;
if (mGeoSensor.getSensorId() > 0) {
long posRawTime = mGeoSensor.convertToRawTime(player.time);
if (player.isPreview) {
posRawTime = mGeoSensor.clampToSensorRange(posRawTime);
}
GeoSensorAtom posAtom = mGeoSensor.queryAtom(posRawTime);
if (posAtom != null) {
pLat = posAtom.calcLat(posRawTime);
pLon = posAtom.calcLon(posRawTime);
}
}
posLat = pLat;
posLon = pLon;
zoom = zm;
centerPx = cPx;
centerPy = cPy;
bearingDeg = br;
/* In fitTrack, rotation and 3D are disabled. */
boolean fit = fitTrack.value;
boolean is3D = !fit && view3D.value;
double bRad;
if (fit) {
bRad = 0;
} else {
bRad = switch (rotation.value) {
case NORTH_UP -> 0.0;
case MANUAL -> Math.toRadians(manualBearing.value);
case HEADING_UP -> Math.toRadians(br);
};
}
bearingRad = bRad;
tiltRad = is3D ? Math.toRadians(tilt.value) : 0.0;
/* BOTTOM_THIRD shifts the center down by 1/6 of the height. */
anchorOffsetY = (!fit && vehicleAnchor.value == VehicleAnchor.BOTTOM_THIRD)
? height / 6.0 : 0.0;
extraScale = extra;
/* Final matrix: translate to anchor → scale → tilt → rotate → translate to center. */
AffineTransform t = new AffineTransform();
t.translate(width / 2.0, height / 2.0 + anchorOffsetY);
if (extraScale != 1.0) {
t.scale(extraScale, extraScale);
}
if (tiltRad > 0) {
t.scale(1.0, Math.cos(tiltRad));
}
if (bearingRad != 0) {
t.rotate(-bearingRad);
}
t.translate(-centerPx, -centerPy);
mapTransform = t;
}
}
@Override
public GeoMapContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
return new GeoMapContext(player, canvas);
}
}GeoMapIndicator is an OSM map layer with a set of figures: settlement boundaries (GeoPlaceBoundaryFigure), GPS track (GeoTrackFigure), position marker (GeoPositionFigure) and place labels (GeoPlaceLabelsFigure). The map is centered on the current position from GeoSensorProperty or on the bounds of the whole track (fitTrack=true — auto-zoom). It supports tile transparency, hiding the OSM background, a rounded border, a 3D view with tilt, map rotation (to north / by heading / manually) and shifting the "vehicle" into the bottom third of the frame.
Data. GeoSensorProperty provides the current position (GeoSensorAtom with lat/lon, interpolated via calcLat/calcLon) and the bounds of the whole track via queryLandBounds(). For heading computation in "heading up" mode the indicator additionally keeps a track query (a window of points sorted by timeRaw, cached by parameters). OSM tiles come from MapTilesProperty. The indicator itself draws only the tiles; the figures are drawn by the SDK on top and read the same context.
Settings. Thirteen properties. Base ones: geo source, tile source, VisibleProperty showMap, FitTrackProperty (auto-zoom; disables the manual geoZoom), GeoZoomProperty, TransparencyProperty, BorderProperty (border color/thickness/rounding). New ones: view3D (3D view) and the dependent tilt (tilt 0..80°); rotation (EnumArrayProperty of RotationMode: NORTH_UP, HEADING_UP, MANUAL) with dependents manualBearing (0..359°, active in MANUAL) and minSpeedKmh (0..50 km/h, active in HEADING_UP); vehicleAnchor (RotationMode anchor: CENTER or BOTTOM_THIRD). The dependencies are wired through dependents()/onChanged()/fromJSON() so the UI disables irrelevant fields.
Drawing. The context in the constructor computes center+zoom (fitTrack → computeFitZoom picks the level and the fractional extraScale; otherwise center on the current atom). For HEADING_UP the heading is averaged over a window of track points (computeHeadingBearing), but recomputed only when speed ≥ minSpeedKmh; below the threshold mLastValidBearingDeg is held. From the heading, tilt (tilt) and anchor a single AffineTransform (mapTransform) is built. In render() this transform is applied, the tile range is computed (circular coverage with rotation/tilt), tiles beyond the "horizon" are culled, and transparency is applied via AlphaComposite. The rounded border is implemented with a RoundRectangle2D clip, then drawBorder is called.
-
The map draws tiles only. Place boundaries, track, marker and labels are separate
IFigureclasses; they readGeoMapContext(center, zoom,mapTransform,posLat/posLon,tiltRad,anchorOffsetY). IfshowMap=falseand the figures are removed, the layer is empty. -
3D, rotation and anchor are not applied in
fitTrack. When the track fits entirely, the map is always "north up", without tilt and without the downward shift. -
tiltis available only whenview3D=true(onChanged/fromJSONinmView3Denables/disables it). -
manualBearingis active only inMANUAL,minSpeedKmh— only inHEADING_UP(applyRotationDependents). The UI listens todependents()and re-renders the panel. -
The heading is held while stopped. Below
minSpeedKmhthe map does not "spin" from GPS noise — the last valid bearing is used. -
extraScaleis a fractional multiplier of fitTrack on top of the integer zoom: the track fits more precisely than the discrete level allows. -
Preview vs final tile rendering differs: preview draws a placeholder while the cache is filled asynchronously, the final render waits for the real tiles (
getTileBlocking, timeout 5 s). -
"Horizon" culling (
horizonCullY) in 3D does not draw tiles above the horizon line, otherwise at a large tilt the map would stretch to infinity.