Google Maps - center map on marker click

后端 未结 2 418
甜味超标
甜味超标 2021-01-24 20:54

I have the following code for placing several markers on a google map.

What I now want to do is when the user clicks on a marker it zooms in and then centers the map to

相关标签:
2条回答
  • 2021-01-24 21:42
    google.maps.event.addListener(marker, "click", function (evt) {
                infowindow.setContent(this.html);
                infowindow.open(map, this);
                map.setCenter(evt.latLng); 
                map.setZoom(10);
            });
    

    that will working in my own app

    0 讨论(0)
  • 2021-01-24 21:45

    marker is left pointing to the last marker added. Either use function closure or "this" like you did for the infowindow:

        google.maps.event.addListener(marker, "click", function () {
            infowindow.setContent(this.html);
            infowindow.open(map, this);
            map.setCenter(this.getPosition()); 
            map.setZoom(10);
        });
    
    0 讨论(0)
提交回复
热议问题