How to Retrieve your Geo Location using jQuery in JavaScript

How to Retrieve your Geo Location using jQuery in JavaScript


In this tutorial we will create a How to Retrieve your Geo Location using jQuery in JavaScript. This tutorial purpose is to teach how to locate your geo location. This will thoroughly take the basic usage of api for locating your actual location . I will provide a sample program to show the actual coding of this tutorial. So Let's do the coding.

Before we get started:

First you have to download the jQuery plugin.

Here is the link for the jQuery that i used in this tutorial

Lastly, this is the link for the template that i used for the layout design

Creating The Interface

This is where we will create a simple interface for our application. This code will only display the tables and html buttons. To create this simply copy and write it into your text editor, then save it 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="">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">How to Retrieve your Geo Location using jQuery</h3>
  15. <hr style="border-top:1px dotted #ccc;"/>
  16. <div class="col-md-8">
  17. <table class="table table-bordered">
  18. <thead class="alert-info">
  19. <tr>
  20. <th>Country Code</th>
  21. <th>Latitude</th>
  22. <th>Longitude</th>
  23. <th>City</th>
  24. </tr>
  25. </thead>
  26. <tbody id="result" style="background-color:#fff;"></tbody>
  27. </table>
  28. </div>
  29. <div class="col-md-2">
  30. <button type="button" id="search" class="btn btn-primary"><span class="glyphicon glyphicon-search"></span> Check My Location</button>
  31. </div>
  32. </div>
  33. </body>
  34. <script src="js/jquery-3.2.1.min.js"></script>
  35. <script src="script.js"></script>
  36. </html>

Creating JavaScript Function

This is where the main function of the application is. This code will locate your location and display information of your place. To do this just copy and write these block of codes inside the text editor and save it as script.js.
  1. $(document).ready(function(){
  2. $('#search').on('click', function(){
  3. let data = "";
  5. $('#result').html("<tr><td colspan='6'><center>Please wait...</center></td></tr>");
  6. $(this).attr('disabled', 'disabled');
  8. setTimeout(function(){
  9. $.ajax({
  10. url: "",
  11. jsonpCallback: "callback",
  12. dataType: "jsonp",
  13. success: function(location) {
  14. data = "<tr>"
  15. + "<td>"+location.country_code+"</td>"
  16. + "<td>"+location.latitude+"</td>"
  17. + "<td>"+location.longitude+"</td>"
  18. + "<td>""</td>"
  19. + "</tr>";
  21. $('#result').html(data);
  23. }
  24. });
  25. }, 2000);
  26. });
  27. });

In the given code we first call the basic jQuery ready event to signal that the DOM of the page is now ready to be use. We use a special api that can locate your location in real time called geojs. This api can use several functions that can display any geo information such as country, ip, map, etc.


The How to Retrieve your Geo Location using jQuery in JavaScript source code that I provide can be download below. Please kindly click the download button.

There you have it we successfully created How to Retrieve your Geo Location using jQuery in 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!

More Tutorials for JavaScript Language

JavaScript Tutorials

Add new comment