I have some JavaScript running in Chrome using the Geolocation API:
navigator.geolocation.watchPosition((pos) => {
console.log(pos.coords);
console.log(Object.keys(pos.coords);
}, (err) => {
alert('Geolocation error.\n' + err.message);
}, {
enableHighAccuracy: true
});
When Chrome detects a change in location, it fires off the first callback. This is working well.
The problem comes when I try to do something with that data. When the callback is fired, on my console I will first be able to see the Coordinates object where I called console.log(pos.coords)
:
Coordinates {latitude: 5.520007, longitude: 13.404954, altitude: null, accuracy: 150, altitudeAccuracy: null, …}
However, the next line where I call console.log(Object.keys(pos.coords))
, I get an empty array:
[]
Likewise, if I try to use JSON.stringify(pos.coords)
, I just get an empty object {}
.
Is there any reasonable way to loop through the keys of pos.coords
? Must I make my own list of keys to loop through?