Display User Location Using JavaScript Source Code

In this tutorial we will create a Display User Location using JavaScript. This code will locate and display the information of user location when the button is clicked. The code use onclick() to initiate a method that will locate the whereabouts of user using dot notation geolocation a buil-in function that has a several functionalities that serve as a gps locator. This program is a user-friendly. Feel free to use it in your system. We will use JavaScript to allow you to implement complex things on web pages. It enables you to create dynamically updating content and add some interactive visual for the user transactions.

Getting Started:

This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12. <div class="col-md-3"></div>
  13. <div class="col-md-6 well">
  14. <h3 class="text-primary">Display User Location Using JavaScript Source Code</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-6">
  17. <button class="btn btn-primary" onclick="getLocation()">Get Location</button>
  18. <br /><br />
  19. <div class="alert alert-info">User Location</div>
  20. <div class="alert alert-danger">
  21. <h3>Latitude:<br />
  22. <span id="latitude"></span></h3>
  23. <h3>Longitude:<br />
  24. <span id="longitude"></span>
  25. </h3>
  26. </div>
  27.  
  28. </div>
  29. </div>
  30. <script src="js/script.js"></script>
  31. </body>
  32. </html>

Creating the Script

This code contains the script of the application. This code will display the user location when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory
  1. var latitude;
  2. var longitude;
  3.  
  4. function getLocation(){
  5. if(navigator.geolocation) {
  6. navigator.geolocation.getCurrentPosition(function(a) {
  7. latitude = a.coords.latitude;
  8. longitude = a.coords.longitude;
  9.  
  10. document.getElementById('latitude').innerHTML = latitude;
  11. document.getElementById('longitude').innerHTML = longitude;
  12. });
  13. }else{
  14. alert('Geolocation not supported.');
  15. }
  16. }
There you have it we successfully created a Display User Location using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment