Last active
January 22, 2018 17:52
-
-
Save amrit3701/73030fd7a1c253a7fdc8392c0fb05380 to your computer and use it in GitHub Desktop.
Get current user 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
| function getLocation() { | |
| navigator.geolocation.getCurrentPosition(showPosition); | |
| } | |
| function showPosition(position) { | |
| console.log("Latitude: " + position.coords.latitude + | |
| "<br>Longitude: " + position.coords.longitude); | |
| } | |
| getLocation(); | |
| /* | |
| <Source>: https://www.w3schools.com/html/html5_geolocation.asp | |
| ===== | |
| <!DOCTYPE html> | |
| <html> | |
| <body> | |
| <p>Click the button to get your coordinates.</p> | |
| <button onclick="getLocation()">Try It</button> | |
| <p id="demo"></p> | |
| <script> | |
| var x = document.getElementById("demo"); | |
| function getLocation() { | |
| if (navigator.geolocation) { | |
| navigator.geolocation.getCurrentPosition(showPosition); | |
| } else { | |
| x.innerHTML = "Geolocation is not supported by this browser."; | |
| } | |
| } | |
| function showPosition(position) { | |
| x.innerHTML = "Latitude: " + position.coords.latitude + | |
| "<br>Longitude: " + position.coords.longitude; | |
| } | |
| </script> | |
| </body> | |
| </html> | |
| ==== | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment