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

ceri188

ceri188

ceri188

KUPU178

HOKI311

RATU311

KUPU178

RATU311

RATU311

kupu178

kupu178

HOKI311

togel

kupu178

ratu311

mix parlay

ceri188

slot online

kupu178

KUPU178

RATU311

THOR311

THOR311

THOR311

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

kupu178

hoki311

THOR311

hoki311

togel online

kupu178

slot mahjong

ceri188

slot server thailand

ratu311

ceri188

ceri188

RATU311

punjabi virasat

mimcord.com

Centrum Medyczne

www.vrcorporate.in

phbalance.vn

THOR311

KUPU178

daftar slot gacor

hoki311

togel hongkong

kupu178

slot mahjong

ratu311

slot gacor

kupu178

kupu178

ceri188

kupu178

ceri188

THOR311

KUPU178

KUPU178

KUPU178

RATU311

kupu178

RATU311

RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

slot online

kupu178

thor311

thor311

kupu178

kupu178

RATU311

KUPU178

THOR311

THOR311

THOR311

KUPU178

slot mahjong ways

hoki311

slot gacor

ceri188

ratu311

kupu178

Casino Baccarat Online

kupu178

kupu178

ceri188

ceri188

THOR311

THOR311

RATU311

toto macau

KUPU178

slot gacor

HOKI311

sabung ayam online

RATU311

SLOT LUCKY NEKO

CERI188

Slot Gacor

KUPU178

RATU311

RATU311

Link Spaceman

CERI188

Slot Gacor Online

THOR311

Mahjong ways

KUPU178

THOR311

THOR311

Live Casino

HOKI311

CERI188

CERI188

THOR311

THOR311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

CERI188

HOKI311 SLOT

TOTO TOGEL TOKYO

CERI188

CERI188 SITE

CERI188

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

Toto Togel

KUPU178

Slot Mahjong

CERI188

CERI188

CERI188

Slot Sweet Bonanza 2500

KUPU178

SLOT ONLINE

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178 X SLOT777

judi bola online

THOR311

KUPU178 10 GAME SLOT TERBAIK

KUPU178

KUPU178 SLOT TERPERCAYA

KUPU178 TOGEL

TOTO 4D MACAU

KUPU178

SITUS BOLA

CERI188

KUPU178

RATU311

THOR311

THOR311

HOKI311

KUPU178

RATU311

THOR311

CERI188

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

Slot Gacor Online

KUPU178

Mix Parlay

CERI188

CERI188

THOR311

THOR311

THOR311

THOR311

THOR311

KUPU178

Slot Thailand

CERI188

CERI188

THOR311 RTP

RATU311

RATU311

RATU311

CERI188

KUPU178 TOTO SLOT

CERI188 TOTO Singapore

THOR311

KUPU178

KUPU178

RATU311

CERI188

RATU311: Alternatif karnpuracollege

RATU311

RATU311

Thor311 Terbaru

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188: Masuk Akun Slot Super Cepat

THOR311 PRO

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

HOKI311

KUPU178

RATU311

RATU311

RATU311

RATU311

THOR311

CERI188

CERI188

RATU311

RATU311

RATU311

RATU311

Game Thor311

HOKI311

HOKI311

RATU311

HOKI311

HOKI311

THOR311

RATU311 NONTON BOLA

RATU311

HOKI311 Link QRIS EWALLET

RATU311

HOKI311 Link Apk Resmi

CERI188

RATU311 judi bola

KUPU178

situs judi bola

HOKI311

RATU311

THOR311

HOKI311

KUPU178

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

KUPU178

KUPU178

RATU311

RATU311

KUPU178

THOR311

slot thailand

judi bola

KUPU178

slot gacor

slot gacor

yakuza303

slot gacor

RATU311

THOR311