-->

How to get Google Maps Public Transit Directions b

2020-01-31 02:27发布

问题:

I use such urls like:

http://maps.google.com/?saddr=546%206th%20Avenue,%20New%20York,%20NY%2010011%20(Sixth%20Avenue,%20New%20York)&daddr=W%20103rd%20St,%20New%20York,%20NY%20&dirflg=r

But it returns full html page. Does anybody know how to get such info in json or xml or any parsable format?

I need public transit.

回答1:

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



回答2:

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?



回答3:

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



回答4:

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.



回答5:

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'