API di Google Geocoder con angolare 4

voti
0

Abbiamo una chiave API di Google Map acquistato. Nel nostro codice angolare, stiamo cercando di usare google.maps.Geocode (). Geocode che utilizza / core libreria angolare AGM fare un geocoding inverso per un elenco di latitudine / longitudine. In un secondo, abbiamo voluto inviare circa 20-30 richieste in modo da poter ottenere la risposta valida e visualizzare l'indirizzo nel nostro portale web. Ma stiamo ottenendo il seguente errore: OVER_QUERY_LIMIT per la chiamata di geocodifica api.

Ecco il frammento di codice per lo stesso:

return Observable.create((observer: Observer<object>) => {
if (geocoder) {
       new google.maps.Geocoder().geocode({ 'location': latlng }, function (results, status) {
       console.log(status);
       if (status === 'OK') {
           console.log(results[0].formatted_address);
       }
    });
}});

Abbiamo provato lo stesso con il java script e ottenere lo stesso errore. Non sono sicuro se abbiamo bisogno di inviare eventuali parametri aggiuntivi per evitare questo errore. Apprezzo se ci può guidare a risolvere la questione.

Grazie in anticipo.

È pubblicato 27/02/2018 alle 22:01
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Grazie per dare alla comunità la possibilità di dare una mano qui. Ora, si potrebbe affrontare questo problema in due modi diversi. Userei entrambi gli approcci come uno si intende per impedirti di raggiungere il vostro limite QPS, e l'altro è quello di aiutare a gestire la situazione quando si è a "quel ponte e si è pronti ad attraversarlo", così dire- .

1) Si potrebbe memorizzare nella cache tutti i risultati per quanto consentito dalle Google ToS standard.

Google Maps API Termini di servizio specifica che è possibile memorizzare nella cache temporaneamente i dati di Google Maps, per un periodo massimo di 30 giorni, per migliorare le prestazioni della vostra applicazione. Con il caching risposte del servizio web, l'applicazione può evitare l'invio di richieste di duplicati oltre brevi periodi di tempo. In realtà, le risposte di servizi web includono sempre l'intestazione HTTP Cache-Control, che indica il periodo per il quale è possibile memorizzare nella cache l'esempio risultato-per, Cache-Control: max-age pubblico = 86400. Per l'efficienza, garantire l'applicazione memorizza nella cache sempre risultati almeno per il periodo di tempo specificato in questa intestazione, ma non più del tempo massimo specificato in Google Maps API Termini di servizio.

2) Si potrebbe strozzare la tua richiesta utilizzando un timeout e / o richieste di jitter ad intervalli casuali tra le risposte come descritto in Google Docs , e un timeout JS con il codice di esempio completo qui sotto, fornita da @Andrew Leach .

// delay between geocode requests - at the time of writing, 100 miliseconds seems to work well
var delay = 100;


  // ====== Create map objects ======
  var infowindow = new google.maps.InfoWindow();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 8,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var geo = new google.maps.Geocoder(); 
  var map = new google.maps.Map(document.getElementById("map"), mapOptions);
  var bounds = new google.maps.LatLngBounds();

  // ====== Geocoding ======
  function getAddress(search, next) {
    geo.geocode({address:search}, function (results,status)
      { 
        // If that was successful
        if (status == google.maps.GeocoderStatus.OK) {
          // Lets assume that the first marker is the one we want
          var p = results[0].geometry.location;
          var lat=p.lat();
          var lng=p.lng();
          // Output the data
            var msg = 'address="' + search + '" lat=' +lat+ ' lng=' +lng+ '(delay='+delay+'ms)<br>';
            document.getElementById("messages").innerHTML += msg;
          // Create a marker
          createMarker(search,lat,lng);
        }
        // ====== Decode the error status ======
        else {
          // === if we were sending the requests to fast, try this one again and increase the delay
          if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
            nextAddress--;
            delay++;
          } else {
            var reason="Code "+status;
            var msg = 'address="' + search + '" error=' +reason+ '(delay='+delay+'ms)<br>';
            document.getElementById("messages").innerHTML += msg;
          }   
        }
        next();
      }
    );
  }

       // ======= Function to create a marker
 function createMarker(add,lat,lng) {
   var contentString = add;
   var marker = new google.maps.Marker({
     position: new google.maps.LatLng(lat,lng),
     map: map,
     zIndex: Math.round(latlng.lat()*-100000)<<5
   });

  google.maps.event.addListener(marker, 'click', function() {
     infowindow.setContent(contentString); 
     infowindow.open(map,marker);
   });

   bounds.extend(marker.position);

 }

  // ======= An array of locations that we want to Geocode ========
  var addresses = [
           '251 Pantigo Road Hampton Bays NY 11946',
           'Amagensett Quiogue NY 11978',
           '789 Main Street Hampton Bays NY 11946',
           '30 Abrahams Path Hampton Bays NY 11946',
           '3 Winnebogue Ln Westhampton NY 11977',
           '44 White Oak Lane Montauk NY 11954',
           '107 stoney hill road Bridgehampton NY 11932',
           '250 Pantigo Rd Hampton Bays NY 11946',
           '250 Pantigo Rd Hampton Bays NY 11946',
           '44 Woodruff Lane Wainscott NY 11975',
           'Address East Hampton NY 11937',
           'Address Amagansett NY 11930',
           'Address Remsenburg NY 11960 ',
           'Address Westhampton NY 11977',
           'prop address Westhampton Dunes NY 11978',
           'prop address East Hampton NY 11937',
           'Address East Hampton NY 11937',
           'Address Southampton NY 11968',
           'Address Bridgehampton NY 11932',
           'Address Sagaponack NY 11962',
            "A totally bogus address"
  ];

  // ======= Global variable to remind us what to do next
  var nextAddress = 0;

  // ======= Function to call the next Geocode operation when the reply comes back

  function theNext() {
    if (nextAddress < addresses.length) {
      setTimeout('getAddress("'+addresses[nextAddress]+'",theNext)', delay);
      nextAddress++;
    } else {
      // We're done. Show map bounds
      map.fitBounds(bounds);
    }
  }

  // ======= Call that function for the first time =======
  theNext();
Risposto il 28/02/2018 a 00:36
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more