@mikezimm/fps-core-v7
Version:
Library of reusable core interfaces, types and constants migrated from fps-library-v2
61 lines • 2.63 kB
JavaScript
/**
* 2024-10-31 GeoLocation code originally migrated from PhotoForm Webpart sample
* Happy Halloween 2024!
* 2024-11-04 updated
*/
export const UnknownGeoLocation = {
timestamp: new Date().getTime(),
localTime: new Date().toLocaleString(),
coords: {},
status: 'Unknown',
message: '',
error: undefined,
};
export function getGeoLocation(debugMode) {
return new Promise((resolve, reject) => {
if (debugMode)
alert(`debugMode getGeoLocation ~ 19`);
let result = JSON.parse(JSON.stringify(UnknownGeoLocation));
if (debugMode)
alert(`debugMode getGeoLocation ~ 21`);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
if (debugMode)
alert(`debugMode getGeoLocation ~ 25`);
result = JSON.parse(JSON.stringify(position));
if (debugMode)
alert(`debugMode getGeoLocation ~ 27`);
result.status = 'Success';
result.localTime = new Date(result.timestamp).toLocaleString();
// https://github.com/fps-solutions/FPS-Photo-Form/issues/68
if (result.localTime.indexOf('Invalid') > -1) {
result.status = 'Error';
result.error = { code: 2, message: 'Invalid Time' };
}
result.message = `${result.localTime}`;
if (debugMode)
alert(`debugMode getGeoLocation ~ 31: ${JSON.stringify(result)}`);
resolve(result); // Resolve the promise with the result
}, (error) => {
if (debugMode)
alert(`debugMode getGeoLocation ~ 35`);
console.error('Error obtaining location:', error);
if (debugMode)
alert(`debugMode getGeoLocation ~ 37: ${error.message}`);
result.status = 'Error';
result.error = error;
result.message = error.message;
reject(result); // Reject the promise with the error result
}, { enableHighAccuracy: true } // Add this line
);
}
else {
result.status = 'Error';
result.message = 'Geolocation is not supported by this browser.';
if (debugMode)
alert(`debugMode getGeoLocation ~ 48: ${result.message}`);
reject(result); // Reject the promise
}
});
}
//# sourceMappingURL=functions.js.map