Google地图关闭infoWindow不起作用

时间:2012-06-29 16:06:03

标签: javascript php google-maps

我试图在PHP中构建一个小的Google Maps api,从数据库中获取Circles和Infowindows。 打开InfoWindow之后,除了关闭InfoWindow之外还有什么用。

Chrome Developer控制台会抛出以下错误: 获取http://maps.googleapis.com/maps/api/js/StaticMapService.GetMapImage?1m2&1i151368577549307460&2i99567197161272770&2e2&3u50&4m2&1u512&2u496&5m3&1e2&2b1&5sde-DE&token=119415 404(未找到)main.js:33< - Onload

点击红色弹出窗口,它显示以下错误: 未捕获的TypeError:对象#没有方法'O'

试图找出一个小时的解决方案,希望你能帮助我!

谢谢!

             

    <script type='text/javascript'>
        var _infoWindow = new Array();var _marker = new Array();

        function initialize()   {
            var mapOptions = {
                zoom: 50,
                center: new google.maps.LatLng(48.52039, 9.05949),
                mapTypeId: google.maps.MapTypeId.SATELLITE
            };
            var map = new google.maps.Map(document.getElementById('map'), mapOptions);


            var circles = [{"color":"fff","center":new google.maps.LatLng(48.5204, 9.05949),"radius":200}];     

            for(var circle in circles)  {
                new google.maps.Circle({
                    strokeColor: circles[circle].color,
                    strokeOpacity: 1,
                    strokeWeight: 2,
                    fillColor: circles[circle].color,
                    fillOpacity: 0.35,
                    map: map,
                    center: circles[circle].center,
                    radius: circles[circle].radius
                });
            }

            var infoWindows = [{"center":new google.maps.LatLng(48.5204, 9.05949),"content":"<h2>Juhu!<\/h2>Html<br>Content <b>erlaubt<\/b>!"}];
            var i = 0;

            for(var infoWindow in infoWindows)  {
                _infoWindow[i] = new google.maps.InfoWindow({
                    content: infoWindows[infoWindow].content
                });


                _marker[i] = new google.maps.Marker({
                    position: infoWindows[infoWindow].center,
                    map: map,
                    title:'Test'
                });


                google.maps.event.addListener(_marker[i], 'click', new Function('_infoWindow['+i+'].open(map,_marker['+i+']);'));

                i++;
            }

        }

        window.onload = function(){
            initialize();
        }
    </script>
</head>

<body>
    <div id='map' style='height:500px;width:500px;'></div>      
</body>

1 个答案:

答案 0 :(得分:1)

您正在本地范围内定义map-variable(初始化),在click-handler中无法访问它。

将其添加到脚本的顶部:

var map;

并从此行中删除var-keyword:

var map = new google.maps.Map(document.getElementById('map'), mapOptions);