0

Here I have a code to show me google places objects when search in boxes.

So:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
            types: ["museum"]
   };
   // alert(request.bounds);
   service.nearbySearch(request, function (results, status) {
   if (status != google.maps.places.PlacesServiceStatus.OK) {
     alert("Request["+searchIndex+"] failed: "+status);
     return;
   }
   // alert(results.length);
   document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
   for (var i = 0, result; result = results[i]; i++) {
     var marker = createMarker(result);
   }
   searchIndex++;
   if (searchIndex < boxes.length) 
     findPlaces(boxes,searchIndex);
   });
}

but if box is empty I get error: Request[i].failed ZERO_RESULT My first question is How to JUMP over this, so how go to next box and this just jump becouse there is no results

Also some time I get OVER_QUERY_LIMIT - How I can solve this problem?

UPDATE: I TRY LIKE THET TO SOLVE PROBLEM BUT AGAIN IS THE SAME:

function findPlaces(boxes,searchIndex) {
   var request = {
       bounds: boxes[searchIndex],
            types: ["museum"]
   };
   // alert(request.bounds);
   service.nearbySearch(request, function (results, status) {
   if (status != google.maps.places.PlacesServiceStatus.OK) {
//JUMP TO THE NEXT
     searchIndex++;

   }
   // alert(results.length);
   document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
   for (var i = 0, result; result = results[i]; i++) {
     var marker = createMarker(result);
   }
//WAIT 3000 TO THE NEW REQUEST
      setTimeout(function () {
    alert('hello');
  }, 3000);
   searchIndex++;
   if (searchIndex < boxes.length) 
     findPlaces(boxes,searchIndex);
   });
}
roaddev
  • 49
  • 1
  • 7
  • View this answer of **OVER_QUERY_LIMIT**: http://stackoverflow.com/questions/3529746/over-query-limit-while-using-google-maps – m.e.conroy Aug 31 '13 at 12:14
  • No, I reach queries per seconds, so I must write something to wait before call next query – roaddev Aug 31 '13 at 12:17
  • Maybe you could upon receiving an **OVER_QUERY_LIMIT** sleep/wait like `setTimeout(fn,1000)` This would wait 1 second then call the function again. – m.e.conroy Aug 31 '13 at 12:23
  • As for the other problem I'd think you'd have to make a check for it `if(status == 'ZERO_RESULT') [do something here]` – m.e.conroy Aug 31 '13 at 12:30
  • But how to go on next box with [do something here] ??? – roaddev Aug 31 '13 at 12:34
  • I UPDATE my question but again is the same... I do what you say – roaddev Aug 31 '13 at 12:47
  • You're already kind of doing it, I think you are returning too soon. `if((status == 'ZERO_RESULT') && (++searchIndex < boxes.length)){ findPlaces(boxes,searchIndex); }else{ return; }` – m.e.conroy Aug 31 '13 at 12:53
  • The problem there is that you'll be increasing `searchIndex` twice before the next `findPlaces` call if you get a status != OK. – m.e.conroy Aug 31 '13 at 13:06

1 Answers1

0
service.nearbySearch(request, function (results, status) {
    if (status != google.maps.places.PlacesServiceStatus.OK) {
        // you probably don't even need the test for 'ZERO_RESULT' just test
        // your index against the boxes array for more items to search since
        // status could be a different error
        if((status == 'ZERO_RESULT') && (++searchIndex < boxes.length)){
            findPlaces(boxes,searchIndex);
        }else{
            return;
        }
    }else{
        document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
        for (var i = 0, result; result = results[i]; i++) {
            var marker = createMarker(result);
        }
        if (++searchIndex < boxes.length) 
            findPlaces(boxes,searchIndex);
    }
});

Ok, I didn't see that you wanted to output a zero result in your sidebar, in this case you don't need to check for status OK since results will have nothing anyhow. Try this:

service.nearbySearch(request, function (results, status) {
    document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
    for (var i = 0;i<results.length; i++) {
        var marker = createMarker(results[i]);
    }
    if (++searchIndex < boxes.length) 
        setTimeout(findPlaces(boxes,searchIndex),500);
});

EDIT - Maybe this:

service.nearbySearch(request, function (results, status) {
    if(status == 'OVER_QUERY_LIMIT'){
        setTimeout(findPlaces(boxes,searchIndex),1000);
    }else{
        document.getElementById('side_bar').innerHTML += "bounds["+searchIndex+"] returns "+results.length+" results<br>"
        for (var i = 0;i<results.length; i++) {
            var marker = createMarker(results[i]);
        }
        if (++searchIndex < boxes.length) 
            setTimeout(findPlaces(boxes,searchIndex),1000);
    }
});
m.e.conroy
  • 3,508
  • 25
  • 27
  • hm, try here http://jsbin.com/EVEWOta/43 - pick distance 10mi, from Paris, to Sofia ... ? – roaddev Aug 31 '13 at 13:17
  • or just 10mi from Dnever to Oklahoma – roaddev Aug 31 '13 at 13:18
  • But again, try 10mi from Paris ti Sofia... places is only in the start and then won't to show .... also thanks very much for helping me... I update here with your annswer: http://jsbin.com/EVEWOta/43 – roaddev Aug 31 '13 at 13:24
  • Not sure if its working the way you want but when I put in Paris to Sofia I see results returning zero on the left going onto the next results set – m.e.conroy Aug 31 '13 at 13:26
  • yes, but see on map, somwhere near Sofia results stop showing – roaddev Aug 31 '13 at 13:27
  • What I need is to show results in all boxes and where is no objects to jump on the next box... – roaddev Aug 31 '13 at 13:28
  • well it seems your `findPlaces` function is working correctly, maybe you need to check your `createMarker` function and output to the console what is being passed to it after the first box – m.e.conroy Aug 31 '13 at 13:33
  • or maybe that **OVER_QUERY_LIMIT** is the culprit. Then you should check for that in the status and then don't increase the `searchIndex` but wait and make the same call again – m.e.conroy Aug 31 '13 at 13:35
  • I see the console nut there is no error... also I try now 30mi from Madrid to Sofia and I get only 4 results ??? Why that? – roaddev Aug 31 '13 at 13:36
  • maybe OVER_QUERY_LIMIT is problem becouse you stay 500 ms setTimeout ? – roaddev Aug 31 '13 at 13:41
  • Where I need to put setTimeout in your code from first example ? – roaddev Aug 31 '13 at 13:42
  • It looks to be working in JSBin with my last edit from the answer above, with the check for **OVER_QUERY_LIMIT** – m.e.conroy Aug 31 '13 at 13:53
  • The only problem with using `setTimeout` is when you have a large amount of boxes, it may take a long time for all markers to appear. the 1000 in the `setTimeout` in my example is equal to 1 second, if you have 90 boxes its going to add 90 seconds (1.5 minutes) to your script run time. You may want to experiment with setting different values here, maybe a 1/4 second = 250. – m.e.conroy Aug 31 '13 at 13:57
  • but here try 10mi from Sofia to Madrid ... the script will come to the boxes[13] and don't go to the next ... why ??? – roaddev Aug 31 '13 at 14:22