Skip to content

A small node module to use Google Place API written in typescript

Notifications You must be signed in to change notification settings

gojob-1337/node-google-place

Repository files navigation

@gojob/google-place

travis build Coverage Status

Description

This package is a library for Node.js to consume the Google Place autocomplete & details API. It returns a small subset of their data.

Installation

yarn add @gojob/google-place

Usage

import * as gp from '@gojob/google-place';

Autocomplete API

const results = await gp.autocomplete({
    key: 'YOUR_API_KEY',
    input: 'Port-de-Bouc',
    language: 'fr',
    countries: ['fr'],
});

gp.autocomplete(query)

Name Type Description
key string Google API Key
input string Searched entry
language string Language to use (optional) Google Code
countries string[] Filter on a country (optional) ISO 3166-1 Alpha-2

Returns a list of prediction or throw if an error occurred.

interface IPrediction {
  id: string;
  name: string;
}

As:

Name Type Description
id string Place id
name string Place name

gp.retrieve(query)

const place = gp.retrieve({
    key: 'YOUR_API_KEY',
    id: 'ChIJ-RzUxt8ethIRksIaOl-0tJY',
    language: 'fr',
});

As:

Name Type Description
key string Google API Key
id string Searched entry
language string Language to use (optional) Google Code

Returns a place or throw if an error occurred.

export interface ILocation {
  lat: number;
  lng: number;
}

export interface IPlace {
  id: string;
  address: string;
  location: ILocation;
  postalCode: string;
  locality: string;
  administrativeAreaLevel1: string;
  administrativeAreaLevel2: string;
  countryCode: string;
  country: string;
}

As:

Name Type Description
id string Place id
address string Formatted address
location object Latitude and longitude coordinates
postalCode string Postal code
locality string Locality name (long or short name)
administrativeAreaLevel1 string Administrative Area Level 1 (long or short name)
administrativeAreaLevel2 string Administrative Area Level 2 (long or short name)
countryCode string Country short name
stateCode string Administrative Area Level 1 (short name)
country string Country name (long name)

postalCode, administrativeAreaLevel1 or administrativeAreaLevel2 may be empty string

gp.reverseGeocode(opts)

const place = gp.reverseGeocode({
    key: 'YOUR_API_KEY',
    latlng: 'XXX,YYY',
    language: 'fr',
});

As:

Name Type Description
key string Google API Key
latlng string Latitude and longitude coordinates (coma separated)
language string Language to use (optional) Google Code

Find an address from a location coordinates. Returns a IPlace.

About

A small node module to use Google Place API written in typescript

Resources

Stars

Watchers

Forks

Packages

No packages published