简体   繁体   English

Google Maps关闭infoWindow不起作用

[英]Google Maps closing infoWindow doesn't work

i tried to built up a little Google Maps api in PHP which fetches Circles and Infowindows from a Database. 我试图用PHP构建一个小的Google Maps api,该api从数据库中获取Circles和Infowindows。 Anything works besides closing an InfoWindow after opening it. 除了在打开信息窗口后将其关闭之外,其他任何操作都没有。

The Chrome Developer console throws out the following errors: GET 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 (Not Found) main.js:33 <- Onload Chrome开发者控制台抛出以下错误:GET http://maps.googleapis.com/maps/api/js/StaticMapService.GetMapImage?1m2&1i151368577549307460&2i995671971197161272770&2e2&3u50&4m2&1u512&2u496&5m3&1e2&2b1&404s():(-)-(33

And on clicking on the Red popup it shows the following error: Uncaught TypeError: Object # has no method 'O' 在单击红色弹出窗口时,将显示以下错误:未捕获的TypeError:对象#没有方法'O'

Tried to figure out a solution for hours now, hope you can help me! 试图找出一个小时的解决方案,希望您能帮助我!

Thank you! 谢谢!

    <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>

You are defining the map-variable inside a local scope(initialize), it will not be accessible inside the click-handler. 您是在本地范围内定义map变量(初始化),在单击处理程序内将无法访问它。

Add this to the top of the scripts: 将其添加到脚本顶部:

var map;

And remove the var-keyword from this line: 并从此行中删除var-keyword:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM