InterviewSolution
| 1. |
Write HTML5 code to demonstrate the use of Geolocation API. |
|
Answer» <!DOCTYPE html><html> <body> <P>Click "try it" 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 functionality is not supported by this browser."; } } function showPosition(position) { x.innerHTML = "Latitude: " + position.coords.latitude + "<br>Longitude: " + position.coords.longitude; } </script> </body></html> The above example asks for user PERMISSION for accessing the location data VIA geolocation API and after CLICKING the button the coordinates of the physical location of the client get displayed. |
|