direction.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Get driving direction using Google Directions API.
  2. var page = require('webpage').create(),
  3. system = require('system'),
  4. origin, dest, steps;
  5. if (system.args.length < 3) {
  6. console.log('Usage: direction.js origin destination');
  7. console.log('Example: direction.js "San Diego" "Palo Alto"');
  8. phantom.exit(1);
  9. } else {
  10. origin = system.args[1];
  11. dest = system.args[2];
  12. page.open(encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin +
  13. '&destination=' + dest + '&units=imperial&mode=driving&sensor=false'), function (status) {
  14. if (status !== 'success') {
  15. console.log('Unable to access network');
  16. } else {
  17. steps = page.content.match(/<html_instructions>(.*)<\/html_instructions>/ig);
  18. if (steps == null) {
  19. console.log('No data available for ' + origin + ' to ' + dest);
  20. } else {
  21. steps.forEach(function (ins) {
  22. ins = ins.replace(/\&lt;/ig, '<').replace(/\&gt;/ig, '>');
  23. ins = ins.replace(/\<div/ig, '\n<div');
  24. ins = ins.replace(/<.*?>/g, '');
  25. console.log(ins);
  26. });
  27. console.log('');
  28. console.log(page.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, ''));
  29. }
  30. }
  31. phantom.exit();
  32. });
  33. }