ruby - A rails app which gets users location using HTML5 geolocation and saves to data base -
seems pretty simple i'm struggling.
here's have far in view. display location coords test working. want persist on database, hence ajax call. doing right way or there easier or better way?
<p id="demo">click button coordinates:</p> <button onclick="getlocation()">try it</button> <script> var x=document.getelementbyid("demo"); var lat; var long; function getlocation() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(showposition); } else{x.innerhtml="geolocation not supported browser.";} } function showposition(position) { lat = position.coords.latitude; long = position.coords.longitude; x.innerhtml="latitude: " + lat + "<br>longitude: " + long; $.ajax({ type: 'post', url: 'http://localhost:3000/locations', data: { lat: lat, long: long }, contenttype: 'application/json', datatype: 'json' }); } </script>
you can try "easier" way, use geocoder gem, provides multiple methods in order fetch user location, 1 of them request.
request.location.city => medellin request.location.country => colombia
you can find useful information in following links
Comments
Post a Comment