Google map V3 Set Center to specific Marker

后端 未结 6 836
深忆病人
深忆病人 2020-12-05 13:04

I am using Google Map V3 to display a list of markers pulled from a DB, using MYsql and PHP. MY markers are set on the map using the full address (from DB, including postcod

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 13:22

    To build upon @6twenty's answer...I prefer panTo(LatLng) over setCenter(LatLng) as panTo animates for smoother transition to center "if the change is less than both the width and height of the map". https://developers.google.com/maps/documentation/javascript/reference#Map

    The below uses Google Maps API v3.

    var marker = new google.maps.Marker({
        position: new google.maps.LatLng(latitude, longitude),
        title: markerTitle,
        map: map,
    });
    google.maps.event.addListener(marker, 'click', function () {
        map.panTo(marker.getPosition());
        //map.setCenter(marker.getPosition()); // sets center without animation
    });
    

提交回复
热议问题