This is a simple javascript to take the geolocation and show it on a webpage.
The example starts with a default HTML 5 page and the part from tags scripts get the geolocation and using innerHTML is shown on the webpage.
To see the location then is need to use this: navigator.geolocation.getCurrentPosition.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <!DOCTYPE html> <html > <head> <meta charset="UTF-8"> <title>Geolocation</title> </head> <body> <div> <p id="test">Click the button to get your current latitude and longitude</p> <button onclick="getLocation()">Where am I?</button> <script> var x=document.getElementById("test"); 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: <div id='lat-value'>" + position.coords.latitude + "</div><br>Longitude: <div id='long-value'>" + position.coords.longitude + "</div><br><div id='map-link'><a href='http://maps.google.com/?q=" + position.coords.latitude + "," + position.coords.longitude + "'>See it on Google</a></div>"; } </script> </div> </body> </html> |