How can I use google maps API to return a journey time with AND without traffic?
I'd like a way of being able to calculate journey time WITH and without traffic, so I can work o开发者_StackOverflow中文版ut the difference so I know how much the traffic is delaying you by.
Also, is there a way of doing it based on a selected date and time? As I know google has this feature, just not sure if their API has made it available. Is it possible without loading up a map for the end visitor?
Thanks!
Unfortunately Google does not support traffic data.
You can populate a map with traffic data in terms of coloured roads, which show how good/bad the traffic is ( http://code.google.com/apis/maps/documentation/javascript/examples/layer-traffic.html ). However this is not supported on many roads or in many countries.
Unfortunately there is no support in the API to incorporate traffic into directions/distances.
Google has many links with live traffic monitoring agencies who send them data. TomTom also collects this data and creates information like this: http://www.tomtom.com/livetraffic/
A lot of agencies charge developers for this data, some readily make it available as an RSS feed.
It'll unfortunately be hard for you to collect all this information yourself but I think if you contact TomTom (for example) they would willingly give you their raw data live for a fee.
Hope that helps.
you can use http get and take data in json for example
http://maps.googleapis.com/maps/api/distancematrix/json?origins=49.52041,11.001359&destinations=52.518271,13.408374&mode=driving&language=en-US&sensor=false
I think you will be able to journey time. But I am having doubt with and without traffic time api provides or not.
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 use two latitude, longitudes as my start and end point, 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.
Please refer this google map api example
http://code.google.com/apis/maps/documentation/javascript/examples/distance-matrix.html
example:
var origin1 = new google.maps.LatLng(55.930385, -3.118425);
var origin2 = "Greenwich, England";
var destinationA = "Stockholm, Sweden";
var destinationB = new google.maps.LatLng(50.087692, 14.421150);
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [origin1, origin2],
destinations: [destinationA, destinationB],
travelMode: google.maps.TravelMode.DRIVING,
avoidHighways: false,
avoidTolls: false
}, callback);
function callback(response, status) {
// See Parsing the Results for
// the basics of a callback function.
}
set the distance matric parameters according to your need.
精彩评论