【发布时间】:2014-06-11 15:44:11
【问题描述】:
我正在使用 Google Maps API 在网页中嵌入地图。地图填满了整个屏幕,但屏幕左侧有大约 400 像素的内容,大部分覆盖了地图。当我想平移地图时,左侧的区域应该被视为不可见。
我想出了以下代码来计算地图的“可用部分”,我希望距离地图边缘 50 像素,并且还应该避开地图左侧的 400 像素区域:
panIfNotClose = function(latLngs){
if(latLngs.length === 0)
return;
// Get some important values that may not be available yet
// http://stackoverflow.com/a/2833015/802335
var bounds = map.getBounds();
if(!bounds)
return setTimeout(panIfNotClose.bind(this, latLngs), 10);
var overlay = new google.maps.OverlayView();
overlay.draw = function(){};
overlay.setMap(map);
var proj = overlay.getProjection();
if(!proj)
return setTimeout(panIfNotClose.bind(this, latLngs), 10);
// Calculate the "usable part" of the map
var center = bounds.getCenter();
var northEast = bounds.getNorthEast();
var southWest = bounds.getSouthWest();
var northEastPx = proj.fromLatLngToContainerPixel(northEast);
var southWestPx = proj.fromLatLngToContainerPixel(southWest);
var menuPadding = 400;
var newNorthEastPx = new google.maps.Point(northEastPx.x + 50, northEastPx.y + 50);
var newSouthWestPx = new google.maps.Point(southWestPx.x - (50 + menuPadding), southWestPx.y - 50);
var newNorthEast = proj.fromContainerPixelToLatLng(newNorthEastPx);
var newSouthWest = proj.fromContainerPixelToLatLng(newSouthWestPx);
var centerBounds = new google.maps.LatLngBounds(newSouthWest, newSouthWest);
// Decide if any of the new LatLngs are far enough away that the map should move
var shouldMove = false;
var targetBounds = new google.maps.LatLngBounds();
for(var i = 0; i < latLngs.length; i++){
targetBounds.extend(latLngs[i]);
if(!centerBounds.contains(latLngs[i]))
shouldMove = true;
}
// If the LatLngs aren't all near the center of the map, pan to it
if(latLngs.length === 1){
if(shouldMove || map.getZoom() !== 18)
map.panTo(latLngs[0]);
map.setZoom(18);
}else{
var targetZoom = Math.min(getBoundsZoomLevel(targetBounds), 18);
if(shouldMove || map.getZoom() !== targetZoom)
map.panTo(targetBounds.getCenter());
map.setZoom(targetZoom);
}
}
此代码应测试“有效”区域以确保所有给定的 LatLngs 都适合其中,但它尚未对 panTo 进行任何更改以将中心“移动”到帐户右侧 200 像素对于左侧 400px 的内容。
代码没有按我的预期工作,但我不知道为什么。我怀疑从LatLng 转换为Point 时我可能做错了什么,反之亦然。我也可能做的工作比必要的多得多,尽管我想不出一种方法来简化它。
【问题讨论】:
-
V3中没有函数
getBoundsZoomLevel -
我实际上从我的代码中其他地方定义的链接答案中获得了确切的功能(注意它没有以
map.为前缀)。我没有包括它,因为它与我的问题并不真正相关。
标签: javascript google-maps-api-3