在加载中心Google地图InfoWindow [英] Center Google Maps InfoWindow On Load

查看:85
本文介绍了在加载中心Google地图InfoWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在加载页面时遇到了一个将InfoWindow居中的问题。在加载时,地图以标记为中心,将InfoWindow关闭屏幕(我正在使用地图容器的高度很小)。

I'm having a problem centering my InfoWindow on page load. On load, the map is centering on the marker, which puts the InfoWindow off screen (I'm working with a short height for my map container).

现在单击marker会将InfoWindow中的地图重新居中,以使其看起来完全像我想要的。既然如此,我甚至试着开启marker.click触发器来实现负载解决方案,但没有运气。我缺少什么?

Now clicking the marker does re-center the map on the InfoWindow so that it looks exactly like I want. That being the case, I've even tried firing the marker.click trigger to achieve a solution on load, but had no luck. What am I missing?

JSFIDDLE: http ://jsfiddle.net/q9NTS/7/

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>


推荐答案

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

You apparently just need to wait longer before triggering the click event.

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

小提琴

这篇关于在加载中心Google地图InfoWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆