DirectionsService - OVER_QUERY_LIMIT after 3 request? - google-maps

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.

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

Google Maps Geocoding i18n

I'm using Google Maps Geocoding via json in my i18n website to validate event addresses and save them to ddbb.
https://developers.google.com/maps/documentation/javascript/geocoding
My main problem is that if an user (with english lang in browser) register an event address in Spain, I will get country as Spain instead of EspaƱa (and the same problem for adminAreas, city, etc).
For countries we have countryCode that solves the problem, but there isn't a code for cities. This results in duplicated (not related) cities in my ddbb.
Is there a way to get results in several languages in the same json request to be able to save translations?
There is the language parameter, but i don't think we can't put several codes separated by coma (because is not explained in documentation).
The only solution I've is make different requests for every lang in my site before save a new address.
We have now an optional parameter 'language' to tell google geocode api wich one to use:
https://developers.google.com/maps/documentation/geocoding/index#ReverseGeocoding
https://developers.google.com/maps/faq#languagesupport
Here you can find language codes supported:
https://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1
geocoder.geocode( { 'address': addresInputField.value, 'language': 'es'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
//do whatever with results
return true;
} else {
//do whatever with errors
return false;
}
});

Limitations of snap to road google api v3

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.

Asynchronous Call not working in Coffee Script

I am using CoffeeScript, Backbone.js and Google Maps API to reverse geocode a lat / lng
I have the function
country: (origin, callback) ->
#geocoder = new google.maps.Geocoder
#geocoder.geocode(
'latLng': origin,
(results, status) =>
if status is google.maps.GeocoderStatus.OK
callback(result[6])
else alert("Geocode was not successful for the following reason: " + status);
)
When I call this I use:
#country(origin, (data) =>
console.log(data.formatted_address)
)
btw:
origin = new google.maps.LatLng(origin_lat, origin_lng)
This does not work, it does not even seem to call it. I have the call back function under (data) but can not get it to work...
Thanks
Not sure about the overall context of your question, but I will take a stab at it anyway.
The way you've defined your country function, it will typically be part of an object or map. If you just want a standalone function, try:
country = (origin, callback) ->
...
Then when calling it, try to replace #country(...) with country(...).
Based on the comments, your call to geocode also looks wrong. You need to create an instance of GeocoderRequest and pass it instead of the 'latLng': origin pair. Unfortunately I haven't used the Google style APIs much, so if this is your real question I'm not much use to you.

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.