2012-12-23 14 views
20

Sto utilizzando l'API di Google Maps per visualizzare circa 50 posizioni sulla mappa. Sto usando la geocodifica lato client. Sto usando window.setTimeout per controllare il numero di richieste di geocodifica che l'applicazione invia al secondo. Se invio più di una richiesta al secondo, ricevo una risposta LIMITATA SOVRAINATA.API di Google Maps OLTRE QUERY Limite al secondo limite

Domanda: Questo limite non dovrebbe essere di 10 query al secondo? Se sì, allora cosa potrei fare di sbagliato? Se no, allora Business API ha un limite di query più generose al secondo?

Si prega di notare che la nostra applicazione non colpirà le 25.000 richieste al giorno.

+0

Come stai interrogando il server di geocoding? Mostra un po 'di codice. – keune

+0

Guardate qui: http://stackoverflow.com/questions/16659398/google-maps-over-query-limit – user2403424

+0

Eventuali duplicati di [OLTRE \ _QUERY \ _LIMIT durante l'utilizzo di google maps] (http: // StackOverflow .com/questions/3529746/over-query-limit-while-using-google-maps) – xomena

risposta

25

Il geocoder dispone di limiti di quota e velocità. Dall'esperienza, puoi geocodificare ~ 10 posizioni senza superare il limite della query (il numero effettivo dipende probabilmente dal caricamento del server). La soluzione migliore è ritardare quando si verificano errori OVER_QUERY_LIMIT, quindi riprovare. Vedere questi posti simili:

+4

Grazie geocodezip. Ho provato a ritardarlo usando window.setTimeout ma ha funzionato solo con un ritardo di 1 secondo.Ora, ho modificato il codice in modo da emettere le richieste di geocodifica fino a ottenere una risposta OVER_QUERY_LIMIT. Dopo di ciò, mi fermo per 3 secondi e poi ripeto. Questa strategia sembra risolvere 50 richieste in circa 26 secondi (anziché 50 secondi). Sono tutto orecchie se c'è una strategia migliore. – user544192

4

Spesso, quando è necessario mostrare tanti punti sulla mappa, si sarebbe meglio usare il lato server approccio, questo articolo spiega quando utilizzarli:

Strategie di geocodifica: https://developers.google.com/maps/articles/geocodestrat

Il limite lato client non è esattamente "10 richieste al secondo" e, poiché non è spiegato nei documenti API, non fare affidamento sul suo comportamento.

1

Questo approccio non è corretto a causa del sovraccarico di Google Server. Per maggiori informazioni si veda https://gis.stackexchange.com/questions/15052/how-to-avoid-google-map-geocode-limit#answer-15365


A proposito, se si desidera procedere in ogni caso, qui è possibile trovare un codice che permette di caricare più marcatori ajax provenienza su google maps evitando errori OVER_QUERY_LIMIT.

Ho provato sul mio server onw e funziona!:

var lost_addresses = []; 
    geocode_count = 0; 
    resNumber = 0; 
    map = new GMaps({ 
     div: '#gmap_marker', 
     lat: 43.921493, 
     lng: 12.337646, 
    }); 

function loadMarkerTimeout(timeout) { 
    setTimeout(loadMarker, timeout) 
} 

function loadMarker() { 
    map.setZoom(6);   
    $.ajax({ 
      url: [Insert here your URL] , 
      type:'POST', 
      data: { 
       "action": "loadMarker" 
      }, 
      success:function(result){ 

       /*************************** 
       * Assuming your ajax call 
       * return something like: 
       * array(
       *  'status' => 'success', 
       *  'results'=> $resultsArray 
       * ); 
       **************************/ 

       var res=JSON.parse(result); 
       if(res.status == 'success') { 
        resNumber = res.results.length; 
        //Call the geoCoder function 
        getGeoCodeFor(map, res.results); 
       } 
      }//success 
    });//ajax 
};//loadMarker() 

$().ready(function(e) { 
    loadMarker(); 
}); 

//Geocoder function 
function getGeoCodeFor(maps, addresses) { 
     $.each(addresses, function(i,e){     
       GMaps.geocode({ 
        address: e.address, 
        callback: function(results, status) { 
          geocode_count++;   

          if (status == 'OK') {  

           //if the element is alreay in the array, remove it 
           lost_addresses = jQuery.grep(lost_addresses, function(value) { 
            return value != e; 
           }); 


           latlng = results[0].geometry.location; 
           map.addMarker({ 
             lat: latlng.lat(), 
             lng: latlng.lng(), 
             title: 'MyNewMarker', 
            });//addMarker 
          } else if (status == 'ZERO_RESULTS') { 
           //alert('Sorry, no results found'); 
          } else if(status == 'OVER_QUERY_LIMIT') { 

           //if the element is not in the losts_addresses array, add it! 
           if(jQuery.inArray(e,lost_addresses) == -1) { 
            lost_addresses.push(e); 
           } 

          } 

          if(geocode_count == addresses.length) { 
           //set counter == 0 so it wont's stop next round 
           geocode_count = 0; 

           setTimeout(function() { 
            getGeoCodeFor(maps, lost_addresses); 
           }, 2500); 
          } 
        }//callback 
       });//GeoCode 
     });//each 
};//getGeoCodeFor() 

Esempio:

map = new GMaps({ 
 
    div: '#gmap_marker', 
 
    lat: 43.921493, 
 
    lng: 12.337646, 
 
}); 
 

 
var jsonData = { 
 
    "status":"success", 
 
    "results":[ 
 
    { 
 
    "customerId":1, 
 
    "address":"Via Italia 43, Milano (MI)", 
 
    "customerName":"MyAwesomeCustomer1" 
 
    }, 
 
    { 
 
    "customerId":2, 
 
    "address":"Via Roma 10, Roma (RM)", 
 
    "customerName":"MyAwesomeCustomer2" 
 
    } 
 
    ] 
 
}; 
 
\t \t \t 
 
function loadMarkerTimeout(timeout) { 
 
    setTimeout(loadMarker, timeout) 
 
} 
 

 
function loadMarker() { \t 
 
    map.setZoom(6); 
 
    
 
    $.ajax({ 
 
    url: '/echo/html/', 
 
    type: "POST", 
 
    data: jsonData, 
 
    cache: false, 
 
    success:function(result){ 
 

 
     var res=JSON.parse(result); 
 
     if(res.status == 'success') { 
 
     resNumber = res.results.length; 
 
     //Call the geoCoder function 
 
     getGeoCodeFor(map, res.results); 
 
     } 
 
    }//success 
 
    });//ajax 
 
    
 
};//loadMarker() 
 

 
$().ready(function(e) { 
 
    loadMarker(); 
 
}); 
 

 
//Geocoder function 
 
function getGeoCodeFor(maps, addresses) { 
 
    $.each(addresses, function(i,e){ \t \t \t \t 
 
    GMaps.geocode({ 
 
     address: e.address, 
 
     callback: function(results, status) { 
 
     geocode_count++; \t \t 
 
     
 
     console.log('Id: '+e.customerId+' | Status: '+status); 
 
     
 
     if (status == 'OK') { \t \t 
 

 
      //if the element is alreay in the array, remove it 
 
      lost_addresses = jQuery.grep(lost_addresses, function(value) { 
 
      return value != e; 
 
      }); 
 

 

 
      latlng = results[0].geometry.location; 
 
      map.addMarker({ 
 
      lat: latlng.lat(), 
 
      lng: latlng.lng(), 
 
      title: e.customerName, 
 
      });//addMarker 
 
     } else if (status == 'ZERO_RESULTS') { 
 
      //alert('Sorry, no results found'); 
 
     } else if(status == 'OVER_QUERY_LIMIT') { 
 

 
      //if the element is not in the losts_addresses array, add it! 
 
      if(jQuery.inArray(e,lost_addresses) == -1) { 
 
      lost_addresses.push(e); 
 
      } 
 

 
     } 
 

 
     if(geocode_count == addresses.length) { 
 
      //set counter == 0 so it wont's stop next round 
 
      geocode_count = 0; 
 

 
      setTimeout(function() { 
 
      getGeoCodeFor(maps, lost_addresses); 
 
      }, 2500); 
 
     } 
 
     }//callback 
 
    });//GeoCode 
 
    });//each 
 
};//getGeoCodeFor()
#gmap_marker { 
 
    min-height:250px; 
 
    height:100%; 
 
    width:100%; 
 
    position: relative; 
 
    overflow: hidden; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
 
<script src="http://maps.google.com/maps/api/js" type="text/javascript"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/gmaps.js/0.4.24/gmaps.min.js" type="text/javascript"></script> 
 

 

 
<div id="gmap_marker"></div> <!-- /#gmap_marker -->

0
Here I have loaded 2200 markers. It takes around 1 min to add 2200 locations. 
<https://jsfiddle.net/suchg/qm1pqunz/11/> 


Hope it would help you. 
+0

Ehi hai qualche idea su come saltare nel caso di INVALID_REQUEST? – hyperfkcb