2010-12-30 10:50:14 +03:00
|
|
|
// Get driving direction using Google Directions API.
|
|
|
|
|
2011-01-01 09:09:02 +03:00
|
|
|
if (phantom.state.length === 0) {
|
2010-12-30 10:50:14 +03:00
|
|
|
var origin, dest;
|
2011-01-15 09:24:47 +03:00
|
|
|
if (phantom.args.length < 2) {
|
2011-01-01 07:10:34 +03:00
|
|
|
console.log('Usage: direction.js origin destination');
|
|
|
|
console.log('Example: direction.js "San Diego" "Palo Alto"');
|
2010-12-30 10:50:14 +03:00
|
|
|
phantom.exit(1);
|
|
|
|
}
|
2011-01-15 09:24:47 +03:00
|
|
|
origin = phantom.args[0];
|
|
|
|
dest = phantom.args[1];
|
2011-01-01 09:09:02 +03:00
|
|
|
phantom.state = origin + ' to ' + dest;
|
2010-12-30 10:50:14 +03:00
|
|
|
phantom.open(encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin +
|
|
|
|
'&destination=' + dest + '&units=imperial&mode=driving&sensor=false'));
|
|
|
|
} else {
|
|
|
|
if (phantom.loadStatus === 'fail') {
|
2011-01-01 07:10:34 +03:00
|
|
|
console.log('Unable to access network');
|
2011-01-02 21:39:50 +03:00
|
|
|
} else {
|
|
|
|
var steps = phantom.content.match(/<html_instructions>(.*)<\/html_instructions>/ig);
|
|
|
|
if (steps == null) {
|
|
|
|
console.log('No data available for ' + phantom.state);
|
|
|
|
} 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(phantom.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, ''));
|
|
|
|
}
|
2010-12-30 10:50:14 +03:00
|
|
|
}
|
|
|
|
phantom.exit();
|
|
|
|
}
|