I have used the geolocation API to return a user's coordinates. Instead of returning the latitude and longitude I would like to return the address of these coordinates. Is there any way I can do this? I'm using Rails for the backend so thought about using the geocoder gem but it would probably make more sense to do it with JS and keep all of the geocoding logic in one place. I have the following code at the moment.
var currentLocation = document.getElementById('coordinatesStore');
document.querySelector('.add-button').addEventListener('click', () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(({ coords: { latitude, longitude }}) => {
currentLocation.value = latitude + ", " + longitude;
});
} else {
currentLocation.value = "Geolocation is not supported by this browser.";
}
});