Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
599 views
in Technique[技术] by (71.8m points)

javascript - 中心/设置地图缩放以覆盖所有可见的标记?(Center/Set Zoom of Map to cover all visible Markers?)

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(setCenter既不支持多个位置或位置数组输入,也不支持setZoom具有此类功能) 在此输入图像描述   ask by Trikaldarshi translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to use the fitBounds() method.(您需要使用fitBounds()方法。)

var markers = [];//some array var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < markers.length; i++) { bounds.extend(markers[i].getPosition()); } map.fitBounds(bounds); Documentation from developers.google.com/maps/documentation/javascript :(来自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.(注意 :当map设置为display: nonefitBounds函数将地图的大小读为0x0 ,因此不执行任何操作。) To change the viewport while the map is hidden, set the map to visibility: hidden , thereby ensuring the map div has an actual size.(要在隐藏地图时更改视口,请将地图设置为visibility: hidden ,从而确保地图div具有实际大小。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...