Issue checking for geolocation

This commit is contained in:
z0ccc 2022-05-22 16:37:15 -04:00
parent 5691f9ed97
commit db2d39637d

View file

@ -1,30 +1,56 @@
const getGeolocation = (setGeolocationData) => { const getGeolocation = (setGeolocationData) => {
new Promise((showPosition, showError) => { new Promise((showPosition, showError) => {
navigator.geolocation.getCurrentPosition(showPosition, showError, { enableHighAccuracy: true }); navigator.geolocation.getCurrentPosition(showPosition, showError, {
}).then(async (position) => { enableHighAccuracy: true,
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),
]);
}) })
.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)); .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`) const checkIssues = () => {
.then((response) => response.json()) console.log(navigator.geolocation.getCurrentPosition.toString());
.then((data) => data.results[0].formatted_address); 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, key,
value, value,
issues: [], issues: [issues].filter(Boolean),
}); });
export default getGeolocation; export default getGeolocation;