Google Maps API: reverse geocoding and address printing

强颜欢笑 提交于 2019-12-02 08:20:48

updated fiddle

Call the reverse geocoder in the click listener for the marker, open the infowindow with the appended address when the callback returns successfully:

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!