Skip to content

Instantly share code, notes, and snippets.

@cvan
Created March 24, 2015 07:46
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save cvan/014c2ba39e95fade9073 to your computer and use it in GitHub Desktop.
Save cvan/014c2ba39e95fade9073 to your computer and use it in GitHub Desktop.
Google Maps multiple destinations sorted by shortest path
require('es6-promise').polyfill();
var request = require('request');
const API_KEY = process.env.GOOGLE_MAPS_API_KEY || '';
var places = [
'steins, mountain view, CA',
'st. stephens green, mountain view, CA',
'shana thai, mountain view, CA',
'beefys cabin, sunnyvale, CA',
];
var promises = [];
var _fetch = function (data) {
return new Promise(function (resolve, reject) {
request.get(data, function (err, response) {
if (err) {
reject(err);
} else {
if (response.statusCode !== 200) {
return reject(new Error('Unexpected response code: ' +
response.statusCode));
}
resolve(response);
}
});
});
};
function url(name, data) {
switch (name) {
case 'directions':
if (Array.isArray(data)) {
data = {waypoints: data};
}
if (!data.mode) {
data.mode = 'walking';
}
return 'https://maps.googleapis.com/maps/api/directions/json?origin=' +
encodeURIComponent(data.waypoints[0]) +
'&destination=' + encodeURIComponent(data.waypoints[data.waypoints.length - 1]) +
'&waypoints=optimize:true|' + data.waypoints.map(encodeURIComponent).join('|') +
'&optimizeWaypoints=true&mode=' + data.mode;
case 'geocode':
if (typeof data === 'string') {
data = {address: data};
}
return 'https://maps.google.com/maps/api/geocode/json?key=' + API_KEY +
'&address=' + encodeURIComponent(data.address);
}
}
places.forEach(function (place) {
promises.push(
_fetch({
url: url('geocode', place),
json: true
}).then(function (response) {
return response.body;
})
);
});
Promise.all(promises).then(function (addresses) {
var coords = addresses.map(function (address) {
return [address.results[0].geometry.location.lat,
address.results[0].geometry.location.lng].join(',');
});
return _fetch({
url: url('directions', coords),
json: true
}).then(function (response) {
return response.body;
});
}).then(function (data) {
var waypointOrder = data.routes[0].waypoint_order;
var placesOrdered = waypointOrder.map(function (orderIdx) {
return places[orderIdx];
});
return placesOrdered;
})
.then(console.log)
.catch(console.error);
@Lakhtarsingh
Copy link

i dont want it in shortest path i want same like normal as per the priority set in path

@Venommm92N
Copy link

Can you please explain the code not necessarily every line, but comment on what each chunk of code does..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment