Google Maps API: reverse geocoding and address printing

后端 未结 1 532
Happy的楠姐
Happy的楠姐 2021-01-28 10:08

I created a map with a bunch of different locations, each with a type of marker and a title/description. What I\'d like to do is extract from lat/lng the relative address. I fou

1条回答
  •  北海茫月
    2021-01-28 10:41

    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("

    " + marker.title + "

    " + data.description + "
    address: "+results[0].formatted_address); infoWindow.open(map, marker); } else { infoWindow.setContent('

    ' + marker.title + '

    ' + data.description); infoWindow.open(map, marker); } }); }); })(marker, data);

    0 讨论(0)
提交回复
热议问题