megamind
megamind

Reputation: 444

How do i get number of tolls between two points in google direction api?

I have to get numbers of tolls when driving from point A to B. I am using google direction API for this. The API provides information weather the road is toll road or not in html_instructions tag in response ,but a tolls road may contain number to tolls,Is there a way to count them.

Upvotes: 4

Views: 3540

Answers (1)

Deep 3015
Deep 3015

Reputation: 10075

Using directions-draggable code. In order to calculate the number of tolls in the way , you have to find them in myroute.legs[i].steps[j].instructions which is the result of direction display services. To find toll road I am using regular expression

Check fiddle demo

Code below

function containsWord(string, word) {
    return new RegExp('(?:[^.\w]|^|^\\W+)' + word + '(?:[^.\w]|\\W(?=\\W+|$)|$)').test(string);
}
function computeTotalDistance(result) {
  var total = 0;
  var myroute = result.routes[0];
  var totalTolls=0;
  for (var i = 0; i < myroute.legs.length; i++) {
  //console.log(myroute.legs[i].steps);
  for(var j=0;j<myroute.legs[i].steps.length;j++){
  //console.log(myroute.legs[i].steps[j].instructions);
  if(containsWord(myroute.legs[i].steps[j].instructions.toLowerCase(), 'toll road')){
  totalTolls+=1;
  }

  }
    total += myroute.legs[i].distance.value;
  }
  total = total / 1000;
  document.getElementById('total').innerHTML = total + ' km';
  document.getElementById('totalTollCount').innerHTML = totalTolls ;
}

Upvotes: 5

Related Questions