Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing fare property in Google directions javascript API

How do I access the fare property for transit directions in the google directions javascript API? The following line isn't working:

alert(response.routes[j].legs[k].fare.value); 

on the directions API page it says:

fare contains the total fare (that is, the total ticket costs) on this route. This property is only returned for transit requests and only for routes where fare information is available for all transit legs. The information includes:

  • currency: An ISO 4217 currency code indicating the currency that the amount is expressed in.
  • value: The total fare amount, in the currency specified above.

Thank you

like image 812
David H Avatar asked Dec 30 '15 07:12

David H


1 Answers

Fare info will not be included in the Directions response if your request is not identified with your API key[1]. This applies to both the Directions API web service and the Directions service in the Maps JavaScript API.

Here is an example for the web service, you'll see fare info (right after "copyrights") once you add your own API (Server) key:

https://maps.googleapis.com/maps/api/directions/json?origin=NE+Fremont+and+80th+Portland+OR&destination=Portland+OR&mode=transit&departure_time=1466096686

Here is an example for the JavaScript API: http://jsbin.com/rumugu/edit?html,output

  • if you put your (Browser) API key, you'll see "2,50 $" pop-up

  • if you remove the key= parameter altogether, "undefined" will pop-up

[1] Maps API for Work and Premium Plan customers can also use their Client ID.

like image 68
miguev Avatar answered Sep 24 '22 14:09

miguev