JavaScript - Detecting Your Location

In this tutorial we will create a Detecting Your Location using JavaScript. This code will display the information where the user is located when the button is clicked. The code use onclick() to initiate a method that use ajax call back to fetch a plugin from the internet, then use it to identify the user location. 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">JavaScript - Detecting Your Location</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-3">
  17. <button type="button" id="detect" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Find Location</button>
  18. </div>
  19. <div class="col-md-9">
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Country</th>
  24. <th>Latitude</th>
  25. <th>Longitude</th>
  26. </tr>
  27. </thead>
  28. <tbody id = "result" style="background-color:#fff;"></tbody>
  29. </table>
  30. </div>
  31. </div>
  32. </body>
  33. <script src="js/jquery-3.2.1.min.js"></script>
  34. <script src="js/script.js"></script>
  35. </html>

Creating the Script

This code contains the script of the application. This code will get 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. $(document).ready(function(){
  2. $('#detect').on('click', function(){
  3. var data = "";
  4.  
  5. $('#result').html("<tr><td colspan='6'><center>Now Loading...</center></td></tr>");
  6. $(this).attr('disabled', 'disabled');
  7.  
  8. setTimeout(function(){
  9. $.ajax({
  10. url: "https://geoip-db.com/jsonp",
  11. jsonpCallback: "callback",
  12. dataType: "jsonp",
  13. success: function(location) {
  14. data = "<tr>"
  15. + "<td>"+location.country_name+"</td>"
  16. + "<td>"+location.latitude+"</td>"
  17. + "<td>"+location.longitude+"</td>"
  18. + "</tr>";
  19.  
  20. $('#result').html(data);
  21.  
  22. }
  23. });
  24. }, 2000);
  25. });
  26. });
There you have it we successfully created a Detecting Your 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