以前,谷歌地图信息窗口的自动平移功能可以避免与自定义地图控件元素重叠,就像与本机地图控件一样。但是,此功能似乎不再起作用。
我已经广泛搜索了其他遇到此问题的人。我发现在v3.32中更新了渲染器后,出现了对此行为的引用。链接here。
除此之外,似乎没有太多可用信息。我已经确认,自动平移不再适用于我以前构建过的应用程序中的自定义控件。
我还使用Google关于自定义控件的示例之一对此进行了确认。示例here。小提琴here。
var map;
var chicago = {lat: 41.85, lng: -87.65};
/**
* The CenterControl adds a control to the map that recenters the map on
* Chicago.
* @constructor
* @param {!Element} controlDiv
* @param {!google.maps.Map} map
* @param {?google.maps.LatLng} center
*/
function CenterControl(controlDiv, map, center) {
// We set up a variable for this since we're adding event listeners
// later.
var control = this;
// Set the center property upon construction
control.center_ = center;
controlDiv.style.clear = 'both';
// Set CSS for the control border
var goCenterUI = document.createElement('div');
goCenterUI.id = 'goCenterUI';
goCenterUI.title = 'Click to recenter the map';
controlDiv.appendChild(goCenterUI);
// Set CSS for the control interior
var goCenterText = document.createElement('div');
goCenterText.id = 'goCenterText';
goCenterText.innerHTML = 'Center Map';
goCenterUI.appendChild(goCenterText);
// Set CSS for the setCenter control border
var setCenterUI = document.createElement('div');
setCenterUI.id = 'setCenterUI';
setCenterUI.title = 'Click to change the center of the map';
controlDiv.appendChild(setCenterUI);
// Set CSS for the control interior
var setCenterText = document.createElement('div');
setCenterText.id = 'setCenterText';
setCenterText.innerHTML = 'Set Center';
setCenterUI.appendChild(setCenterText);
// Set up the click event listener for 'Center Map': Set the center of
// the map
// to the current center of the control.
goCenterUI.addEventListener('click', function() {
var currentCenter = control.getCenter();
map.setCenter(currentCenter);
});
// Set up the click event listener for 'Set Center': Set the center of
// the control to the current center of the map.
setCenterUI.addEventListener('click', function() {
var newCenter = map.getCenter();
control.setCenter(newCenter);
});
}
/**
* Define a property to hold the center state.
* @private
*/
CenterControl.prototype.center_ = null;
/**
* Gets the map center.
* @return {?google.maps.LatLng}
*/
CenterControl.prototype.getCenter = function() {
return this.center_;
};
/**
* Sets the map center.
* @param {?google.maps.LatLng} center
*/
CenterControl.prototype.setCenter = function(center) {
this.center_ = center;
};
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: chicago
});
// Create the DIV to hold the control and call the CenterControl()
// constructor
// passing in this DIV.
var centerControlDiv = document.createElement('div');
var centerControl = new CenterControl(centerControlDiv, map, chicago);
centerControlDiv.index = 1;
centerControlDiv.style['padding-top'] = '10px';
map.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv);
}
还有其他人遇到此问题并找到合适的解决方法吗?