Work with Localities

Use the LocalitiesService to geocode addresses and reverse geocode locations.

  1. Overview
  2. Getting started
  3. Geocode and Reverse Geocode

Overview

The LocalitiesService class provides methods for using the Localities API through Woosmap Map JavaScript API. It supports modern usage patterns such as Promises.

This guide focus Localities Geocode which provides location for addresses and vice versa.

Review the example below to see how to use this service for geocoding and reverse geocoding using the Woosmap Map JS API:

Getting started

Before using the Localities service in the Map JavaScript API, it is required to have a secure and valid Woosmap API key and enable the Localities API for it.

Next, you must load the Map JS API library by including a script tag in your HTML page as follows:

HTML
        <script async
        src="https://sdk.woosmap.com/map/map.js?key=YOUR_API_KEY&callback=initMap">
</script>

    

See the Get Started with Map JS API guide for more information.

Geocode and Reverse Geocode

Request Localities Geocode

To initiate a request to Localities Geocode in the Woosmap Map JS API, create an object of type woosmap.map.LocalitiesService and call LocalitiesService.geocode(), passing it a LocalitiesGeocodeRequest object literal.

The LocalitiesGeocodeRequest object literal requires either an address string to perform a geocode request or a latlng object for a reverse geocode request. It contains the following fields:

Type definition for LocalitiesGeocodeRequest
        {
    address?: string,
    components?: LocalitiesComponentRestrictions,
    countryCodeFormat?: string, //'alpha2' | 'alpha3'
    data?: LocalitiesRequestData,
    fields?: string,
    language?: string,
    latLng?: LatLng | LatLngLiteral,
}

    

Below is a sample LocalitiesGeocodeRequest for reverse geocoding:

JavaScript
        const reverseGeocodeRequest = {
    latlng: {lat: 43.610, lng: 3.876},
    language: "fr",
    components: {country: ["FR"]},
};

    

The LocalitiesService’s geocode() method is asynchronous and returns a Promise object that is either fulfilled with a LocalitiesPredictions object or rejected with an APIError object. You can attach handlers to this Promise object to process results upon operation success or failure.

JavaScript
        const localitiesService = new woosmap.map.LocalitiesService();
localitiesService
    .geocode(reverseGeocodeRequest)
    .then((response) => {
        console.log(response.results);
    })
    .catch((error) => {
        console.log(error);
    })
    .finally(() => {
        console.log("done");
    });

    

Localities Geocode Response

A successful geocode() call returns a LocalitiesGeocodeResponse object with a result field, which contains same field as a LocalitiesDetailsResponse.

Was this article helpful?
Have more questions? Submit a request