我在页面加载时将我的 InfoWindow 居中时遇到问题。加载时, map 以标记为中心,这使 InfoWindow 远离屏幕(我正在为我的 map 容器设置一个较短的高度)。
现在单击标记确实使 map 在 InfoWindow 上重新居中,这样它看起来就像我想要的那样。既然如此,我什至尝试触发 marker.click 触发器来实现负载解决方案,但没有运气。我错过了什么?
JSFIDDLE:http://jsfiddle.net/q9NTS/7/
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
var marker;
var infoWindow;
var map;
function initialize() {
var mapOptions = {
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
setLocation();
}
function setLocation() {
var address = '@(Model.Event.Address)' + ', ' + '@(Model.Event.City)' + ', ' + '@(Model.Event.State)' + ' ' + '@(Model.Event.Zip)';
geocoder.geocode({ 'address': address }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var position = results[0].geometry.location;
marker = new google.maps.Marker({
map: map,
position: position,
title: '@(Model.Event.Venue)'
});
map.setCenter(marker.getPosition());
var content = 'blah';
infoWindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map, marker);
});
//infoWindow.open(map, marker); doesn't work
google.maps.event.trigger(marker, 'click'); //still doesn't work
}
else {
//
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
最佳答案
您显然只需要在触发点击事件之前等待更长时间。
// wait until the map is idle.
google.maps.event.addListenerOnce(map, 'idle', function() {
setTimeout(function() {
// wait some more (...)
google.maps.event.trigger(marker, 'click'); //still doesn't work
},2000);
});
关于javascript - 加载时将 Google map 信息窗口居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18304525/