2010-12-30 10:50:14 +03:00
|
|
|
// Get driving direction using Google Directions API.
|
|
|
|
|
2011-09-13 00:17:54 +04:00
|
|
|
var page = require('webpage').create(),
|
2012-01-14 19:13:26 +04:00
|
|
|
system = require('system'),
|
2011-05-26 01:10:35 +04:00
|
|
|
origin, dest, steps;
|
|
|
|
|
2012-01-14 19:13:26 +04:00
|
|
|
if (system.args.length < 3) {
|
2011-05-26 01:10:35 +04:00
|
|
|
console.log('Usage: direction.js origin destination');
|
|
|
|
console.log('Example: direction.js "San Diego" "Palo Alto"');
|
|
|
|
phantom.exit(1);
|
|
|
|
} else {
|
2012-01-14 19:13:26 +04:00
|
|
|
origin = system.args[1];
|
|
|
|
dest = system.args[2];
|
2011-05-26 01:10:35 +04:00
|
|
|
page.open(encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin +
|
|
|
|
'&destination=' + dest + '&units=imperial&mode=driving&sensor=false'), function (status) {
|
|
|
|
if (status !== 'success') {
|
|
|
|
console.log('Unable to access network');
|
2011-01-02 21:39:50 +03:00
|
|
|
} else {
|
2011-05-26 01:10:35 +04:00
|
|
|
steps = page.content.match(/<html_instructions>(.*)<\/html_instructions>/ig);
|
|
|
|
if (steps == null) {
|
2011-05-26 13:04:00 +04:00
|
|
|
console.log('No data available for ' + origin + ' to ' + dest);
|
2011-05-26 01:10:35 +04:00
|
|
|
} else {
|
|
|
|
steps.forEach(function (ins) {
|
|
|
|
ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>');
|
|
|
|
ins = ins.replace(/\<div/ig, '\n<div');
|
|
|
|
ins = ins.replace(/<.*?>/g, '');
|
|
|
|
console.log(ins);
|
|
|
|
});
|
|
|
|
console.log('');
|
|
|
|
console.log(page.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, ''));
|
|
|
|
}
|
2011-01-02 21:39:50 +03:00
|
|
|
}
|
2011-05-26 01:10:35 +04:00
|
|
|
phantom.exit();
|
|
|
|
});
|
2010-12-30 10:50:14 +03:00
|
|
|
}
|