5

Google Maps V3 APIを使用して、クライアント側で逆ジオコードをどのように実行しますか?アドレスからLatLngへのフォワードジオコードは単純です(以下のコード)が、逆ジオコードに対して同じことをどのように行いますか?

通常の地理コードコード:

geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    map.setCenter(results[0].geometry.location);
    var marker = new google.maps.Marker({
    map: map,
    position: results[0].geometry.location
  });
4

1 に答える 1

13

プロセスはまったく同じですが、ジオコード関数に住所オブジェクトを提供する代わりに、LatLng オブジェクトを提供するという小さな違いがあります。

逆ジオコード コード:

var input = document.getElementById("latlng").value;
var latlngStr = input.split(",",2);
var lat = parseFloat(latlngStr[0]);
var lng = parseFloat(latlngStr[1]);
var latlng = new google.maps.LatLng(lat, lng);

geocoder.geocode({'latLng': latlng}, function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) {
      map.setZoom(11);
      marker = new google.maps.Marker({
          position: latlng, 
          map: map
      }); 
      infowindow.setContent(results[1].formatted_address);
      infowindow.open(map, marker);
    } else {
      alert("No results found");
    }
  } else {
    alert("Geocoder failed due to: " + status);
  }
});

Google から直接の例

それが役立つことを願っています。

于 2011-07-02T19:04:41.977 に答える