Limitations of snap to road google api v3 - google-maps

Update:
I have some gps locatiwaon points and i want to draw the path.I the way draw using polylines which dont follow road.I want to do "snap to road",so that i can improve the way path.
I referred this code to snap to road for the locations saved in array "snap_path_points"
I am getting path only for first 8 calls (limitation),below is code
var service = new google.maps.DirectionsService(), poly,snap_path=[];
poly = new google.maps.Polyline({ map: map });
for(j=0;j<snap_path_points.length-1;j++){
service.route({
origin: snap_path_points[j],
destination: snap_path_points[j+1],
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
snap_path = snap_path.concat(result.routes[0].overview_path);
poly.setPath(snap_path);
}
});
}
I divided my locations to batches of 8 and tried the same (i.e if 24 points use 3 for loops ) but i am getting OVER_QUERY_LIMIT.
i wonder how its working with event listener here.
OVER_QUERY_LIMIT indicates the webpage has sent too many requests
within the allowed time period.
Is there anyway to plot complete snap to road path with more than 8 points.

The problem is not that you sent too many points it is that you made too many request in the given time period. The snap to roads api states that:
Users of the standard API:
2,500 free requests per day and 10 requests per second
So if you sent more request than the limit described above, it is the reason why you are getting that error.

Related

Google maps api fastest route around traffic

I have a SPA that I need to be able to provide the user directions between two points. I want the directions to work that same way that they do currently on the web version of google maps. IE. https://www.google.ca/maps/dir/ in the hosted by google web version when you request directions it will give you the fastest route considering traffic... (See Screenshot)
Currently when I issue the request using the following code it only returns a single route that does not consider traffic.
var directionsService = this.get('directionsService');
var directionsDisplay = this.get('directionsDisplay');
directionsService.route({
origin: new google.maps.LatLng(this.get('currentLocation.lat'),this.get('currentLocation.lng')),
destination: new google.maps.LatLng(toAddress.lat,toAddress.lng),
travelMode: 'DRIVING',
provideRouteAlternatives: true
}, function(response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
Does anyone know thr correct way to do this?
Check number of routes returned by DirectionService.route, the callback will have a DirectionsResult object with an array of DirectionsRoute, which contains the information about the legs and steps its composed.
If its only returning 1 route, try to set provideRouteAlternatives to true in your DirectionsRequest object.
Happy coding!
TyBourque.
I think current google map API doesn't support direction map with traffic layer.
It is only available using embedded map API.
If you have already found the best solution (not using embedded map API), please write here
Regards

"duration_in_traffic" value is not match with Google Map site

Live traffic duration time is different from Google Map application and Google Map Distance Matrix API response.
I have tried the following way to get live traffic duration as showing the Google Map application.
First Way
https://maps.googleapis.com/maps/api/distancematrix/json?
key=<MyKey>&origins=-33.6771817,151.12005739999995&
destinations=-33.7704818,150.98828609999998&travelMode=DRIVING&
departure_time=[Current UTC Time]
Second Way
var service = new google.maps.DistanceMatrixService();
service.getDistanceMatrix(
{
origins: [{lat: <lng>, lng: <lng>}, 'Origin Name'],
destinations: ['Destination Name', {lat: <lat>, lng: <lng>}],
travelMode: 'DRIVING',
drivingOptions: {
departureTime: [CurrentUTCTime>],
trafficModel: 'best_guess'
}
}
API results are successfully received but "duration_in_traffic" node value is not match with Google Map Application's live traffic duration time (please refer the attached images)
please assist me what am i doing wrong in this. thanks in advance.
I can get the exact "draffic_in_duration" value by using the first way with departure_time parameter value as now
https://maps.googleapis.com/maps/api/distancematrix/json?
key=<MyKey>&origins=-33.6771817,151.12005739999995&
destinations=-33.7704818,150.98828609999998&travelMode=DRIVING&
departure_time=now
cheers

Google Maps suggests bad direction

I am currently working with the Google Maps API to render directions.
From the technical side everything works fine:
var $canvas = $element.querySelector('#map-canvas');
vvar map = new maps.Map($canvas, {
center: new maps.LatLng(52.46004869999999, 13.37898690),
mapTypeId: maps.MapTypeId.ROADMAP,
zoom: 14
});
var route = {
travelMode: google.maps.TravelMode.BICYCLING,
origin: new google.maps.LatLng(52.455833, 13.322948),
destination: new google.maps.LatLng(52.459281, 13.356367),
};
new google.maps.DirectionsService().route(route, function(body) {
var display = new google.maps.DirectionsRenderer();
display.setMap(map);
display.setDirections(body);
});
Unfortunately the suggested route is absolutely crap. Instead of going directly from A to B it even leaves the city...
Why does this work good with maps.google.com but not with the API? What do I have to change so I get a correct result?
Bodo
The origin is placed on a train station, it seems the DirectionsService suggests to put the car on a train, because there is a vehicle Shipping available.
Google-Maps doesn't use the exact origin, it seems that it geocodes the LatLng and takes the result with type "street_address"(it's Berlinickestraße 16A, 12165 Berlin, Deutschland) as origin.
You may do the same(geocode the LatLng's first and use the results with a type of street_address as origin and destination)

DirectionsService - OVER_QUERY_LIMIT after 3 request?

This is my code :
function traceRoute(part, arr) {
var polylineTrattaTreno = new Array();
var request = {
origin: part,
destination: arr,
travelMode: google.maps.TravelMode.TRANSIT
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
... manage my data
} else {
console.log(status);
}
});
}
and I noticed that if I call this function 3 times, all is ok! Than I always get OVER_QUERY_LIMIT. Tried on putting timeout of 1 or 2 seconds. But nothing happens. Seems that I can't do more than 3 request for time?
According to the Documentation-
Use of the Google Directions API is subject to a query limit of 2,500
directions requests per day. Each directions search will count as a
single request against your daily quota when the mode of
transportation is driving, walking or cycling. Searching for transit
directions will count as 4 requests.
Adding to it-
Additionally, note that Directions API URLs are restricted to 2048
characters, before URL Encoding. As some Directions service URLs may
involve many locations along a path, be aware of this limit when
constructing your URLs.
Your origin and destination values might be quite distant and also considering the fact you are using transit direction, causing the limit being reached soon.
Update- If you set the mode to "transit" you must also specify either a departure_time or an arrival_time.

Google Maps V3 Route Destination Mark Edit

Using the google.maps.DirectionsService.route() and google.maps.DirectionsRenderer.setDirections() method, is it possible to change the text on the info window for the destination, without creating a custom parser for the journey?
I couldn't see anything in the API which allowed you to access the markers of the route.
I don't want any code, just yes/no, and a hint for the right direction to take.
Current Function:
var request = {
origin: origPoint,
destination: new google.maps.LatLng(dest.lat(), dest.lng()),
travelMode: google.maps.DirectionsTravelMode.DRIVING,
region: "GB"
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
Thanks,
Psy
I'm afraid that the api doesn't offer a direct way to access the info windows or access the markers.
But there are several ways to achieve that parsing parts of the result data:
I guess the simplest way to change text on the info window is to overwrite the DirectionsLegs' start and/or end addresses of the DirectionsResult. You have to do it before calling directionsDisplay.setDirections(result).
Or you can display just the polyline (see suppressMarkers and suppressInfoWindows of renderer's options) and create the markers and infowindows yourself - you have to access data from DirectionsLegs of DirectionsResult.
I would prefer the second way since it's cleaner and you have more freedom in adjustments. The first way, it's only a hack and you are just changing the text.