Created
June 8, 2016 15:50
-
-
Save raghubetina/adad51bb192628500354dea1252975dd to your computer and use it in GitHub Desktop.
Using HTML5 Location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>HTML5 Location Example</title> | |
| <!-- jQuery (included in Rails out of the box) --> | |
| <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script> | |
| </head> | |
| <body> | |
| <form> | |
| <div id="geolocation_error"></div> | |
| <label for="latitude">Latitude</label> | |
| <input id="latitude" type="text" name="lat" value=""> | |
| <label for="longitude">Longitude</label> | |
| <input id="longitude" type="text" name="lng" value=""> | |
| <button>Check In</button> | |
| </form> | |
| <script> | |
| $(document).ready(function() { | |
| // This code handles the case that HTML5 location doesn't work | |
| var output = $("#geolocation_error"); | |
| if (!navigator.geolocation){ | |
| output.html("<p>Geolocation is not supported by your browser</p>"); | |
| return; | |
| } | |
| function error() { | |
| output.html("Unable to retrieve your location"); | |
| }; | |
| // This code uses the coordinates to populate form inputs | |
| function success(position) { | |
| var lat = position.coords.latitude; | |
| var lng = position.coords.longitude; | |
| $("#latitude").val(lat); | |
| $("#longitude").val(lng); | |
| }; | |
| navigator.geolocation.getCurrentPosition(success, error); | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment