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

hoki311

hoki311

hoki311

hoki311

hoki311

RATU311

KUPU178

HOKI311

THOR311

THOR311

login hoki311

daftar hoki311

hoki 311

link hoki311

hoki311

RATU311

hoki311

hoki311

hoki311

hoki311

KUPU178

thor311

thor311

thor311

ceri188

ceri188

KUPU178

ceri188

ceri188

link ceri188

KUPU178

KUPU178

KUPU178

kupu178

ceri188

ceri188

ceri188

ceri188

ceri188

THOR311

ceri188

games online ceri188

link login ceri188

ceri188

thor311

RATU311

RATU311

RATU311

sbobet88

sbobet

parlay

mix parlay

judi bola online

thor311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

THOR311

thor311 RTP

thor311

slot gacor

kupu178

slot mahjong

ceri188

sabung ayam

ratu311

THOR311

THOR311

THOR311

thor311 aplikasi

ratu311 sabung ayam

slot gacor

RATU311

ceri188

ceri188

ceri188

ceri188

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

ceri188

ceri188

ceri188 alternatif

ceri188

link ceri188

ceri188

RATU311

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

Togel Online

KUPU178

Situs Togel

KUPU178

ceri188

ceri188

KUPU178

KUPU178

ceri188

ceri188

ratu311

ceri188

ceri188

ratu311

KUPU178

THOR311

ceri188

ceri188

THOR311

THOR311

RATU311

thor311 tajen bali

thor311 alternatif

situs toto4d

thor311 toto4d

thor311 akses

ceri188

daftar ceri188

cery188

ceri188

slot gacor

kupu 178

kupu178

ceri1888

ceri188

slot online

hoki311

togel online

ceri188

ding dong

ratu311

slot gacor

kupu178

live casino

kupu178

mix parlay

ceri188

judi bola online

thor311 slot

THOR311

RATU311

KUPU178

THOR311

ceri188

ratu311

slot online

hoki311

kupu178

togel online

kupu178

slot gacor

ceri188

judi bola online

ceri188

ratu311

kupu178

RATU311

RATU311

live casino roullete

ceri188

slot pg soft

kupu178

KUPU178

THOR311

RATU311

KUPU178

THOR311

THOR311

thor311 domino qq

thor311 akses

thor dingdong

ceri188

ceri188

kupu178

kupu178

togel

kupu178

mix parlay

ceri188

slot online

kupu178

THOR311

THOR311

KUPU178

kupu178

hoki311

togel online

kupu178

ceri188

ceri188

www.vrcorporate.in

phbalance.vn

togel hongkong

kupu178

slot gacor

kupu178

kupu178

ceri188

kupu178

KUPU178

RATU311

slot online

kupu178

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

toto macau

KUPU178

Slot Gacor

KUPU178

RATU311

Mahjong ways

KUPU178

KUPU178

KUPU178

RATU311

RATU311

Toto Togel

KUPU178

CERI188

Slot Sweet Bonanza 2500

KUPU178

RATU311

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

THOR311

HOKI311

KUPU178

Slot Mahjong Ways

KUPU178

CERI188

Slot Online Resmi

KUPU178

RATU311 SLOT ONLINE

KUPU178

KUPU178: Link Akses Resmi

KUPU178: Strategi Menang Di Game Live Casino

KUPU178

KUPU178: Minimal 200 Rupiah

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

CERI188

RATU311: Alternatif karnpuracollege

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

RATU311 NONTON BOLA

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

RATU311 judi bola

KUPU178

HOKI311

RATU311

THOR311

HOKI311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

KUPU178

RATU311

THOR311