Google Maps - center map on marker click

后端 未结 2 421
甜味超标
甜味超标 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: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);
        });
    

提交回复
热议问题