Google Maps API:反向地理编码和地址打印

问题描述:

我创建了一个包含一系列不同位置的地图,每个位置都有一种标记和一个标题/描述。我想要做的是从lat/lng中提取相对地址。我发现this function应该做我正在寻找..但我不明白如何将其应用于我现有的代码。这里是my fiddleGoogle Maps API:反向地理编码和地址打印

我正在寻找一种方式来输出的地址在这里:

infoWindow.setContent('<h3>' + this.title + '</h3>' + data.description); 

应该像

infoWindow.setContent('<h3>' + this.title + '</h3>' + data.description + results[0].formatted_address); 
+0

你写了“我找到了这个函数”,那是什么函数? – geocodezip 2014-08-29 23:26:59

+0

ops,对不起。忘了链接。添加。 – MultiformeIngegno 2014-08-30 00:14:23

updated fiddle

呼叫在点击监听器的反向地理编码标记,当回调成功时,打开带附加地址的infowindow:

var marker = new google.maps.Marker({ 
    position: myLatlng, 
    map: map, 
    title: data.title, 
    // animation: google.maps.Animation.DROP, 
    icon: new google.maps.MarkerImage(icon) 
}); 
(function (marker, data) { 
    google.maps.event.addListener(marker, "click", function (e) { 
     geocoder.geocode({ 
      'latLng': marker.getPosition() 
     }, function (results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       infoWindow.setContent("<h3>" + marker.title + "</h3>" + data.description + "<br><b>address:</b> "+results[0].formatted_address); 
       infoWindow.open(map, marker); 
      } else { 
       infoWindow.setContent('<h3>' + marker.title + '</h3>' + data.description); 
       infoWindow.open(map, marker); 
      } 
     }); 
    }); 
})(marker, data);