I'am working on app that ask geolocation to render data around a place. The location is asked with the first page is loaded.
I tried 2 approachs:
Server-side with the gem 'geocoder', it works on my PC but on Iphone and android is working sometimes (maybe due to the timeout ? i set the maximum value but it don't change anything)
def localize
@location = lookup_ip_location
if @location.data["loc"].nil?
@user.latitude = 48.49
@user.longitude = -2.7
else
@user.latitude = @location.data['loc'].split(',').first.to_f
@user.longitude = @location.data['loc'].split(',').second.to_f
end
end
Client-side with the script 'navigator.geolocation.getCurrentPosition' , it works on PC but on mobile device, it's random and i can't pinpoint the main issue. On Iphone, sometimes, i need to unlock some privacy setting, on Android, i don't know.
<div class="container">
<input type="text" id="latitude">
<input type="text" id="longitude">
</div>
<script>
let latloc = document.getElementById('latitude');
let lonloc = document.getElementById('longitude');
var options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
function error(err) {
console.warn(`ERROR(${err.code}): ${err.message}`);
}
function setGeoCookie(position) {
var cookie_val = position.coords.latitude + "|" + position.coords.longitude;
document.cookie = "lat_lng=" + escape(cookie_val);
latloc.value = position.coords.latitude;
lonloc.value = position.coords.longitude;
}
function getGeoLocation() {
navigator.geolocation.getCurrentPosition(setGeoCookie, error, options);
}
getGeoLocation();
</script>
How do you manage location (and permission if needed) on a Rails app ?
Thanks