You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 7-8.RSVP
Socket
Socket
Sign inDemoInstall

@googlemaps/google-maps-services-js

Package Overview
Dependencies
Maintainers
2
Versions
87
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@googlemaps/google-maps-services-js

Node.js client library for Google Maps API Web Services


Version published
Weekly downloads
261K
decreased by-0.48%
Maintainers
2
Install size
2.19 MB
Created
Weekly downloads
 

Package description

What is @googlemaps/google-maps-services-js?

@googlemaps/google-maps-services-js is a Node.js client library for Google Maps Services. It allows developers to access various Google Maps APIs, such as Geocoding, Directions, Distance Matrix, Elevation, and Places, among others. This package simplifies the process of integrating Google Maps functionalities into Node.js applications.

What are @googlemaps/google-maps-services-js's main functionalities?

Geocoding

Geocoding converts addresses into geographic coordinates. This example demonstrates how to use the geocode method to get the latitude and longitude of a given address.

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

client.geocode({
  params: {
    address: '1600 Amphitheatre Parkway, Mountain View, CA',
    key: 'YOUR_API_KEY'
  }
}).then(response => {
  console.log(response.data.results);
}).catch(error => {
  console.log(error);
});

Directions

The Directions API calculates directions between locations. This example shows how to get directions from New York to Los Angeles.

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

client.directions({
  params: {
    origin: 'New York, NY',
    destination: 'Los Angeles, CA',
    key: 'YOUR_API_KEY'
  }
}).then(response => {
  console.log(response.data.routes);
}).catch(error => {
  console.log(error);
});

Distance Matrix

The Distance Matrix API provides travel distance and time for a matrix of origins and destinations. This example demonstrates how to get the distance and travel time between New York and Los Angeles.

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

client.distancematrix({
  params: {
    origins: ['New York, NY'],
    destinations: ['Los Angeles, CA'],
    key: 'YOUR_API_KEY'
  }
}).then(response => {
  console.log(response.data.rows);
}).catch(error => {
  console.log(error);
});

Places

The Places API allows you to search for places within a specified area. This example shows how to find nearby restaurants within a 1500-meter radius of a given location.

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

client.placesNearby({
  params: {
    location: { lat: 37.7749, lng: -122.4194 },
    radius: 1500,
    type: 'restaurant',
    key: 'YOUR_API_KEY'
  }
}).then(response => {
  console.log(response.data.results);
}).catch(error => {
  console.log(error);
});

Other packages similar to @googlemaps/google-maps-services-js

Readme

Source

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: "asdf",
    },
    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.

OldNew
Can provide paramsCan provide params, headers, instance, timeout (see Axios Request Config)
API key configured at ClientAPI key configured per method in params object
Retry is supportedRetry is configurable via axios-retry or retry-axios
Does not use promises by defaultPromises are default
Typings are in @types/googlemapsTypings are included
Does not support keep aliveSupports keep alive
Does not support interceptorsSupports interceptors
Does not support cancelalationSupports 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.

Keywords

FAQs

Package last updated on 05 Jan 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc