2012-04-29 4 views
7

지도를 클릭 한 후 주소와 좌표를 가져와야합니다. Google map api v3을 사용하고 있습니다. 좌표 입력을 회복했으나 포인트도 필요합니다.지도를 클릭 한 후 주소를 얻으십시오

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

     } 

이 PHP 스크립트를 사용하여 주소를 가져 왔지만 자바 스크립트에서 사용하는 방법을 모르겠습니까? 자바 스크립트를 사용하여 주소를 얻을 수 있습니까?

<?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; 

    ?> 

답변

17

다음은 주소 검색의 간단한 예입니다. 결과는 복잡해질 수 있습니다 (배열입니다. 십자가, 이웃, 주 및 국가의 임의의 조합으로 보았습니다. 패턴이 보이지 않았습니다.)

나는 첫 번째 줄만 사용하고 있습니다. 결과는 거리, 도시, 주, 국가의 조합입니다. 여기에 내용을 읽어 :

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

데모는 여기에 있습니다 : http://jsfiddle.net/eB2RX/1/

나는 해상도가 매우 좋지 않아 발견, 내 말은, 미국에서 당신은 거리 번호를 얻을 수 있지만 튀니지한다. 거리 이름 만 보았습니다. 코드의

부 :

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

덕분에 많은 사람, 작동) – Houx

+0

천만에요! –

+0

제발, 나는 프랑스어로 결과를 얻는다. 나는 영어로 그것을 필요로한다, 어떤 생각. – Houx

관련 문제