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.

user323075
  • 81
  • 1
  • 1
  • 3
  • Possible duplicate of [Calculate driving directions using PHP?](http://stackoverflow.com/questions/1800510/calculate-driving-directions-using-php) – miguev Jul 07 '16 at 10:17

5 Answers5

6

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

Mathias Conradt
  • 27,904
  • 20
  • 132
  • 186
  • What about xml? when I do output=xml or output=kml, the saved file is blank :( – C0D3 Dec 25 '11 at 14:01
  • xml/kml doesn't seem to work. I guess only json is available. Not an option to parse json for your case? – Mathias Conradt Dec 25 '11 at 16:43
  • 1
    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? – C0D3 Jan 05 '12 at 00:07
  • 1
    Using this method is undocumented and therefore a) subject to breaking and b) may violate maps terms of use – Mano Marks Jan 10 '12 at 21:33
  • @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. – Mathias Conradt Jan 11 '12 at 01:00
  • @ManoMarks But in fact it is documented: http://code.google.com/apis/maps/documentation/directions/#JSON and http://code.google.com/apis/maps/documentation/webservices/ – Mathias Conradt Jan 11 '12 at 01:02
  • @MathiasLin That is the official API, take a look at the URLs, they're different between the question and the web service, http://maps.google.com/?saddr=... vs. http://maps.googleapis.com/maps/api/directions/ – Mano Marks Jan 11 '12 at 01:47
  • @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: http://code.google.com/apis/maps/documentation/directions/ – Mano Marks Jan 11 '12 at 01:49
  • @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. – Mathias Conradt Jan 11 '12 at 02:39
  • @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: http://www.google.com/intl/en_us/help/terms_maps.html – Mano Marks Jan 11 '12 at 16:26
  • @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. – Mathias Conradt Jan 11 '12 at 16:31
  • 1
    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 routes – Dermot Nov 27 '12 at 04:24
  • @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: https://maps.google.com/?saddr=436, Civil Line Road, Civil Lines, Gurgaon, Haryana 122007&daddr=A-06, Infocity,Gurgaon, Haryana&dirflg=r&output=json – dilipkumar1007 Mar 29 '16 at 07:38
  • @Dermot 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 – dilipkumar1007 Mar 29 '16 at 07:39
  • @dilipkumar1007 My post was made over 4 years ago, I imagine they've probably changed their API at this point – Dermot Mar 30 '16 at 00:26
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?

Mikhail Shilkov
  • 30,280
  • 3
  • 56
  • 87
  • 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. – Mathias Conradt Jul 24 '12 at 14:51
  • @MathiasLin, did I understand correctly, that output=xml used to work, but it doesn't anymore? Don't you use google transit 'API' anymore? – Mikhail Shilkov Jul 27 '12 at 13:41
  • 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. – Mathias Conradt Jul 28 '12 at 03:57
  • Is there any update on the availability of google transit api? – sachinr May 16 '14 at 01:36
  • @sachinr Yes, it's available since August 2012. See the links here: http://mikeshilkov.wordpress.com/2012/08/14/google-adds-public-transit-into-api/ – Mikhail Shilkov May 16 '14 at 13:25
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

Shailesh
  • 51
  • 1
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'

Vijay Kumbhar
  • 866
  • 1
  • 13
  • 31
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.

Community
  • 1
  • 1
s6mike
  • 470
  • 5
  • 10