Google geocoder API cu Angular 4

voturi
0

Avem un Google Map cheie API achiziționat. În codul nostru angular, încercăm să folosească „google.maps.Geocode (). Geocodifica“, care utilizează biblioteca unghiulară / miez AGM pentru a face o geocodare inversă pentru o listă de latitudine / longitudine. Într-un al doilea, am vrut să trimită în jurul valorii de 20-30 de cereri, astfel încât să putem obține răspuns valid și va afișa adresa în portalul nostru web. Dar noi sunt obtinerea de eroare de mai jos: „OVER_QUERY_LIMIT“ pentru apel Geocodificarea api.

Iată fragmentul de cod pentru același lucru:

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);
       }
    });
}});

Am încercat același lucru folosind java script si obtinerea aceeași eroare. Nu sunt sigur dacă avem nevoie pentru a trimite orice parametri suplimentari pentru a evita această eroare. Apreciez dacă ne ghida în rezolvarea problemei.

Mulțumesc anticipat.

Întrebat 27/02/2018 la 22:01
sursa de către utilizator
În alte limbi...                            


1 răspunsuri

voturi
0

Vă mulțumim pentru a da comunității șansa de a te ajuta aici. Acum, ai putea aborda această problemă în două moduri diferite. Mi-ar folosi ambele abordări ca unul este menit să vă păstrați de la a ajunge la limita QPS, iar celălalt este de a vă ajuta să gestionați situația când vă aflați la „acel pod și sunteți gata să-l traverseze“, asa-a-vorbi .

1) potential Ai putea cache toate rezultatele așa cum este permis de TOS standard Google.

Hărți Google Termenii și condițiile API precizează că puteți cache temporar date Hărți Google, pentru o perioadă de până la 30 de zile, pentru a îmbunătăți performanța cererii dumneavoastră. Prin caching răspunsuri de serviciu web, aplicația poate evita trimiterea de solicitări de duplicat pe perioade scurte de timp. De fapt, răspunsurile de servicii web include întotdeauna antetul HTTP Cache-Control, care indică perioada pentru care puteți cache exemplu rezultate pentru, Cache-Control: publice, max-age = 86400. Pentru eficiență, aplicația asigură cache-uri întotdeauna rezultate pentru cel puțin cantitatea de timp specificată în acest antet, dar nu mai mult decât timpul maxim specificat în Hărți Google Termenii și condițiile API.

2) Ai putea strangula solicitarea utilizând un timeout și / sau cereri de jitter la intervale aleatorii între răspunsurile așa cum este descris în Google Docs , și un JS Timeout cu codul de probă completă de mai jos, cu condiția de @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();
Publicat 28/02/2018 la 00:36
sursa de către utilizator

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