Document Maps JavaScript API Advanced Tutorial Service plugins and tools Geocoding and Reverse Geocoding

Geocoding and Reverse Geocoding

In the JS API, you can call the geocoding/reverse geocoding interface to achieve the conversion between address description information and geographic coordinates (latitude and longitude). At the same time, combined with map styles and interactions, business needs are realized.

  • Forward Geocoding: For example, on a search page, the user inputs an address description, and then calls the forward geocoding interface to convert the address description into geographic coordinates (latitude and longitude).
  • Reverse Geocoding: For example, on a certain map page, users can select a point by clicking on the map, and then call the reverse geocoding interface to convert geographic coordinates (latitude and longitude) into address description information.

1、User Guide

AMap.Geocoder provides geocoding and reverse geocoding services, used for converting between address descriptions and latitude/longitude coordinates. The query results can be obtained through a callback function.

1.1 Forward Geocoding

Implement forward geocoding (referred to as geocoding) using the getLocation() method of AMap.Geocoder.

//introducing plugins, this example uses asynchronous introduction, more introduction methods https://lbs.amap.com/api/javascript-api-v2/guide/abc/plugins
AMap.plugin("AMap.Geocoder", function () {
  var geocoder = new AMap.Geocoder({
    city: "764010000", //city specifies the city for code query, supports passing adcode and citycode
  });

  var address = "25 Soi Charoen Krung 63, Khwaeng Yan Nawa, Khet Sathon";

  geocoder.getLocation(address, function (status, result) {
    if (status === "complete" && result.info === "OK") {
      //detailed geographic coordinate information in result
      console.log(result);
    }
  });
});

1.2 Reverse Geocoding

To implement reverse geocoding, use the getAddress() method of AMap.Geocoder.

//introducing plugins, this example uses asynchronous introduction, more introduction methods https://lbs.amap.com/api/javascript-api-v2/guide/abc/plugins
AMap.plugin("AMap.Geocoder", function () {
  var geocoder = new AMap.Geocoder();

  var lnglat = [100.50952,13.710995];

  geocoder.getAddress(lnglat, function (status, result) {
    if (status === "complete" && result.info === "OK") {
      //result is the corresponding detailed geographic information
      console.log(result);
    }
  });
});