JavaScript GeoLocation


/ Published in: JavaScript
Save to your folder(s)

Html and JavaScript code to see the longitude and latitude of the viewing browser.


Copy this code and paste it in your HTML
  1. HTML
  2. <!doctype html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <title>Where am I</title>
  7. <script src="myLoc.js"></script>
  8. <link rel="stylesheet" href="myLoc.css">
  9. </head>
  10.  
  11. <body>
  12. <div id="location">
  13. You location will go here.
  14. </div>
  15. </body>
  16. </html>
  17.  
  18.  
  19. JAVASCRIPT
  20. window.onload = getMyLocation;
  21.  
  22. function getMyLocation()
  23. {
  24. if(navigator.geolocation)
  25. {
  26. navigator.geolocation.getCurrentPosition(displayLocation);
  27. }
  28. else{
  29. alert("Oops, no geolocation support");
  30. }
  31. }
  32.  
  33. function displayLocation(position)
  34. {
  35. var latitude = position.coords.latitude;
  36. var longitude = position.coords.longitude;
  37.  
  38. var div = document.getElementById("location");
  39. div.innerHTML = "You are at Latitude: "+latitude+ ", Longitude: "+longitude;
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.