居中/设置地图缩放以覆盖所有可见标记? [英] Center/Set Zoom of Map to cover all visible Markers?
问题描述
我在地图上设置了多个标记,我可以静态设置缩放级别和中心,但我想要的是覆盖所有标记并尽可能放大所有市场都可见
可用方法如下
:
<块引用>fitBounds(bounds[, padding])
参数:
`bounds`: [`LatLngBounds`][1]|[`LatLngBoundsLiteral`][1]`padding`(可选):数字|[`Padding`][1]
返回值:无
设置视口以包含给定的边界.
注意:当地图设置为display: none
时,fitBounds
函数读取地图的大小为0x0
,因此什么都不做.要在地图隐藏时更改视口,请将地图设置为 visibility: hidden
,从而确保地图 div 具有实际大小.
I am setting multiple markers on my map and I can set statically the zoom levels and the center but what I want is, to cover all the markers and zoom as much as possible having all markets visible
Available methods are following
and
Neither setCenter
supports multiple location or Location array input nor setZoom
does have this type of functionality
You need to use the fitBounds()
method.
var markers = [];//some array
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < markers.length; i++) {
bounds.extend(markers[i]);
}
map.fitBounds(bounds);
Documentation from developers.google.com/maps/documentation/javascript:
fitBounds(bounds[, padding])
Parameters:
`bounds`: [`LatLngBounds`][1]|[`LatLngBoundsLiteral`][1] `padding` (optional): number|[`Padding`][1]
Return Value: None
Sets the viewport to contain the given bounds.
Note: When the map is set todisplay: none
, thefitBounds
function reads the map's size as0x0
, and therefore does not do anything. To change the viewport while the map is hidden, set the map tovisibility: hidden
, thereby ensuring the map div has an actual size.
这篇关于居中/设置地图缩放以覆盖所有可见标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!