2012-04-29 28 views
7

Tôi cần lấy địa chỉ và tọa độ sau khi nhấp vào bản đồ, tôi đang sử dụng bản đồ google api v3, tôi đã phục hồi lại tọa độ ở đầu vào nhưng tôi cũng cần có điểm của điểm.nhận địa chỉ sau khi nhấp vào bản đồ

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

     } 

Tôi đã sử dụng tập lệnh php này để nhận địa chỉ nhưng nếu không biết cách sử dụng nó trong javascript? Tôi có thể sử dụng javascript để nhận địa chỉ không?

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

    ?> 

Trả lời

17

Đây là ví dụ đơn giản về truy xuất địa chỉ. Kết quả có thể trở nên phức tạp (nó là một mảng, từ những gì tôi thấy đó là sự pha trộn tùy ý của các đường giao nhau, khu phố, tiểu bang và quốc gia. Tôi không thấy một mẫu)

Tôi chỉ sử dụng dòng đầu tiên của kết quả là sự kết hợp của đường phố, thành phố, tiểu bang, quốc gia. Đọc ở đây các chi tiết:

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

Một bản demo là ở đây: http://jsfiddle.net/eB2RX/1/

tôi nhận thấy độ phân giải không phải là rất tốt, tôi có nghĩa là, ở Mỹ bạn sẽ có được số đường phố nhưng không phải ở Tunisia. Tôi chỉ thấy tên phố.

Một phần của mã:

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

nhờ một người đàn ông rất nhiều, nó làm việc;) – Houx

+0

Bạn đang chào đón! –

+0

xin vui lòng, tôi nhận được kết quả bằng tiếng Pháp, tôi cần chúng bằng tiếng Anh, bất kỳ ý tưởng. – Houx

Các vấn đề liên quan