Center map on startup location
This commit is contained in:
1 parent
7933b2e86a
commit
0344227a1b
6 files changed
+155
-19
No files matched your search
+72
-8
@@ -19,6 +19,9 @@ const lmeState = {
|
||||
measurementRefreshTimer: null,
|
||||
gpsMarker: null,
|
||||
gpsAccuracy: null,
|
||||
gpsPositionVisible: true,
|
||||
hasLiveGpsFix: false,
|
||||
lastAccuracy: 0,
|
||||
gpsTrail: null,
|
||||
selectedParcelsLayer: null,
|
||||
gpsParcelLayer: null,
|
||||
@@ -40,6 +43,7 @@ const cadastreCrs = L.extend({}, L.CRS.EPSG4326, { code: "EPSG:4258" });
|
||||
window.lmeMap = {
|
||||
init,
|
||||
showSearchResult,
|
||||
showInitialPosition,
|
||||
displayOfficialParcel,
|
||||
exportCadastreView,
|
||||
closeCadastrePreview,
|
||||
@@ -69,6 +73,7 @@ window.lmeMap = {
|
||||
centerOnGps,
|
||||
toggleTrail,
|
||||
setNativeGpsPosition,
|
||||
setGpsPositionVisible,
|
||||
setGpsError,
|
||||
resize,
|
||||
showInfo: setInfo
|
||||
@@ -178,6 +183,23 @@ function showSearchResult(latitude, longitude, label) {
|
||||
setInfo(label || `${lat.toFixed(6)}, ${lon.toFixed(6)}`);
|
||||
}
|
||||
|
||||
function showInitialPosition(location) {
|
||||
const latitude = Number(location?.latitude);
|
||||
const longitude = Number(location?.longitude);
|
||||
const accuracy = Math.max(0, Number(location?.accuracy) || 0);
|
||||
if (!Number.isFinite(latitude) || !Number.isFinite(longitude)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const latlng = [latitude, longitude];
|
||||
lmeState.lastPosition = latlng;
|
||||
lmeState.lastAccuracy = accuracy;
|
||||
lmeState.map.setView(latlng, accuracy > 80 ? 17 : 19);
|
||||
setText("gpsState", "Posizione iniziale acquisita - GPS non attivo");
|
||||
setInfo("Mappa centrata sulla posizione attuale. GPS continuo non attivo.");
|
||||
updateGpsInsideState();
|
||||
}
|
||||
|
||||
function displayOfficialParcel(xmlText, latitude, longitude, parcel, label, shouldFit) {
|
||||
const lat = Number(latitude);
|
||||
const lon = Number(longitude);
|
||||
@@ -1705,6 +1727,25 @@ function setNativeGpsPosition(location) {
|
||||
});
|
||||
}
|
||||
|
||||
function setGpsPositionVisible(enabled) {
|
||||
lmeState.gpsPositionVisible = Boolean(enabled);
|
||||
if (!lmeState.gpsPositionVisible) {
|
||||
if (lmeState.gpsMarker && lmeState.map.hasLayer(lmeState.gpsMarker)) {
|
||||
lmeState.map.removeLayer(lmeState.gpsMarker);
|
||||
}
|
||||
if (lmeState.gpsAccuracy && lmeState.map.hasLayer(lmeState.gpsAccuracy)) {
|
||||
lmeState.map.removeLayer(lmeState.gpsAccuracy);
|
||||
}
|
||||
setInfo("Punto e cerchio GPS nascosti. Il rilevamento continua.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (lmeState.hasLiveGpsFix && lmeState.lastPosition) {
|
||||
updateGpsPositionLayers(lmeState.lastPosition, lmeState.lastAccuracy);
|
||||
}
|
||||
setInfo("Punto e cerchio GPS visibili.");
|
||||
}
|
||||
|
||||
function setGpsError(error) {
|
||||
setText("gpsState", "GPS non disponibile");
|
||||
setInfo(error || "Errore GPS.");
|
||||
@@ -1729,14 +1770,12 @@ function onGpsPosition(position) {
|
||||
const { latitude, longitude, accuracy } = position.coords;
|
||||
const latlng = [latitude, longitude];
|
||||
lmeState.lastPosition = latlng;
|
||||
|
||||
if (!lmeState.gpsMarker) {
|
||||
lmeState.gpsMarker = L.marker(latlng, { title: "La tua posizione" }).addTo(lmeState.map);
|
||||
lmeState.gpsAccuracy = L.circle(latlng, { radius: accuracy, color: "#2563eb", weight: 2, fillColor: "#60a5fa", fillOpacity: 0.16 }).addTo(lmeState.map);
|
||||
lmeState.lastAccuracy = Math.max(0, Number(accuracy) || 0);
|
||||
const firstLiveFix = !lmeState.hasLiveGpsFix;
|
||||
lmeState.hasLiveGpsFix = true;
|
||||
updateGpsPositionLayers(latlng, lmeState.lastAccuracy);
|
||||
if (firstLiveFix) {
|
||||
lmeState.map.setView(latlng, 19);
|
||||
} else {
|
||||
lmeState.gpsMarker.setLatLng(latlng);
|
||||
lmeState.gpsAccuracy.setLatLng(latlng).setRadius(accuracy);
|
||||
}
|
||||
|
||||
if (lmeState.gpsTrailEnabled) {
|
||||
@@ -1744,10 +1783,35 @@ function onGpsPosition(position) {
|
||||
lmeState.gpsTrail.setLatLngs(lmeState.gpsTrailPoints);
|
||||
}
|
||||
|
||||
setText("gpsState", `${latitude.toFixed(6)}, ${longitude.toFixed(6)} - precisione ${Math.round(accuracy)} m`);
|
||||
setText("gpsState", `${latitude.toFixed(6)}, ${longitude.toFixed(6)} - precisione ${Math.round(lmeState.lastAccuracy)} m`);
|
||||
updateGpsInsideState();
|
||||
}
|
||||
|
||||
function updateGpsPositionLayers(latlng, accuracy) {
|
||||
if (!lmeState.gpsPositionVisible) {
|
||||
return;
|
||||
}
|
||||
if (!lmeState.gpsMarker) {
|
||||
lmeState.gpsMarker = L.marker(latlng, { title: "La tua posizione" });
|
||||
lmeState.gpsAccuracy = L.circle(latlng, {
|
||||
radius: accuracy,
|
||||
color: "#2563eb",
|
||||
weight: 2,
|
||||
fillColor: "#60a5fa",
|
||||
fillOpacity: 0.16
|
||||
});
|
||||
} else {
|
||||
lmeState.gpsMarker.setLatLng(latlng);
|
||||
lmeState.gpsAccuracy.setLatLng(latlng).setRadius(accuracy);
|
||||
}
|
||||
if (!lmeState.map.hasLayer(lmeState.gpsMarker)) {
|
||||
lmeState.gpsMarker.addTo(lmeState.map);
|
||||
}
|
||||
if (!lmeState.map.hasLayer(lmeState.gpsAccuracy)) {
|
||||
lmeState.gpsAccuracy.addTo(lmeState.map);
|
||||
}
|
||||
}
|
||||
|
||||
function onGpsError(error) {
|
||||
setText("gpsState", "GPS non autorizzato o non disponibile");
|
||||
setInfo(error.message || "Errore GPS.");
|
||||
|
||||
Reference in new issue
Block a user