2010-10-13 8 views

risposta

85

V'è un buon esempio su https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

Per accorciare un po ':

geocoder = new google.maps.Geocoder(); 

function codeAddress() { 

    //In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead 
    var address = document.getElementById('address').value; 

    geocoder.geocode({ 'address' : address }, function(results, status) { 
     if(status == google.maps.GeocoderStatus.OK) { 

      //In this case it creates a marker, but you can get the lat and lng from the location.LatLng 
      map.setCenter(results[0].geometry.location); 
      var marker = new google.maps.Marker({ 
       map  : map, 
       position: results[0].geometry.location 
      }); 
     } else { 
      alert('Geocode was not successful for the following reason: ' + status); 
     } 
    }); 
} 
+6

Non vogliamo che tutta la documentazione sia valida come quella di jQuery. Ho difficoltà con alcuni documenti di Google. – b729sefc

22

Non credo location.LatLng funziona, ma funziona:

results[0].geometry.location.lat(), results[0].geometry.location.lng() 

Trovato durante l'esplorazione del codice sorgente Get Lat Lon.

+0

Grazie per questo! – cpcdev

16

Se avete bisogno di fare questo sul backend è possibile utilizzare la seguente struttura URL:

https://maps.googleapis.com/maps/api/geocode/json?address=STREET_ADDRESS 

Esempio codice PHP usando ricciolo:

$curl = curl_init(); 

curl_setopt($curl, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address)); 

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); 

$json = curl_exec($curl); 

curl_close ($curl); 

visualizzare ulteriori documentation per maggiori dettagli.

I documenti forniscono output di esempio e ti aiuteranno a ottenere il tuo API key per poter effettuare richieste all'API di geocoding di Google Maps.

+0

Sai qual è il limite per questo. Voglio dire quante chiamate posso fare in un'ora o in un giorno? –

+2

I limiti di API gratuiti sono 2500 richieste per periodo di 24 ore, 5 richieste al secondo ... vedere https://developers.google.com/maps/documentation/geocoding/intro#Limits – farinspace

+1

È venuto qui a cercare questo. Gli utenti PHP potrebbero voler usare '$ arr = json_decode ($ json, true);' per accedere ai dati. –

Problemi correlati