1

I am using google maps to draw the path among various location which are stored in database.

fiddle

While passing 15 geopoints(anything more than 10) am getting status as OVER_QUERY_LIMIT.

I understand that we need to give some milliseconds of time gap when we pass more than 10 geopoints per seconds.

My question is HOW TO DO THAT..Where to add sleep() or setTimeout() or any other time delay code

I've tried all,maximum all possibilities provided at SO but failed.As because they are just saying give some time gap between request but how to do that?

Code Snippet:

    var markers = [
                        {
                            "title": 'abc',
                            "lat": '17.5061925',
                            "lng": '78.5049901',
                            "description": '1'
                        },                  


                        {
                            "title": 'abc',
                            "lat": '17.50165',
                            "lng": '78.5139204',
                            "description": '2'
                        },
                        .
                        .
                        .
                        .
                        .
                        {
                            "title": 'abc',
                            "lat": '17.4166067',
                            "lng": '78.4853992',
                            "description": '15'
                        }

         ];


    var map;

    var mapOptions = {
        center: new google.maps.LatLng(markers[0].lat, markers[0].lng),
        zoom: 15    ,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var path = new google.maps.MVCArray();
    var service = new google.maps.DirectionsService();

    var infoWindow = new google.maps.InfoWindow();
    var map = new google.maps.Map(document.getElementById("map"), mapOptions);

    var poly = new google.maps.Polyline({
        map: map,
        strokeColor: '#000000'
    });

    var lat_lng = new Array();

    for (var i = 0; i <= markers.length-1; i++)
    {
           var src = new google.maps.LatLng(markers[i].lat, markers[i].lng);
           var des = new google.maps.LatLng(markers[i+1].lat, markers[i+1].lng);

            poly.setPath(path);
            service.route({
                origin: src,
                destination: des,
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            }, 
           function (result, status)
           {
                if (status == google.maps.DirectionsStatus.OK)
                {
  for (var i = 0, len = result.routes[0].overview_path.length;
       i < len; i++) 
                    {
                        path.push(result.routes[0].overview_path[i]);

                    }
                 } 
                 else{

                     if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
                     {      
                          document.getElementById('status').innerHTML +="request failed:"+status+"<br>";
                     }                                    

            } 
         });

    }
  });

RESULT MAP(OVER_QUERY_LIMIT):

OVER_QUERY_LIMIT

Prabs
  • 4,923
  • 6
  • 38
  • 59
  • I've referred it @Andreas but it didn't mentioned **How to do** It is all about **What to do** which is very difficult for me to pretend. – Prabs Apr 02 '15 at 11:35

1 Answers1

1

Google reccomends:

Lowering usage, by optimizing applications to use the web services more efficiently.

Increasing usage limits, when possible, by purchasing additional allowance for your Google Maps API for Work license.

Also HERE there is some workaround that seems to fit what you need in

if status == "OVER_QUERY_LIMIT": 
    time.sleep(2)

This is written in Phyton, but you can use it as Pseudocode, it's easily readable:

url = "MAPS_API_WEBSERVICE_URL"
attempts = 0
success = False

while success != True and attempts < 3:
  raw_result = urllib.urlopen(url).read()
  attempts += 1
  # The GetStatus function parses the answer and returns the status code
  # This function is out of the scope of this example (you can use a SDK).
  status = GetStatus(raw_result)
  if status == "OVER_QUERY_LIMIT":
    time.sleep(2)
    # retry
    continue
  success = True

if attempts == 3:
  # send an alert as this means that the daily limit has been reached
  print "Daily limit has been reached"

In your javascript code, simply set a timeout:

if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT)
{      
    setTimeout(3000);
}      

Check the working fiddle

Community
  • 1
  • 1
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • Thank you so much for your time and response @Jordi..Am trying to implement your concept in JScript..I'll tell you when am done with it – Prabs Apr 02 '15 at 12:27
  • tell me if you have some doubt... I can give you a hand with `javascript` code ;) – Jordi Castilla Apr 02 '15 at 13:26
  • If you edit your answer in jscript which can work with this [Fiddle](http://jsfiddle.net/en608jxa/) then that would be of great help.. :) – Prabs Apr 02 '15 at 13:46
  • you have the working fiddle and the relevant code lines in the update of my answer... hope that helps... – Jordi Castilla Apr 02 '15 at 14:45
  • Hello @Jordi...Thank you so much for your patience,but am sorry to say that still the is path is incomplete.As you can see the source(green-dot) and destination(red-dot) are not met .Path line stops before reaching destination – Prabs Apr 03 '15 at 04:44
  • Hello @Jordi.. Trust you are doing great..Still I didn't solved this question.. Could you be of any help please.. – Prabs Sep 11 '15 at 05:52