2016-07-01 19 views
12

Come utilizzi l'API di Google Maps v3 senza la chiave? L'ho visto in this example ma non riesco a capire che cosa lo stia causando in modo specifico.API di Google Maps senza chiave?

Modifica: nel caso in cui qualcuno lo suggerisca, le altre risposte su questo argomento su StackOverflow sono obsolete e non più applicabili.

risposta

12

Infatti il ​​link postato http://www.birdtheme.org/useful/v3largemap.html opere senza chiave (c'è solo un avvertimento nella console). Sembra che Google stia whitelist alcuni nomi di dominio per consentire l'uso dell'API senza chiave.

Ho cercato di usare la v3 API di Google Maps per visualizzare piastrelle OpenStreetMap ma produce un "errore di API di Google Maps: MissingKeyMapError" sul mio localhost mentre si sta lavorando su questo sito senza chiave API: http://harrywood.co.uk/maps/examples/google-maps/apiv3.html

Sulla maggior parte siti web che non utilizzano una chiave API, c'è un errore che impedisce l'utilizzo di Google Maps ma è possibile aggirare questa censura.

soluzione se non si possiede il server utilizzando API di Google Maps senza chiave:

Blocca la richiesta HTTP inviando l'errore con un addon come AdBlockPlus e dello Stato http://maps.googleapis.com/maps/api/js/AuthenticationService.Authenticate?*

Questo vi darà accesso alle tessere API di Google Maps, ai marker, al popup InfoWindow ... Ma se si desidera utilizzare l'API Geocoding, è necessario anche remove your HTTP referer header con un componente aggiuntivo del browser come "Modifica intestazioni".

soluzione per aggiungere nella vostra pagina web in modo che ogni visitatore può accedere a Google Maps API senza chiave:

Qui di seguito è un JavaScript incidere ho creato per utilizzare Google Maps API V3 senza chiave e bypassare il messaggio di errore.

// hack Google Maps to bypass API v3 key (needed since 22 June 2016 http://googlegeodevelopers.blogspot.com.es/2016/06/building-for-scale-updates-to-google.html) 
var target = document.head; 
var observer = new MutationObserver(function(mutations) { 
    for (var i = 0; mutations[i]; ++i) { // notify when script to hack is added in HTML head 
     if (mutations[i].addedNodes[0].nodeName == "SCRIPT" && mutations[i].addedNodes[0].src.match(/\/AuthenticationService.Authenticate?/g)) { 
      var str = mutations[i].addedNodes[0].src.match(/[?&]callback=.*[&$]/g); 
      if (str) { 
       if (str[0][str[0].length - 1] == '&') { 
        str = str[0].substring(10, str[0].length - 1); 
       } else { 
        str = str[0].substring(10); 
       } 
       var split = str.split("."); 
       var object = split[0]; 
       var method = split[1]; 
       window[object][method] = null; // remove censorship message function _xdc_._jmzdv6 (AJAX callback name "_jmzdv6" differs depending on URL) 
       //window[object] = {}; // when we removed the complete object _xdc_, Google Maps tiles did not load when we moved the map with the mouse (no problem with OpenStreetMap) 
      } 
      observer.disconnect(); 
     } 
    } 
}); 
var config = { attributes: true, childList: true, characterData: true } 
observer.observe(target, config); 

Questo vi darà accesso a Google Maps API di piastrelle, pennarelli, infowindow popup ... per l'API Geocoding, si hanno anche per rimuovere il tuo riferimento HTTP con il meta tag HTML sottostante.

<meta name="referrer" content="no-referrer"> <!-- don't send HTTP referer for privacy purpose and to use Google Maps Geocoding API without key --> 
+0

Grazie, questo funziona per me :) – user3719857

+0

Sì, funziona, ma la prima richiesta API è molto lenta (da 5 a 10 secondi), a causa del download ritardato del file QuotaService.RecordEvent di Google, quindi di qualsiasi altro richiesta funziona bene ... Qualche spiegazione? –

+0

@DrFred Ho visto che ci vuole un po 'più di tempo per ottenere la prima risposta dall'API di Geocoding. Il problema del ritardo proviene certamente dai server di Google che tentano di autenticarti con una chiave API, ma sono lieto di avere una soluzione funzionante e spero che questa soluzione possa funzionare il più a lungo possibile. Prima di questo trucco, non riuscivo nemmeno a testare l'API di Google Maps da localhost senza chiave API. Se lavorassi su un nuovo progetto, userei OpenStreetMap per mappe gratuite e per eliminare queste restrizioni. – baptx

18

A key is now required. In precedenza non era necessario e i siti che utilizzavano l'API senza chiave prima del 22 giugno 2016 sono stati grandfathering per consentire loro di continuare a lavorare.

Vedi this blog post for additional information (testo pertinente sotto):

As of June 22, 2016 we are making the following changes to the Google Maps APIs Standard Plan:

  1. We no longer support keyless access (any request that doesn't include an API key). Future product updates are only available for requests made with an API key. API keys allow us to contact developers when required and help us identify misbehaving implementations.
  2. We have implemented a simple 25,000 map loads per day free limit to new Google Maps JavaScript API, Static Maps API, and Street View Image API implementations. The confusing 90-consecutive-day grace period for these APIs is being retired on October 12, 2016. With this change, developers can predictably plan for growth while media sites and US nonprofits can request more quota at no charge via our dedicated support programs.
  3. We have reduced the daily map load maximum limit you can purchase for Google Maps JavaScript API, Static Maps API, and Street View Image API from 1,000,000 to 100,000 requests per API.* We believe higher-volume developers are best served with a Premium Plan license, which includes technical support and a Service Level Agreement, and with this change we've also created consistency between Standard Plan quotas across our maps and web service APIs.
  4. We now count Google Maps JavaScript API client-side requests towards the daily limit of the associated web service API.*

The new policies will apply immediately to all Maps API implementations created on or after June 22nd, 2016.

Existing applications have been grandfathered based on their current usage to ensure that they continue to function both now and in the future. We will also be proactively contacting all existing API key users who, based on usage growth patterns, may be impacted in the future. If you’re an existing user, please take the time to read our Policy Update for Standard Plan summary for details on how each of these changes might affect your implementation.

+1

La chiave ** non è richiesta ** ho postato un link che le funzioni senza chiave. – CoryG

+0

Non è questo il punto. Puoi prendere lo stesso codice esatto dalla pagina ed eseguirlo localmente senza errori, ergo stanno facendo qualcosa per non dover usare una chiave all'interno di quel codice. La domanda non ha nulla a che vedere con ciò che Google dice è o non è OK, ma si basa interamente su ciò che è. – CoryG

+3

L'host locale/il filesystem possono lavorare senza chiavi. – geocodezip

0
<script language="javascript" src="https://maps.google.com/maps/api/js"></script> 
    <script> 
    var target = document.head; 
    var observer = new MutationObserver(function(mutations) { 
     for (var i = 0; mutations[i]; ++i) {     // notify when script to hack is added in HTML head 
      if (mutations[i].addedNodes[0].nodeName == "SCRIPT" && mutations[i].addedNodes[0].src.match(/\/AuthenticationService.Authenticate?/g)) { 
       var str = mutations[i].addedNodes[0].src.match(/[?&]callback=.*[&$]/g); 
       if (str) { 
        if (str[0][str[0].length - 1] == '&') { 
         str = str[0].substring(10, str[0].length - 1); 
        } else { 
         str = str[0].substring(10); 
        } 
        var split = str.split("."); 
        var object = split[0]; 
        var method = split[1]; 
        window[object][method] = null; 
       } 
       observer.disconnect(); 
      } 
     } 
    }); 
    var config = { attributes: true, childList: true, characterData: true } 
    observer.observe(target, config); 
    </script> 
+0

non funziona al di fuori di una pagina locale. – CoryG

+0

usarlo nella sezione testa, ho usato nei miei diversi progetti – Anup