2012-04-29 16 views
7

Ho bisogno di ottenere l'indirizzo e le coordinate dopo aver cliccato sulla mappa, sto usando google map api v3, ho recuperato le coordinate negli input ma ho anche bisogno di arddress del punto.Ottieni l'indirizzo dopo aver cliccato sulla mappa

function initialize() { 
      var myOptions = { 
       center: new google.maps.LatLng(36.835769, 10.247693), 
       zoom: 15, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      var map = new google.maps.Map(document.getElementById("map_canvas"), 
      myOptions); 
      google.maps.event.addListener(map, 'click', function(event) { 
       placeMarker(event.latLng); 
      }); 

      var marker; 
      function placeMarker(location) { 
       if(marker){ //on vérifie si le marqueur existe 
        marker.setPosition(location); //on change sa position 
       }else{ 
        marker = new google.maps.Marker({ //on créé le marqueur 
         position: location, 
         map: map 
        }); 
       } 
       latitude.value=location.lat(); 
       longitude.value=location.lng(); 
      } 

     } 

Ho usato questo script php per ottenere l'indirizzo ma se non so come usarlo in javascript? Posso usare javascript per ottenere l'indirizzo?

<?php 
    $url  = 'http://where.yahooapis.com/geocode?location=40.714224,-73.961452&gflags=R&flags=J'; 
    $response = json_decode(file_get_contents($url)); 
    $location = $response->ResultSet->Results[0]; 
    foreach ((array) $location as $key => $value) { 
      if($key == 'city') 
       $city = $value; 
      if($key == 'country') 
       $country = $value; 
      if($key == 'street') 
       $street = $value; 
    } 
    echo "Street: ".$street."<br>"; 
    echo "City: ".$city; 
    echo "<br/>Country: ".$country; 

    ?> 

risposta

17

Ecco un semplice esempio di recupero dell'indirizzo. I risultati possono essere complicati (è un array, da quello che ho visto è un mix arbitrario di strade trasversali, quartieri, stato e paese .Non ho visto uno schema)

Sto solo usando la prima riga di risultati che sono una combinazione di strada, città, stato, nazione. Leggi qui i dettagli:

https://developers.google.com/maps/documentation/javascript/geocoding#GeocodingResults

Una demo è qui: http://jsfiddle.net/eB2RX/1/

ho notato la risoluzione non è molto buona, voglio dire, negli Stati Uniti si ottiene numeri civici, ma non in Tunisia. Ho visto solo i nomi di Street.

Parte del codice:

var map; 
    var geocoder; 
    var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2, 
    mapTypeId: google.maps.MapTypeId.ROADMAP }; 

    function initialize() { 
     var myOptions = { 
      center: new google.maps.LatLng(36.835769, 10.247693), 
      zoom: 15, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

     geocoder = new google.maps.Geocoder(); 
     var map = new google.maps.Map(document.getElementById("map_canvas"), 
     myOptions); 
     google.maps.event.addListener(map, 'click', function(event) { 
      placeMarker(event.latLng); 
     }); 

     var marker; 
     function placeMarker(location) { 
      if(marker){ //on vérifie si le marqueur existe 
       marker.setPosition(location); //on change sa position 
      }else{ 
       marker = new google.maps.Marker({ //on créé le marqueur 
        position: location, 
        map: map 
       }); 
      } 
      document.getElementById('lat').value=location.lat(); 
      document.getElementById('lng').value=location.lng(); 
      getAddress(location); 
     } 

    function getAddress(latLng) { 
    geocoder.geocode({'latLng': latLng}, 
     function(results, status) { 
     if(status == google.maps.GeocoderStatus.OK) { 
      if(results[0]) { 
      document.getElementById("address").value = results[0].formatted_address; 
      } 
      else { 
      document.getElementById("address").value = "No results"; 
      } 
     } 
     else { 
      document.getElementById("address").value = status; 
     } 
     }); 
    } 
    } 
+0

grazie un uomo molto, funziona;) – Houx

+0

siete i benvenuti! –

+0

per favore, ottengo il risultato in francese, ho bisogno di loro in inglese, qualsiasi idea. – Houx

Problemi correlati