Categories
JavaScript Answers

How to get city name from a latitude and longitude point with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *