Geolocation get Country and City Name

In this article we will learn how we to fetch users current location or how to get geolocation in javascript, So first we need to learn about the browser navigators or what is window.navigator, So when we use some browser on desktop, mobile, laptop etc, there are some state what user need to enable, so browser can access some user specific information and provide to website on the browser in other terms window.navigator can be defined as below

Browser navigator Api (window.navigator)

The Navigator interface represents the state and the identity of the user agent. It allows scripts to query it and to register themselves to carry on some activities.

so for example if user enables bluethoot then in navigator api Navigator.bluetooth will return Bluetooth object for the current document, providing access to Web Bluetooth API functionality, javascript get gps location

Same way we have navigator.geolocation which provides the current latitude and longitude of the user from where he or she is browsing the website and for this we need to enables the locations setting from browser once user enables the location only then we are able to fetch current latitude and longitude. So below is the options showed in image by which user can enable the location on the website.

javascript geolocation ask permission, Once you Allow the website to enable user locations, the we need to fetch the latitude and longitude once we able to get these values then we will pass it to google geolocation API, So let learn how we can fetch the latitude and latitude navigator.geolocation javascript example is below

in above code we have a navigator object and inside the geolocation.getCurrentPosition() function which will return parameters, in first parameters we have the position as a result and in second we have error if there is some restrictions in getting the latitude and longitude (if location is not allowed by user or JavaScript is not working on browser), in above code userPosition is a callback function which we can execute and get the user location city etc. and the other parameter is error is we face any error in getting the position of the user.

Geocoding service (Google Maps Geocoding API)

Now we will use google geocoding service to get the user location, for that we need the google api key which you can get from google JavaScript API, below is the url from where you can get the API keys

https://developers.google.com/maps/documentation/geocoding/overview

Now lets try to extend the geolocation code to get the user location

Above code we have passed the user current latitude and longitude we get from navigator.geolocation.getCurrentPosition is passed to function userPosition, we stored potitions in lat, lon variable in function userPosition store and api url where we pass lat and lon variable https://maps.googleapis.com/maps/api/geocode/json?latlng=${lat},${lon}&key=${apiKey}

var apiKey you need to get is from google geocoding api after than by using fetch api method we hit the api url and if everything is correct then as a result we will get the current location city , state, pin code address of the user using browser

Further reading

  • React UseEffect | simplifyScript

    Understanding the useEffect hook in React

    In this article, we will learn about another React hook called useEffect(), through this article you will be able to understand what is react useEffect Hook and how to use useEffect() hook in react, this is most communally used hook after useState() hook, we mostly use this hook in every component on react now first […]
  • React useState function

    How to Use useState Hook in React Application

     In a React application, normal vanilla JavaScript may not work for some concepts. For example, if I want to toggle a class on a button click or add and remove classes, I might need to show a popup on the website. This can be easily achieved in vanilla JavaScript using the element.classList.toggle(“newClass”) method, allowing us to add […]
  • javascript DOm manipulation - simplifyscript

    What is DOM manipulation in javascript 

    In this article, we will explore JavaScript DOM manipulation, a key feature of JavaScript that allows developers to modify the Document Object Model (DOM) of a webpage. DOM manipulation enables you to dynamically alter the content and structure of HTML documents. To grasp this concept thoroughly, we’ll start by defining what the DOM is and […]