Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
distance-matrix-api
Advanced tools
Readme
The Distance Matrix API is a service offered by www.DistanceMatrixAPI.com 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 DistanceMatrixAPI, and consists of rows containing duration and distance values for each pair.
Please refer to 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 API.
npm install distance-matrix-api
var distance = require('distance-matrix-api');
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);
})
You can get the key for free without credit cards by registering at distancematrixapi.com
API_KEY
, or...distance.key('YOUR-API-KEY');
I
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');
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'];
...
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'];
...
var distance = require('distance-matrix-api');
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);
}
}
}
}
});
FAQs
A Node.js wrapper for Distance Matrix API offered by DistanceMatrixAPI.com
The npm package distance-matrix-api receives a total of 19 weekly downloads. As such, distance-matrix-api popularity was classified as not popular.
We found that distance-matrix-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.