1

我有这个功能,可以将新标记添加到谷歌地图。

但是中心不起作用。有什么建议吗?

function addMarker(lat, lng, name){
    new google.maps.Marker({
        map: map,
        icon: image,
        position: new google.maps.LatLng(lat, lng),
        title: name,
        center: new google.maps.LatLng(lat, lng),
        zoom: 6
    });
}

center不起作用。

4

1 回答 1

5

当您通过 Google Maps API 创建新的标记对象时,您可以调用地图对象的setCenter函数,并传入标记的位置:

map.setCenter(marker.position);

这应该使地图以标记为中心。

如果要为该过程设置动画,请panTo改用:

map.panTo(marker.position);

还应注意,您尝试在构造函数中为 Marker 对象设置的centerzoom属性不存在。这些是地图对象本身的属性,需要在那里设置。

我想象这样的事情:

function addMarker(lat, lng, name)
{ 
    var newMarker = new google.maps.Marker({ map: map, 
                                             icon: image, 
                                             position: new google.maps.LatLng(lat, lng), 
                                             title: name });
    map.setCenter(newMarker.position);
    map.setZoom(6);
}

我还考虑将mapand添加image到参数列表中,addMarker而不是使用“全局”范围的对象,或者我完全放弃使用函数。这有点挑剔,取决于您的代码的需要。

更多信息在这里

于 2012-10-22T23:30:34.630 回答