Javascript 地图初始化后,Google Maps v3 调整缩放级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5701055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps v3 adjust zoom level after map has been initialised
提问by Jono
I am initialising a google map and periodicaly (every 30 seconds) updating markers/infowindows on the map. I am using Bounds to automatically zoom in but ensure that all markers/infowindows are visible.
我正在初始化一个谷歌地图并定期(每 30 秒)更新地图上的标记/信息窗口。我正在使用 Bounds 自动放大,但确保所有标记/信息窗口都可见。
My problem is that the infowindows are placed at the edge of the map and are often cut off.
So, I need to zoom out by '1' each time the map is updated.
I can use 'map.zoom' to determine the current zoom level and recalculate the new, desired value but I don't know how to set this new value.
Can the zoom parameter only be set upon intitialising the map?
我的问题是信息窗口被放置在地图的边缘并且经常被切断。因此,每次更新地图时,我都需要缩小“1”。我可以使用 'map.zoom' 来确定当前的缩放级别并重新计算新的所需值,但我不知道如何设置这个新值。
缩放参数只能在初始化地图时设置吗?
Surely this is simple?
这当然很简单吗?
Below is my 'update' function;
下面是我的“更新”功能;
function updatePage() {
//clear current markers
clearOverlays();
var location1 = new google.maps.LatLng(team1data.lat, team1data.lon);
var location2 = new google.maps.LatLng(team2data.lat, team2data.lon);
var bounds = new google.maps.LatLngBounds();
bounds.extend(location1);
bounds.extend(location2);
map.fitBounds(bounds);
var curZoom = map.zoom;
console.log(curZoom);
var newZoom = (curZoom - 1);
console.log(newZoom);
var infowindow1 = new google.maps.InfoWindow({
content: '<div style=\"height:100px;width:230px;font-size:12px;\"><span style="font-weight:bold;">' + team1data.user + ' (' + team1data.location + '): </span>' + team1data.tweet + '</div>'
});
var infowindow2 = new google.maps.InfoWindow({
content: '<div style=\"height:100px;width:230px;font-size:12px;\"><span style="font-weight:bold;">' + team2data.user + ' (' + team2data.location + '): </span>' + team2data.tweet + '</div>'
});
//can add as many markers to the map as you like
var marker1 = new google.maps.Marker({
position: location1,
map: map,
title: "Team 1"
});
markersArray.push(marker1);
google.maps.event.addListener(marker1, 'click',
function() {
infowindow.open(map, marker);
});
var marker2 = new google.maps.Marker({
position: location2,
map: map,
title:"Team 2"
});
markersArray.push(marker2);
infowindow1.open(map, marker1);
infowindow2.open(map, marker2);
setTimeout("team1tweets()", 30000)
}
回答by tomtastico
According to the API reference, you can use map.setZoom(zoom)
根据API参考,您可以使用 map.setZoom(zoom)
https://developers.google.com/maps/documentation/javascript/reference/map#Map.setZoom
https://developers.google.com/maps/documentation/javascript/reference/map#Map.setZoom
It is also better to use map.getZoom()
to get the zoom level, as map.zoom
will only tell you the initial zoom level, not the current one.
使用map.getZoom()
获取缩放级别也更好,因为它map.zoom
只会告诉您初始缩放级别,而不是当前缩放级别。