3

我在将 InfoWindow 置于页面加载中心时遇到问题。加载时,地图以标记为中心,这使 InfoWindow 离开屏幕(我正在为我的地图容器使用较短的高度)。

现在单击标记确实会在 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>
4

1 回答 1

6

您显然只需要等待更长的时间才能触发点击事件。

// 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);
});

小提琴

于 2013-08-19T01:17:50.370 回答