Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Maps API to get travel time data [closed]

All the examples I've come across using Google Maps API seem to show a map of some kind. I would like to incorporate the data about the estimated travel time by car they give you when you ask for a road description from A to B into a site. And only that data. Is it possible without loading up a map for the end visitor?

like image 517
Felix Andersen Avatar asked Jun 25 '09 09:06

Felix Andersen


People also ask

How do I get travel time on Google Maps?

Swipe left or right through the transportation options until you see the transit icon. Just below the transit options, you will see “Depart at…”. Tap on “Depart at…”, and adjust the time that you wish to depart. Use the Picker Item to choose the date and time that you would like to depart for your trip, then tap Done.

Can I get traffic data from Google Maps API?

Google Maps traffic information is available via the Distance Matrix API, and to access it, the only thing you need to do is to get a developer key from Google. You can call the service up to 2,500 times in a 24-hour period for free, which should be fine for most advertisers.

Is Google Distance Matrix API free?

The Distance Matrix API uses a pay-as-you-go pricing model.

Does Google Maps travel time stop?

Include multiple travel stops to get an accurate ETAGoogle Maps lets you add stops so you can get a more accurate destination time.


2 Answers

The above answer is in violation of Google API terms of service, and hence, incorrect.

The Google Maps API only allows you to calculate travel time if it's referenced against a Google Map displayed to the user.

You can't use the API if you don't display a Google Map to the end user of the service.

Update:

Any answer here, that does not show the final results mapped on a google map, is in violation of the aforementioned terms of service and eventually, incorrect.

like image 155
Barry Avatar answered Sep 28 '22 21:09

Barry


Yep, this is definitely possible using the API. You can construct a GDirections object without a map or directions div. You can do a load request from A to B and then call the getDuration method to get the total travel time.

First you need to create a directions object:

// no need to pass map or results div since // we are only interested in travel time. var directions = new GDirections ();  

Then do your load request to resolve the directions (I have used two latitudes and longitudes as my start and end points, but you can use addresses here as well):

var wp = new Array (); wp[0] = new GLatLng(32.742149,119.337218); wp[1] = new GLatLng(32.735347,119.328485); directions.loadFromWaypoints(wp); 

Then you need to listen for the load event so you can call getDuration once the directions have been resolved:

GEvent.addListener(directions, "load", function() {     $('log').innerHTML = directions.getDuration ().seconds + " seconds";         }); 

You can find the whole example here and the JavaScript here. You can check the Google Maps Documentation for more info about the options you can give the GDirections object (like walking distance etc...). You should also listen to the error event for geocoding failures.

like image 38
RedBlueThing Avatar answered Sep 28 '22 20:09

RedBlueThing