And*_*rew 1 javascript google-maps
我有这个功能,将新标记添加到谷歌地图.
但是中心不起作用.有什么建议吗?
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
});
}
Run Code Online (Sandbox Code Playgroud)
但是center
不起作用.
当您通过Google Maps API创建新的标记对象时,您可以调用地图对象的setCenter
函数,传入标记的位置:
map.setCenter(marker.position);
Run Code Online (Sandbox Code Playgroud)
这应该将地图置于标记的中心.
如果要为流程设置动画,请panTo
改为使用:
map.panTo(marker.position);
Run Code Online (Sandbox Code Playgroud)
还应注意,您尝试在Marker对象的构造函数中设置的center
和zoom
属性不存在.这些是地图对象本身的属性,需要在那里设置.
我想象这样的事情:
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);
}
Run Code Online (Sandbox Code Playgroud)
我还考虑添加map
和image
参数列表addMarker
而不是使用"全局"范围对象,或者我放弃使用函数.这有点挑剔,取决于您的代码的需要.
归档时间: |
|
查看次数: |
346 次 |
最近记录: |