居中/设置地图缩放以覆盖所有可见标记? [英] Center/Set Zoom of Map to cover all visible Markers?

查看:23
本文介绍了居中/设置地图缩放以覆盖所有可见标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在地图上设置了多个标记,我可以静态设置缩放级别和中心,但我想要的是覆盖所有标记并尽可能放大所有市场都可见

可用方法如下

:

<块引用>

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

setZoom(zoom:number)

and

setCenter(latlng:LatLng)

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 to display: none, the fitBounds function reads the map's size as 0x0, and therefore does not do anything. To change the viewport while the map is hidden, set the map to visibility: hidden, thereby ensuring the map div has an actual size.

这篇关于居中/设置地图缩放以覆盖所有可见标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆