2.3.1 • Published 4 months ago

simple-multi-geocoder v2.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

simple-multi-geocoder

Features

  • Geocode forward and reverse.
  • Autocomplete addresses.
  • Available both in the browser and node.

Supported providers

  • Google
  • Here
  • Mapbox

Installing

Package manager

Using npm:

$ npm install simple-multi-geocoder

Using yarn:

$ yarn add simple-multi-geocoder

Using pnpm:

$ pnpm add simple-multi-geocoder

Once the package has been installed, import the library using either import or require:

import { geocode, autocomplete } from "simple-multi-geocoder";

Here OAuth 2.0

Besides traditionnal API Key, Here provider supports OAuth 2.0 authentication, see here documentation for more information.

This package provides a helper function to generate the access token.

import { generateHereAccessToken } from "simple-multi-geocoder";
const accessToken = await generateHereAccessToken({
  clientId: "...",
  clientSecret: "...",
});

In that case, you should pass the bearerToken instead of apiKey in the options object.

const response = await geocode.forward("here", {
  credentials: { bearerToken: accessToken },
  query: "Rue du Belvédère 23, 1050 Ixelles, Belgique",
  country: "BE",
  language: "fr",
  limit: 1,
  raw: true,
});

API

Forward geocoding

geocode.forward(provider, options)

import { geocode } from "simple-multi-geocoder";

const API_KEY = "..."; // get it from secure environment
const address = "Rue du Belvédère 23, 1050 Ixelles, Belgique";

const response = await geocode.forward("here", {
  credentials: { apiKey: API_KEY },
  query: address,
  country: "BE",
  language: "fr",
  limit: 1,
});

console.log(response);
/*
{
  formattedAddress: "Rue du Belvédère 23, 1050 Ixelles, Belgique",
  latitude: 50.82679,
  longitude: 4.37359,
  components: {
    streetNumber: "23",
    streetName: "Rue du Belvédère",
    zipcode: "1050",
    city: "Ixelles",
    county: "Bruxelles",
    state: "Bruxelles",
    district: "Flagey - Malibran",
    country: "Belgique",
    countryCode: "BE",
  },
  extra: {
    id: "here:af:streetsection:NEk2q66IKlrCDNB4JhoMOC:CggIBCCc4o62ARABGgIyMw",
    confidence: 1,
  },
}
*/
ParameterDescription
provider (required)Provider (google, here, mapbox) token
options (required)Options object (see below)
options
ParameterTypeDescription
credentials (required)objectProvider access token (see below)
query (required)stringComplete address in string format (e.g. Rue du Belvédère 23, 1050 Ixelles, Belgique)
addressStructuredAddressStructured address object to geocode (see below)
languagestringLanguage of the returned result (IETF BCP 47 format)
countryCodestringLimit the search to a specific country (ISO_3166-1_alpha-2 format)
limitnumberMaximum number of results to be returned (default: 1) (not supported by Google)
rawbooleanReturn the raw result
paramsobjectParams object specific to the provider (see below)
Object StructuredAddress

Only available for Here provider for now, you can pass an object with the following fields:

import { StructuredAddress } from "simple-multi-geocoder";
const address: StructuredAddress = {
  street: "Rue du chemin",
  number: "123",
  zip: "1234",
  city: "City",
  countryCode: "Country Code (in ISO 3166-1 alpha-2 format)",
};
credentials

You should provide one of the following fields:

ParameterTypeDescription
apiKeystringProvider api key
bearerTokenstringProvider bearer token (only available for Here)
params

You can check the official API documentation from providers to see which options you can pass the geocoder

Results

We always return an array of object with the following fields

FieldTypeDescription
formattedAddressstringThe complete formatted address
latitudenumberThe latitude of the result
longitudenumberThe longitude of the result
componentsobjectThe address components (see below)
extraobjectAn object with additional informations (see below)
components
FieldTypeDescription
countrystringThe country where is located the result
countryCodestringThe country code of the result (ISO 3166-1 alpha-2 format)
statestringThe state where is located the result
regionstringThe region where is located the result
citystringThe city where is located the result
zipCodestringThe postal code of the city
streetNamestringThe street name where is located the result
streetNumberstringThe street number where is located the result
extra
FieldTypeDescription
idstringThe unique identifier of the result provided by the provider
bboxobjectThe bounding box of the result
confidencenumberA number between 0 and 1 indicating how the result location correspond to our query

Reverse geocoding

geocode.reverse(provider, options)

import { geocode } from "simple-multi-geocoder";

const API_KEY = ... // get it from secure environment
const address = "Rue du Belvédère 23, 1050 Ixelles, Belgique"

const coordinates = {
  latitude: 50.82679,
  longitude: 4.37359
}

const response = await geocode.reverse(
  "here",
  { apiKey: API_KEY, coordinates: coordinates, country: "BE", language: "fr", limit: 1 }
)

console.log(response)
/*
{
  formattedAddress: "Rue du Belvédère 23, 1050 Ixelles, Belgique",
  latitude: 50.82679,
  longitude: 4.37359,
  components: {
    streetNumber: "23",
    streetName: "Rue du Belvédère",
    zipcode: "1050",
    city: "Ixelles",
    county: "Bruxelles",
    state: "Bruxelles",
    district: "Flagey - Malibran",
    country: "Belgique",
    countryCode: "BE",
  },
  extra: {
    id: "here:af:streetsection:NEk2q66IKlrCDNB4JhoMOC:CggIBCCc4o62ARABGgIyMw",
    confidence: 1,
  },
}
*/
ParameterDescription
provider (required)Provider (google, here, mapbox) token
options (required)Options object (see below)
options
ParameterTypeDescription
credentials (required)objectProvider access token (see above)
coordinates (required)objectcoordinates of the point to reverse geocode
languagestringLanguage of the returned result (IETF BCP 47 format)
countryCodestringLimit the search to a specific country (ISO_3166-1_alpha-2 format)
limitnumberMaximum number of results to be returned (default: 1) (not supported by Google)
rawbooleanReturn the raw result
paramsobjectParams object specific to the provider (see below)
coordinates
FieldTypeDescription
latitudenumberLatitude of the point
longitudenumberLongitude of the point
params

You can check the official API documentation from providers to see which options you can pass the geocoder

Results

We always return an array of object with the following fields

FieldTypeDescription
formattedAddressstringThe complete formatted address
latitudenumberThe latitude of the result
longitudenumberThe longitude of the result
componentsobjectThe address components (see below)
extraobjectAn object with additional informations (see below)
components
FieldTypeDescription
countrystringThe country where is located the result
countryCodestringThe country code of the result (ISO 3166-1 alpha-2 format)
statestringThe state where is located the result
regionstringThe region where is located the result
citystringThe city where is located the result
zipCodestringThe postal code of the city
streetNamestringThe street name where is located the result
streetNumberstringThe street number where is located the result
extra
FieldTypeDescription
idstringThe unique identifier of the result provided by the provider
bboxobjectThe bounding box of the result
confidencenumberA number between 0 and 1 indicating how the result location correspond to our query

Autocomplete

autocomplete(provider, options)

import { autocomplete } from "simple-multi-geocoder";

const API_KEY = ... // get it from secure environment
const address = "Rue du Belvédère 23, 1050 Ixelles, Belgique"

const response = await autocomplete(
  "here",
  { apiKey: API_KEY, query: address, country: "BE", language: "fr", limit: 1 }
)

console.log(response)
/*
{
  formattedAddress: "Rue du Belvédère 23",
  components: {
    streetNumber: "23",
    streetName: "Rue du Belvédère",
    zipcode: "1050",
    city: "Ixelles",
    county: "Bruxelles",
    state: "Bruxelles",
    district: "Flagey - Malibran",
    country: "Belgique",
    countryCode: "BE",
  },
  extra: {
    id: "here:af:streetsection:NEk2q66IKlrCDNB4JhoMOC:CggIBCCc4o62ARABGgIyMw"
  },
}
*/
ParameterDescription
provider (required)Provider (google, here, mapbox) token
options (required)Options object (see below)
options
ParameterTypeDescription
credentials (required)objectProvider access token (see above)
query (required)stringPartial address in string format (e.g. Rue du Belvédère 23)
languagestringLanguage of the returned result (IETF BCP 47 format)
countryCodestringLimit the search to a specific country (ISO_3166-1_alpha-2 format)
limitnumberMaximum number of results to be returned (default: 1) (not supported by Google)
rawbooleanReturn the raw result
paramsobjectParams object specific to the provider (see below)
params

You can check the official API documentation from providers to see which options you can pass the geocoder

Results

We always return an array of object with the following fields

FieldTypeDescription
formattedAddressstringThe complete formatted address
components (optional)objectThe address components (see below)
extraobjectAn object with additional informations (see below)
components
FieldTypeDescription
countrystringThe country where is located the result
countryCodestringThe country code of the result (ISO 3166-1 alpha-2 format)
statestringThe state where is located the result
regionstringThe region where is located the result
citystringThe city where is located the result
zipCodestringThe postal code of the city
streetNamestringThe street name where is located the result
streetNumberstringThe street number where is located the result
extra
FieldTypeDescription
idstringThe unique identifier of the result provided by the provider
1.2.0

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.1.6

8 months ago

1.3.3

6 months ago

1.2.4

8 months ago

1.1.5

8 months ago

1.3.2

6 months ago

1.2.3

8 months ago

1.1.4

8 months ago

1.0.5

8 months ago

1.3.1

8 months ago

1.2.2

8 months ago

1.1.3

8 months ago

1.3.0

8 months ago

1.2.1

8 months ago

1.1.2

8 months ago

2.3.0

4 months ago

2.1.2

5 months ago

2.2.0

4 months ago

2.1.1

6 months ago

2.3.1

4 months ago

2.1.0

6 months ago

2.0.0

6 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago