Skip to content

Colota - Android GPS Location tracker

feature(map): add accuracy visualization

Max Dietrich90686fc5d5d8
diff --git a/src/components/features/dashboard/DashboardMap.tsx b/src/components/features/dashboard/DashboardMap.tsx
index ddd2f3ba..563134f4 100644
--- a/src/components/features/dashboard/DashboardMap.tsx
+++ b/src/components/features/dashboard/DashboardMap.tsx
@@ -246,13 +246,15 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
<div id="map"></div>
<script src="openlayers/ol.js"></script>
<script>
- const vectorSource = new ol.source.Vector();
+ const geofenceSource = new ol.source.Vector();
+ const accuracySource = new ol.source.Vector();
const map = new ol.Map({
target: "map",
layers: [
new ol.layer.Tile({ source: new ol.source.OSM() }),
- new ol.layer.Vector({ source: vectorSource }),
+ new ol.layer.Vector({ source: geofenceSource }),
+ new ol.layer.Vector({ source: accuracySource }),
],
view: new ol.View({
center: ol.proj.fromLonLat([${lon}, ${lat}]),
@@ -260,6 +262,14 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
}),
});
+ let accuracyFeature = null;
+
+ const accuracyStyle = new ol.style.Style({
+ fill: new ol.style.Fill({
+ color: "${colors.primary}22"
+ })
+ });
+
const markerEl = document.createElement("div");
markerEl.innerHTML = \`
<div class="marker-pulse" style="display:none"></div>
@@ -286,7 +296,7 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
function drawGeofences(geofences) {
- vectorSource.clear();
+ geofenceSource.clear();
geofences.forEach(zone => {
const center = ol.proj.fromLonLat([zone.lon, zone.lat]);
const circle = new ol.geom.Circle(center, zone.radius);
@@ -326,8 +336,8 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
})
);
- vectorSource.addFeature(feature);
- vectorSource.addFeature(label);
+ geofenceSource.addFeature(feature);
+ geofenceSource.addFeature(label);
});
}
@@ -364,7 +374,15 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
const newPos = ol.proj.fromLonLat([data.coords.longitude, data.coords.latitude]);
animateMarker(newPos, 500);
- map.getView().animate({ center: newPos, duration: 500, easing: ol.easing.linear });
+
+ // Only auto-center if user has not manually moved the map
+ if (isMapCentered()) {
+ map.getView().animate({
+ center: newPos,
+ duration: 500,
+ easing: ol.easing.linear
+ });
+ }
const markerIcon = markerEl.querySelector(".marker");
const markerPulse = markerEl.querySelector(".marker-pulse");
@@ -379,6 +397,17 @@ export function DashboardMap({ coords, tracking, activeZoneName }: Props) {
markerPulse.style.display = isActive ? "block" : "none";
}
+
+ if (data.coords.accuracy && data.coords.accuracy > 0) {
+ if (!accuracyFeature) {
+ accuracyFeature = new ol.Feature();
+ accuracyFeature.setStyle(accuracyStyle);
+ accuracySource.addFeature(accuracyFeature);
+ }
+
+ const circle = new ol.geom.Circle(newPos, data.coords.accuracy);
+ accuracyFeature.setGeometry(circle);
+ }
}
if (data.action === "center_map") {
diff --git a/src/screens/GeofenceScreen.tsx b/src/screens/GeofenceScreen.tsx
index bb8b6b43..51034ce3 100644
--- a/src/screens/GeofenceScreen.tsx
+++ b/src/screens/GeofenceScreen.tsx
@@ -376,13 +376,15 @@ export function GeofenceScreen({}: ScreenProps) {
<div id="map"></div>
<script src="openlayers/ol.js"></script>
<script>
- const vectorSource = new ol.source.Vector();
+ const geofenceSource = new ol.source.Vector();
+ const accuracySource = new ol.source.Vector();
const map = new ol.Map({
target: "map",
layers: [
new ol.layer.Tile({ source: new ol.source.OSM() }),
- new ol.layer.Vector({ source: vectorSource }),
+ new ol.layer.Vector({ source: geofenceSource }),
+ new ol.layer.Vector({ source: accuracySource }),
],
view: new ol.View({
center: ol.proj.fromLonLat([${lon}, ${lat}]),
@@ -390,6 +392,14 @@ export function GeofenceScreen({}: ScreenProps) {
}),
});
+ let accuracyFeature = null;
+
+ const accuracyStyle = new ol.style.Style({
+ fill: new ol.style.Fill({
+ color: "${colors.primary}22"
+ })
+ });
+
const markerEl = document.createElement("div");
markerEl.innerHTML = \`
<div class="marker-pulse" style="display:none"></div>
@@ -414,7 +424,7 @@ export function GeofenceScreen({}: ScreenProps) {
}
function drawGeofences(geofences) {
- vectorSource.clear();
+ geofenceSource.clear();
geofences.forEach(zone => {
const center = ol.proj.fromLonLat([zone.lon, zone.lat]);
const circle = new ol.geom.Circle(center, zone.radius);
@@ -452,8 +462,8 @@ export function GeofenceScreen({}: ScreenProps) {
})
);
- vectorSource.addFeature(feature);
- vectorSource.addFeature(label);
+ geofenceSource.addFeature(feature);
+ geofenceSource.addFeature(label);
});
}
@@ -495,7 +505,14 @@ export function GeofenceScreen({}: ScreenProps) {
if (data.tracking) {
markerOverlay.setPosition(newPos);
animateMarker(newPos, 500);
- map.getView().animate({ center: newPos, duration: 500 });
+ // Only auto-center if user has not manually moved the map
+ if (isMapCentered()) {
+ map.getView().animate({
+ center: newPos,
+ duration: 500,
+ easing: ol.easing.linear
+ });
+ }
}
const markerIcon = markerEl.querySelector(".marker");
@@ -510,6 +527,16 @@ export function GeofenceScreen({}: ScreenProps) {
markerPulse.style.borderColor = markerColor;
markerPulse.style.display = isActive ? "block" : "none";
}
+
+ if (data.coords.accuracy && data.coords.accuracy > 0) {
+ if (!accuracyFeature) {
+ accuracyFeature = new ol.Feature();
+ accuracyFeature.setStyle(accuracyStyle);
+ accuracySource.addFeature(accuracyFeature);
+ }
+ const circle = new ol.geom.Circle(newPos, data.coords.accuracy);
+ accuracyFeature.setGeometry(circle);
+ }
}
if (data.action === "center_map") {