To get city name from a latitude and longitude point with JavaScript, we use the MapQuest API.
For instance, we write
const fetchLocationName = async (lat, lng) => {
const response = await fetch(
"https://www.mapquestapi.com/geocoding/v1/reverse?key=API-Key&location=" +
lat +
"%2C" +
lng +
"&outFormat=json&thumbMaps=false"
);
const responseJson = await response.json();
console.log(JSON.stringify(responseJson));
};
to make a get request to the MapQuest reverse geocoding URL with fetch
with the lat
latitude and lng
longitude.
Then we call json
to get the JSON response body from response
.