How to get Google Maps Public Transit Directions by HTTP server-side request

試著忘記壹切 提交于 2019-11-27 20:51:31

You just add &output=json to the end of the url.

I'd like to extend Mathias's answer a little bit.

  1. There's no official Google Transit API at the momemnt. Transits are provided by agencies, and most of Transits are not public. So, Google is not allowed to open them as API.

  2. You may try to consume the "unofficial" data using your link + "&output=json".

  3. However, the result won't be a valid JSON. Instead, that's something, that can be easily converted to a JavaScript object. (The differences are: there is no quotes around property names, the strings are not properly encoded etc.)

  4. Imagine you got this JavaScript object. However, it won't allow you to easily get the structured route details. Object's properties contain the route points coordinates, but no descriptions. The only place where the descriptions may be found is 'panel' property, which contains a chunk of HTML text (you may find a link to the sample of HTML in my blog post)

  5. So, you'll have to convert this HTML into XML (X-HTML) and then build the parser of this XML to get the essence data of a trip.

Seems like a bit of overkill to me. Having in mind, that "unofficial" API may change in the future, including slight changes in 'panel' HTML structure that will kill your parser.

@MathiasLin, how did you overcome this?

Google Maps Javascript v3 and the Directions Web Service now has this capability as per : http://googlegeodevelopers.blogspot.in/2012/06/public-transit-routing-and-layer-now.html

s6mike

Re choosing public transport - it seems that it defaults to public transport now (at least when I use it).

Changing the last URL parameter, dirflg=r, into dirflg=w switches the directions to walking mode.

Plus see:

Walking, bicycle and public transport directions with time required in each mode , iPhone

for more detail about the parameters.

Google direction API response is in HTML, JSON format please check https://developers.google.com/maps/documentation/javascript/directions?hl=lv

& section 'The DirectionsResult Object'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!