Get user location from Browser using javascript and Geolocation API


Get user location from Browser using javascript and Geolocation API

Hi friends, in this tutorial I will explain how to get user location from Browser using javaScript and HTML Geolocation API. This is a very essential part of any web application and is often required at the time of developing websites or any kind of web application because everyone wants to track the current location of the user. As such, we can do this with the help of HTML geolocation API and Google geocodes API.

Get user location from Browser using javascript and Geolocation API
Get user location from Browser using javascript and Geolocation API

HTML Geolocation API

The HTML Geolocation API is used to get the geographic location of the user with the help of the getCurrentPosition() method.

Steps to get user location from browser using javascript and HTML Geolocation API

Step 1:- We will declare a function inside button type using the onclick event of JavaScript.

Step 2:- Next, we will use that function to return the visitor location if succeed.

Step 3:- After that, we have to check whether the browser supports the geolocation or not with the help of window.navigator.geolocation object as shown below

if(window.navigator.geolocation){
            //alert('Browser support');
            window.navigator.geolocation.getCurrentPosition(mycoords,myerrors);
  }
 else{
            alert('Browser does not support');
   }

Step 4:- Now, if the browser supports, we will pass two parameters (mycoords,myerrors) inside the getCurrentPosition() method.

Step 5:- Now, declare the mycoords() function as shown below

 function mycoords(position){

      var lat = position.coords.latitude;
      var long = position.coords.longitude;
      //alert(lat+'/'+long);
      var myapi = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+long+"&key=your API key";

      $.get(myapi,function(data){
        console.log(data);
        $('#user_location').val(data.results[2].formatted_address);
      });
    }

Explanation of the above function

The above function will return the latitude and longitude of the user’s location with the help of the position object and we will pass the lat and long variable to Google geocode API URL to get the address of the user.

To use the Google geocode API, we have to create an API key. If you don’t know how to create an API key then follow the below article

Google places autocomplete example using maps JavaScript API

The Geocode API URL should be as shown below

https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+long+"&key=your API key

If the Geocode API works successfully then you can see the array format of the user location in the console as shown below

Get user location from Browser using javascript and Geolocation API

Step 6:- Now, declare the myerrors() function as shown below

function myerrors(error){
       if(error.code=0){
          alert('Unknown Error');
       }
       else if(error.code=1){
          alert('Permission Denied');
       }
       else if(error.code=2){
          alert('Position Unavailable');
       }
       else{
          alert('Time Out');
       }
    }

Explanation of the above function

myerrors is the second parameter of the getCurrentPosition() method that is used to handle the errors with an error object that is passed as a parameter inside the myerrors() function. The myerrors() function will be executed if we fail to get the user’s location.

The error object will throw the below errors

  • Unknown error.
  • Permission Denied.
  • Position unavailable.
  • Time Out.

Complete Code:-

<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="width: 60%;">
  <br><br><br><br>
  <h3>Get user location from browser using javascript and HTML Geolocation API</h3>
    <div class="form-group">
      <button type="button" class="btn btn-success btn-sm" onclick="getlocation()">Click here to get user location</button>
    </div>
    <div class="form-group">
      <input type="text" class="form-control" id="user_location">
    </div>
</div>
<script>
  function getlocation(){
      
        if(window.navigator.geolocation){
            //alert('Browser support');
            window.navigator.geolocation.getCurrentPosition(mycoords,myerrors);
          }
          else{
            alert('Browser does not support');
          }
       }

function mycoords(position){

      var lat = position.coords.latitude;
      var long = position.coords.longitude;
      //alert(lat+'/'+long);
      var myapi = "https://maps.googleapis.com/maps/api/geocode/json?latlng="+lat+","+long+"&key=AIzaSyBh1__iOLq4pmTZES2w8Ss1dWFs2ITWCys";
      $.get(myapi,function(data){
        console.log(data);
        $('#user_location').val(data.results[2].formatted_address);
      });
    }
     function myerrors(error){

       if(error.code=0){
          alert('Unknown Error');
       }
       else if(error.code=1){
          alert('Permission Denied');
       }
       else if(error.code=2){
          alert('Position Unavailable');
       }
       else{
          alert('Time Out');
       }
    }
  </script>
</body>
</html>

Step 7:- Now open the browser and run the HTML file.

Conclusion:- I hope it will help you to understand the overview of user geolocation. If you have any doubt then please leave a comment below.


Leave a Comment

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

RATU311

Mahjong Ways 2

CERI188

CERI188

sv388

CERI188

KUPU178 TOTO SLOT

CERI188

CERI188 TOTO Singapore

KUPU178

RATU311

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

parlay

HOKI311

THOR311

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311