remember gps setting on load

This commit is contained in:
Matt Walsh 2022-12-13 15:43:06 -06:00
parent e9e68cc786
commit 5fd79f0b19
7 changed files with 75 additions and 50 deletions

2
dist/index.html vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -36,7 +36,9 @@ const init = () => {
document.getElementById('NavigatePrevious').addEventListener('click', btnNavigatePreviousClick); document.getElementById('NavigatePrevious').addEventListener('click', btnNavigatePreviousClick);
document.getElementById('NavigatePlay').addEventListener('click', btnNavigatePlayClick); document.getElementById('NavigatePlay').addEventListener('click', btnNavigatePlayClick);
document.getElementById('ToggleFullScreen').addEventListener('click', btnFullScreenClick); document.getElementById('ToggleFullScreen').addEventListener('click', btnFullScreenClick);
document.getElementById('btnGetGps').addEventListener('click', btnGetGpsClick); const btnGetGps = document.getElementById('btnGetGps');
btnGetGps.addEventListener('click', btnGetGpsClick);
if (!navigator.geolocation) btnGetGps.style.display = 'none';
document.getElementById('divTwc').addEventListener('click', () => { document.getElementById('divTwc').addEventListener('click', () => {
if (document.fullscreenElement) updateFullScreenNavigate(); if (document.fullscreenElement) updateFullScreenNavigate();
@ -77,12 +79,16 @@ const init = () => {
// Auto load the previous query // Auto load the previous query
const query = localStorage.getItem('latLonQuery'); const query = localStorage.getItem('latLonQuery');
const latLon = localStorage.getItem('latLonLon'); const latLon = localStorage.getItem('latLon');
if (query && latLon) { const fromGPS = localStorage.getItem('latLonFromGPS');
if (query && latLon && !fromGPS) {
const txtAddress = document.getElementById('txtAddress'); const txtAddress = document.getElementById('txtAddress');
txtAddress.value = query; txtAddress.value = query;
loadData(JSON.parse(latLon)); loadData(JSON.parse(latLon));
} }
if (fromGPS) {
btnGetGpsClick();
}
const twcPlay = localStorage.getItem('play'); const twcPlay = localStorage.getItem('play');
if (twcPlay === null || twcPlay === 'true') postMessage('navButton', 'play'); if (twcPlay === null || twcPlay === 'true') postMessage('navButton', 'play');
@ -101,7 +107,9 @@ const init = () => {
postMessage('navButton', 'play'); postMessage('navButton', 'play');
localStorage.removeItem('latLonQuery'); localStorage.removeItem('latLonQuery');
localStorage.removeItem('latLonLon'); localStorage.removeItem('latLon');
localStorage.removeItem('latLonFromGPS');
document.getElementById('btnGetGps').classList.remove('active');
}); });
// swipe functionality // swipe functionality
@ -129,14 +137,14 @@ const autocompleteOnSelect = async (suggestion, elem) => {
} }
}; };
const doRedirectToGeometry = (geom) => { const doRedirectToGeometry = (geom, haveDataCallback) => {
const latLon = { lat: round2(geom.y, 4), lon: round2(geom.x, 4) }; const latLon = { lat: round2(geom.y, 4), lon: round2(geom.x, 4) };
// Save the query // Save the query
localStorage.setItem('latLonQuery', document.getElementById('txtAddress').value); localStorage.setItem('latLonQuery', document.getElementById('txtAddress').value);
localStorage.setItem('latLonLon', JSON.stringify(latLon)); localStorage.setItem('latLon', JSON.stringify(latLon));
// get the data // get the data
loadData(latLon); loadData(latLon, haveDataCallback);
}; };
const btnFullScreenClick = () => { const btnFullScreenClick = () => {
@ -211,7 +219,7 @@ const btnNavigateMenuClick = () => {
return false; return false;
}; };
const loadData = (_latLon) => { const loadData = (_latLon, haveDataCallback) => {
// if latlon is provided store it locally // if latlon is provided store it locally
if (_latLon) loadData.latLon = _latLon; if (_latLon) loadData.latLon = _latLon;
// get the data // get the data
@ -221,7 +229,7 @@ const loadData = (_latLon) => {
document.getElementById('txtAddress').blur(); document.getElementById('txtAddress').blur();
stopAutoRefreshTimer(); stopAutoRefreshTimer();
latLonReceived(latLon); latLonReceived(latLon, haveDataCallback);
}; };
const swipeCallBack = (direction) => { const swipeCallBack = (direction) => {
@ -332,38 +340,39 @@ const postMessage = (type, myMessage = {}) => {
navMessage({ type, message: myMessage }); navMessage({ type, message: myMessage });
}; };
const getPosition = async () => new Promise((resolve) => {
navigator.geolocation.getCurrentPosition(resolve);
});
const btnGetGpsClick = async () => { const btnGetGpsClick = async () => {
if (!navigator.geolocation) return; if (!navigator.geolocation) return;
const btn = document.getElementById('btnGetGps');
const position = await (() => new Promise((resolve) => { // toggle first
navigator.geolocation.getCurrentPosition(resolve); if (btn.classList.contains('active')) {
}))(); btn.classList.remove('active');
localStorage.removeItem('latLonFromGPS');
return;
}
// set gps active
btn.classList.add('active');
// get position
const position = await getPosition();
const { latitude, longitude } = position.coords; const { latitude, longitude } = position.coords;
let data;
try {
data = await json('https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/reverseGeocode', {
data: {
location: `${longitude},${latitude}`,
distance: 1000, // Find location up to 1 KM.
f: 'json',
},
});
} catch (e) {
console.error('Unable to fetch reverse geocode');
console.error(e.status, e.responseJSONe);
}
const ZipCode = data.address.Postal;
const { City } = data.address;
const State = states.getTwoDigitCode(data.address.Region);
const Country = data.address.CountryCode;
const query = `${ZipCode}, ${City}, ${State}, ${Country}`;
const txtAddress = document.getElementById('txtAddress'); const txtAddress = document.getElementById('txtAddress');
txtAddress.value = query; txtAddress.value = `${round2(latitude, 4)}, ${round2(longitude, 4)}`;
txtAddress.blur();
txtAddress.focus();
// Save the query doRedirectToGeometry({ y: latitude, x: longitude }, (point) => {
localStorage.setItem('latLonQuery', query); console.log(point);
const location = point.properties.relativeLocation.properties;
// Save the query
const query = `${location.city}, ${location.state}`;
localStorage.setItem('latLon', JSON.stringify({ lat: latitude, lon: longitude }));
localStorage.setItem('latLonQuery', query);
localStorage.setItem('latLonFromGPS', true);
txtAddress.value = `${location.city}, ${location.state}`;
});
}; };

View file

@ -49,10 +49,12 @@ const message = (data) => {
} }
}; };
const getWeather = async (latLon) => { const getWeather = async (latLon, haveDataCallback) => {
// get initial weather data // get initial weather data
const point = await getPoint(latLon.lat, latLon.lon); const point = await getPoint(latLon.lat, latLon.lon);
if (typeof haveDataCallback === 'function') haveDataCallback(point);
// get stations // get stations
const stations = await json(point.properties.observationStations); const stations = await json(point.properties.observationStations);
@ -331,8 +333,8 @@ const AssignLastUpdate = (date) => {
} }
}; };
const latLonReceived = (data) => { const latLonReceived = (data, haveDataCallback) => {
getWeather(data); getWeather(data, haveDataCallback);
AssignLastUpdate(null); AssignLastUpdate(null);
}; };

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -53,19 +53,33 @@ button {
border: 1px solid darkgray; border: 1px solid darkgray;
} }
#btnGetGps img { #btnGetGps {
img {
&.dark { &.dark {
display: none; display: none;
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
display: inline-block; display: inline-block;
}
}
&.light {
@media (prefers-color-scheme: dark) {
display: none;
}
} }
} }
&.light { &.active {
background-color: black;
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
display: none; background-color: white;
}
img {
filter: invert(1);
} }
} }
} }