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

5

8

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.

Arms answered 22/4, 2010 at 9:20 Comment(1)
Possible duplicate of Calculate driving directions using PHP?Yardley
R
6

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

Roddie answered 22/6, 2010 at 2:43 Comment(15)
What about xml? when I do output=xml or output=kml, the saved file is blank :(Honea
xml/kml doesn't seem to work. I guess only json is available. Not an option to parse json for your case?Roddie
Even with json the url doesn't seem to give directions for different travel modes, only driving. Can someone post the url for other travel modes please?Honea
Using this method is undocumented and therefore a) subject to breaking and b) may violate maps terms of useTendril
@ManoMarks The method is now working for 2 years+ at least, and if it changes, you would just push another apk update, shouldn't be a big deal; better than not able to offer anything at all. b) which term? Just because it's not documented doesn't mean you're violating terms. I've been discussing the map route topic with Google Maps sales partners (when wanting route navigation natively on Android) and afaik this is the way to go legally. But please let me know which term in the Google Maps ToS you might see violated here; would be happy to check and eventually confirm it.Roddie
@ManoMarks But in fact it is documented: code.google.com/apis/maps/documentation/directions/#JSON and code.google.com/apis/maps/documentation/webservicesRoddie
@MathiasLin That is the official API, take a look at the URLs, they're different between the question and the web service, maps.google.com/?saddr=... vs. maps.googleapis.com/maps/api/directionsTendril
@MathiasLin Full dislosure: I work on the Google Maps API team. Just to clarify, using the Maps API webservice is within our terms of service: code.google.com/apis/maps/documentation/directionsTendril
@ManoMarks thanks for the info. Yes, I was aware of differing URLs. However, if using the other URL, which ToS would be abused? I think usage would still be within the ToS. (But of course, the user should use the WS url anyway.) Asking, because I did study the ToS in detail before quite a bit.Roddie
@MathiasLin, using the maps.google.com URL is not using the API, it's using maps.google.com as an output stream. Therefore it falls under Google Maps Terms of Use. I am not a lawyer, so I can't give an official legal determination, and it seems like 2a is on point: google.com/intl/en_us/help/terms_maps.htmlTendril
@ManoMarks Alright, I guess we just leave it as this, doesn't matter much anyway. Best to use webservices API anyway. I think whether I use a browser or my Android app to get the data, it's all the same basically, a http client, making a http request. Technically the same, should be no difference therefore. Anyway, not that important anymore. Cheers.Roddie
You can change the transport option by changing the dirflg= param. From what I can figure out, dirflg=w == walking - dirflg=c == driving (car) - dirflg=r == Public transport (maybe stands for Rail?) and dirflg=b == bike routesControvert
@MathiasConradt When I'm trying to hit URL by adding &output=json to the end of the URL, getting not found result. Please suggest me. Thnx in advance. This is the URL which I'm trying to hit: maps.google.com/?saddr=436, Civil Line Road, Civil Lines, Gurgaon, Haryana 122007&daddr=A-06, Infocity,Gurgaon, Haryana&dirflg=r&output=jsonThanhthank
@Controvert When I'm trying to hit URL by adding &output=json to the end of the URL, getting not found result. Please suggest me. Thnx in advanceThanhthank
@Thanhthank My post was made over 4 years ago, I imagine they've probably changed their API at this pointControvert
F
4

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?

Fluviatile answered 10/4, 2012 at 6:10 Comment(5)
Answering your question, even though a bit late: We've used that approach with output=xml (not json) and parsed the xml, which worked fine. If the 'unofficial' API would change in the future, we'd just simply push another update of the apk.Roddie
@MathiasLin, did I understand correctly, that output=xml used to work, but it doesn't anymore? Don't you use google transit 'API' anymore?Fluviatile
Didn't use transit API; the project was done in 2010 but is discontinued, so not sure if my mentioned approach still works, but it worked ok back then in 2010.Roddie
Is there any update on the availability of google transit api?Vindication
@Vindication Yes, it's available since August 2012. See the links here: mikeshilkov.wordpress.com/2012/08/14/…Fluviatile
L
3

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

Update: Latest server side API docs: https://developers.google.com/maps/documentation/directions/get-directions

Landgravine answered 15/8, 2012 at 16:39 Comment(0)
F
1

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.

Fissionable answered 15/2, 2012 at 14:0 Comment(0)
L
1

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'

Lightfoot answered 17/7, 2013 at 7:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.