npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

google-distance-matrix

v1.1.1

Published

A Node.js wrapper for Google Maps Distance Matrix API

Downloads

11,927

Readme

Node.js wrapper for Google Distance Matrix API

The Google Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations. The information returned is based on the recommended route between start and end points, as calculated by the Google Maps API, and consists of rows containing duration and distance values for each pair.

Please refer to Google Distance Matrix API documentation for further details on request parameters and response format.

Unlike similar modules, this one accepts multiple origins and the result data is just like the one returned by the Google API.

Installation

npm install google-distance-matrix

Usage

var distance = require('google-distance-matrix');

var origins = ['San Francisco CA'];
var destinations = ['New York NY', '41.8337329,-87.7321554'];

distance.matrix(origins, destinations, function (err, distances) {
    if (!err)
        console.log(distances);
})

Parameters

API Key

Please read the API Key documentation first.

If using a key:

  • There are 2 options to define the key:
  1. Create an environment variable GOOGLE_API_KEY, or...
  2. Programatically:
distance.key('YOUR-API-KEY');

If using client and signature:

  1. Create environment variables GOOGLE_CLIENT_KEY and GOOGLE_SIGNATURE_KEY, or...
  2. Programmatically:
distance.client('YOUR-CLIENT-KEY');
distance.signature('YOUR-SIGNATURE');

Options

Mode (optional): driving | walking | bicycling | transit, default driving

distance.mode('driving');

Language (optional): default en

distance.language('pt');

Avoid (optional): tolls | highways | ferries | indoor, default: null

distance.avoid('tolls');

Units (optional): metric | imperial, default: metric

distance.units('imperial');

Departure time (optional): desired time of departure as seconds since midnight, January 1, 1970 UTC

distance.departure_time(1404696787);

Arrival time (optional): desired time of arrival as seconds since midnight, January 1, 1970 UTC

distance.arrival_time(1404696787);

Traffic model (this parameter may only be specified for requests where the travel mode is driving, and where the request includes a departure_time): best_guess | pessimistic | optimistic, default best_guess

distance.traffic_model('optimistic');

Transit mode (this parameter may only be specified for requests where the mode is transit): bus | subway | train | tram | rail

distance.transit_mode('rail');

Transit routing preference (this parameter may only be specified for requests where the mode is transit): less_walking | fewer_transfers

distance.transit_routing_preference('fewer_transfers');

Origins

An array of one or more addresses and/or textual latitude/longitude values from which to calculate distance and time. If you pass an address as a string, the service will geocode the string and convert it to a latitude/longitude coordinate to calculate directions. If you pass coordinates, ensure that no space exists between the latitude and longitude values.

...
var origins = ['San Francisco CA', '40.7421,-73.9914'];
...

Destinations

An array of one or more addresses and/or textual latitude/longitude values to which to calculate distance and time. If you pass an address as a string, the service will geocode the string and convert it to a latitude/longitude coordinate to calculate directions. If you pass coordinates, ensure that no space exists between the latitude and longitude values.

...
var destinations = ['New York NY', 'Montreal', '41.8337329,-87.7321554', 'Honolulu'];
...

Example

var distance = require('google-distance-matrix');

var origins = ['San Francisco CA', '40.7421,-73.9914'];
var destinations = ['New York NY', 'Montreal', '41.8337329,-87.7321554', 'Honolulu'];

distance.key('<Your API key here>');
distance.units('imperial');

distance.matrix(origins, destinations, function (err, distances) {
    if (err) {
        return console.log(err);
    }
    if(!distances) {
        return console.log('no distances');
    }
    if (distances.status == 'OK') {
        for (var i=0; i < origins.length; i++) {
            for (var j = 0; j < destinations.length; j++) {
                var origin = distances.origin_addresses[i];
                var destination = distances.destination_addresses[j];
                if (distances.rows[0].elements[j].status == 'OK') {
                    var distance = distances.rows[i].elements[j].distance.text;
                    console.log('Distance from ' + origin + ' to ' + destination + ' is ' + distance);
                } else {
                    console.log(destination + ' is not reachable by land from ' + origin);
                }
            }
        }
    }
});