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