npm install @googlemaps/google-maps-services-js

Node.js client library for Google Maps API Web Services

About @googlemaps/google-maps-services-js

The @googlemaps/google-maps-services-js package is a powerful Node.js client library designed to make it easier for developers to integrate Google Maps API Web Services into their applications. This npm package provides robust tools that allow for the simple execution of tasks such as geocoding, reverse geocoding, directions, distance matrix calculations, and more. By utilizing this library, developers can enhance their applications with dynamic mapping capabilities such as plotting routes, calculating distances between locations, and converting addresses into geographic coordinates. The inclusion of such features can significantly improve user engagement by providing interactive and useful map-based functionalities.

To get started with integrating these extensive mapping services into your Node.js application, you can simply run the command npm install @googlemaps/google-maps-services-js. This command installs the package and all necessary dependencies, setting up your project to harness the full potential of Google Maps API Web Services. Once installed, developers can easily access various services provided by the Google Maps platform, enabling the implementation of location-based features in a straightforward manner. The ease of installation and setup makes it an ideal choice for projects requiring detailed geographic data and interactive maps.

The @googlemaps/google-maps-services-js library is continuously updated to support the latest features offered by Google Maps APIs, ensuring developers have access to the most advanced tools for location services. The package's compatibility with modern JavaScript and Node.js practices allows for seamless integration and scalability within a wide range of applications, from small projects to large-scale enterprise systems. By leveraging this npm package, developers can create more engaging and personalized user experiences, boosting the functionality and appeal of any application that requires detailed geographic information or mapping capabilities.

More from googlemaps

googlemaps npm packages

Find the best node modules for your project.

Search npm

@googlemaps/js-api-loader

Wrapper for the loading of Google Maps JavaScript API script in the...

Read more
,

@googlemaps/markerclusterer

Creates and manages per-zoom-level clusters for large amounts of markers...

Read more
,

@googlemaps/google-maps-services-js

Node...

Read more
,

@googlemaps/url-signature

Sign a URL for Google Maps Platform requests...

Read more
,

@googlemaps/jest-mocks

[![npm](https://img.shields.io/npm/v/@googlemaps/jest-mocks)](https://www.npmjs...

Read more
,

@googlemaps/markerwithlabel

[![npm](https://img.shields.io/npm/v/@googlemaps/markerwithlabel)](https://www.npmjs...

Read more
,

@googlemaps/three

[![npm](https://img.shields.io/npm/v/@googlemaps/three)](https://www.npmjs...

Read more

Dependencies

Core dependencies of this npm package and its dev dependencies.

@googlemaps/url-signature, agentkeepalive, axios, query-string, retry-axios, @types/jest, @types/node, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, eslint, eslint-config-prettier, eslint-plugin-jest, eslint-plugin-prettier, jest, nock, prettier, ts-jest, typedoc, typescript

Documentation

A README file for the @googlemaps/google-maps-services-js code repository. View Code

Node.js Client for Google Maps Services

npm Test Release codecov GitHub contributors semantic-release Discord

This library is a refactor of a previous version published to @google/maps. It is now being published to @googlemaps/google-maps-services-js.

Use Node.js? Want to geocode something? Looking for directions? This library brings the Google Maps API Web Services to your Node.js application.

The Node.js Client for Google Maps Services is a Node.js Client library for the following Google Maps APIs:

Keep in mind that the same terms and conditions apply to usage of the APIs when they're accessed through this library.

Attention!

This library is designed for server-side Node.js applications. Attempting to use it client-side, in either the browser or any other environment like React Native, may in some cases work, but mostly will not. Please refrain from reporting issues with these environments when attempting to use them, since server-side Node.js applications is the only supported environment for this library. For other environments, try the Maps JavaScript API, which contains a comparable feature set, and is explicitly intended for use with client-side JavaScript.

Quick Start

$ npm install @googlemaps/google-maps-services-js

Below is a simple example calling the elevation method on the client class.

Import the Google Maps Client using TypeScript and ES6 module:

import {Client} from "@googlemaps/google-maps-services-js";

Alternatively using JavaScript without ES6 module support:

const {Client} = require("@googlemaps/google-maps-services-js");

Now instantiate the client to make a call to one of the APIs.

const client = new Client({});

client
  .elevation({
    params: {
      locations: [{ lat: 45, lng: -110 }],
      key: process.env.GOOGLE_MAPS_API_KEY,
    },
    timeout: 1000, // milliseconds
  })
  .then((r) => {
    console.log(r.data.results[0].elevation);
  })
  .catch((e) => {
    console.log(e.response.data.error_message);
  });

Reference Documentation

The generated reference documentation can be found here. The TypeScript types are the authoritative documentation for this library and may differ slightly from the descriptions.

Developing

In order to run the end-to-end tests, you'll need to supply your API key via an environment variable.

$ export GOOGLE_MAPS_API_KEY=AIza-your-api-key
$ npm test

Migration

This section discusses the migration from @google/maps to @googlemaps/google-maps-services-js and the differences between the two.

Note: The two libraries do not share any methods or interfaces.

The primary difference is @google/maps exposes a public method that takes individual parameters as arguments while @googlemaps/google-maps-services-js exposes methods that take params, headers, body, instance(see Axios). This allows direct access to the transport layer without the complexity that was inherent in the old library. Below are two examples.

Old (@google/maps):

const googleMapsClient = require('@google/maps').createClient({
  key: 'your API key here'
});

googleMapsClient
  .elevation({
    locations: {lat: 45, lng: -110}
  })
  .asPromise()
  .then(function(r) {
    console.log(r.json.results[0].elevation);
  })
  .catch(e => {
  console.log(e);
  });

New (@googlemaps/google-maps-services-js):

const client = new Client({});

client
  .elevation({
    params: {
      locations: [{ lat: 45, lng: -110 }],
      key: process.env.GOOGLE_MAPS_API_KEY
    },
    timeout: 1000 // milliseconds
  }, axiosInstance)
  .then(r => {
    console.log(r.data.results[0].elevation);
  })
  .catch(e => {
    console.log(e);
  });

The primary differences are in the following table.

Old New
Can provide params Can provide params, headers, instance, timeout (see Axios Request Config)
API key configured at Client API key configured per method in params object
Retry is supported Retry is configurable via axios-retry or retry-axios
Does not use promises by default Promises are default
Typings are in @types/googlemaps Typings are included
Does not support keep alive Supports keep alive
Does not support interceptors Supports interceptors
Does not support cancelalation Supports cancellation

Premium Plan Authentication

Authentication via client ID and URL signing secret is provided to support legacy applications that use the Google Maps Platform Premium Plan. The Google Maps Platform Premium Plan is no longer available for sign up or new customers. All new applications must use API keys.

const client = new Client({});

client
  .elevation({
    params: {
      locations: [{ lat: 45, lng: -110 }],
      client_id: process.env.GOOGLE_MAPS_CLIENT_ID,
      client_secret: process.env.GOOGLE_MAPS_CLIENT_SECRET
    },
    timeout: 1000 // milliseconds
  })
  .then(r => {
    console.log(r.data.results[0].elevation);
  })
  .catch(e => {
    console.log(e.response.data.error_message);
  });

Support

This library is community supported. We're comfortable enough with the stability and features of the library that we want you to build real production applications on it. We will try to support, through Stack Overflow, the public surface of the library and maintain backwards compatibility in the future; however, while the library is in version 0.x, we reserve the right to make backwards-incompatible changes. If we do remove some functionality (typically because better functionality exists or if the feature proved infeasible), our intention is to deprecate and give developers a year to update their code.

If you find a bug, or have a feature suggestion, please log an issue. If you'd like to contribute, please read How to Contribute.