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);
to call fetch
to make a get request to https://api.ipregistry.co/?key=tryout
Then we get the response from it with json
.
And then we get the country and city data from the payload
.
We put that in an async function.