From db2d39637dd24a13142093a527bf7485bf6e49aa Mon Sep 17 00:00:00 2001 From: z0ccc Date: Sun, 22 May 2022 16:37:15 -0400 Subject: [PATCH] Issue checking for geolocation --- src/utils/geolocation.js | 62 ++++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/utils/geolocation.js b/src/utils/geolocation.js index 9870335..94e84af 100644 --- a/src/utils/geolocation.js +++ b/src/utils/geolocation.js @@ -1,30 +1,56 @@ const getGeolocation = (setGeolocationData) => { new Promise((showPosition, showError) => { - navigator.geolocation.getCurrentPosition(showPosition, showError, { enableHighAccuracy: true }); - }).then(async (position) => { - const geocode = await getGeocode(position.coords.latitude, position.coords.longitude); - setGeolocationData([ - getObj('Latitude', position.coords.latitude), - getObj('Longitude', position.coords.longitude), - getObj('Accuracy', position.coords.accuracy), - getObj('Altitude', position.coords.altitude), - getObj('Altitude accuracy', position.coords.altitudeAccuracy), - getObj('Heading', position.coords.heading), - getObj('Speed', position.coords.speed), - getObj('Reverse geocoding', geocode), - ]); + navigator.geolocation.getCurrentPosition(showPosition, showError, { + enableHighAccuracy: true, + }); }) + .then(async (position) => { + const geocode = await getGeocode( + position.coords.latitude, + position.coords.longitude + ); + + const issues = checkIssues(); + console.log(issues); + console.log(navigator.geolocation.getCurrentPosition); + + setGeolocationData([ + getObj('Latitude', position.coords.latitude, issues), + getObj('Longitude', position.coords.longitude, issues), + getObj('Accuracy', position.coords.accuracy, issues), + getObj('Altitude', position.coords.altitude, issues), + getObj('Altitude accuracy', position.coords.altitudeAccuracy, issues), + getObj('Heading', position.coords.heading, issues), + getObj('Speed', position.coords.speed, issues), + getObj('Reverse geocoding', geocode, issues), + ]); + }) .catch((e) => setGeolocationData(e.message)); }; -const getGeocode = (lat, long) => fetch(`https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&key=AIzaSyByyRWGncal9iAq1-3Ek2WQ3ROLw9bCS-8`) - .then((response) => response.json()) - .then((data) => data.results[0].formatted_address); +const checkIssues = () => { + console.log(navigator.geolocation.getCurrentPosition.toString()); + if ( + !navigator.geolocation.getCurrentPosition + .toString() + .includes('[native code]') + ) { + return "Failed navigator.geolocation.getCurrentPosition.toString().includes('[native code]')"; + } + return null; +}; -const getObj = (key, value) => ({ +const getGeocode = (lat, long) => + fetch( + `https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${long}&key=AIzaSyByyRWGncal9iAq1-3Ek2WQ3ROLw9bCS-8` + ) + .then((response) => response.json()) + .then((data) => data.results[0].formatted_address); + +const getObj = (key, value, issues) => ({ key, value, - issues: [], + issues: [issues].filter(Boolean), }); export default getGeolocation;