Google 地图 v3 在地图初始化后调整缩放级别
我正在初始化谷歌地图并定期(每 30 秒)更新地图上的标记/信息窗口。 我正在使用边界自动放大,但确保所有标记/信息窗口都可见。
我的问题是信息窗口位于地图的边缘并且经常被切断。 因此,每次更新地图时我都需要缩小“1”。 我可以使用“map.zoom”来确定当前的缩放级别并重新计算新的所需值,但我不知道如何设置这个新值。
缩放参数只能在初始化地图时设置吗?
这当然很简单吗?
以下是我的“更新”功能;
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)
}
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.
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?
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)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据API参考,您可以使用
map.setZoom(zoom)
https://developers.google.com/maps/documentation/javascript/reference/map#Map.setZoom
使用
map.getZoom()< 也更好/code> 获取缩放级别,因为
map.zoom
只会告诉您初始缩放级别,而不是当前缩放级别。According to the API reference, you can use
map.setZoom(zoom)
https://developers.google.com/maps/documentation/javascript/reference/map#Map.setZoom
It is also better to use
map.getZoom()
to get the zoom level, asmap.zoom
will only tell you the initial zoom level, not the current one.