How to get visitor’s location using geolocation with JavaScript?

Sometimes, we want to get visitor’s location using geolocation with JavaScript.

In this article, we’ll look at how to get visitor’s location using geolocation with JavaScript.

How to get visitor’s location using geolocation with JavaScript?

To get visitor’s location using geolocation with JavaScript, we can use the ipregistry API.

For instance, we write

const response = await fetch("https://api.ipregistry.co/?key=tryout");
const payload = await response.json();
console.log(payload.location.country.name, payload.location.city);

in an async function to make a GET request to https://api.ipregistry.co/?key=tryout with fetch.

Then we get the location data from the location property of the response.

Conclusion

To get visitor’s location using geolocation with JavaScript, we can use the ipregistry API.