简体   繁体   English

google.maps.event.addListener 不是函数

[英]google.maps.event.addListener is not a function

I've been trying to add infowindows to my markers and I'm stuck since the avaible SO answers are pretty old.我一直在尝试将 infowindows 添加到我的标记中,但由于可用的 SO 答案已经很老了,所以我被困住了。 I have this code that corresponds to this answer in SO:我有这段代码对应于 SO 中的这个答案:

var map_center = new google.maps.LatLng(-0.179041, -78.499211);
        var map = new google.maps.Map(document.getElementById('mapa_ciudad'),
                mapOptions);

        marker_objects = [];

        for (i = 0; i < markers.length; i++){
            marker = new google.maps.Marker({
               position: new google.maps.LatLng(markers[i][1], markers[i][2]),
               map : map,
               title: markers[i][0]
            });

            var infowindow = new google.maps.InfoWindow();

            google.maps.event.addListener(marker, "click", function(marker){   
    // !!! PROBLEM HERE
                return function(){
                    var content = [marker.title];
                    infowindow.setContent(content);
                    infowindow.open(map, marker);
                }
            })(marker);

            marker_objects.push(marker);
        }

        function AutoCenter(){
            var bounds = new google.maps.LatLngBounds();
            $.each(marker_objects, function(index, marker){
               bounds.extend(marker.position)
            });
            map.fitBounds(bounds);
        }

        AutoCenter();

I get the error TypeError: google.maps.event.addListener(...) is not a function我收到错误TypeError: google.maps.event.addListener(...) is not a function

You have an error in the definition of the anonymous function that creates the event listener function.您在创建事件侦听器函数的匿名函数的定义中有错误。 Your code:你的代码:

google.maps.event.addListener(marker, "click", function(marker){   
// !!! PROBLEM HERE
    return function(){
        var content = [marker.title];
        infowindow.setContent(content);
        infowindow.open(map, marker);
    }
})(marker);

Should be (note the extra set of parentheses):应该是(注意额外的一组括号):

google.maps.event.addListener(marker, "click", (function(marker) {
  return function(evt) {
    var content = marker.getTitle();
    infowindow.setContent(content);
    infowindow.open(map, marker);
  }
})(marker));

working fiddle工作小提琴

code snippet:代码片段:

 function initialize() { var map_center = new google.maps.LatLng(-0.179041, -78.499211); var map = new google.maps.Map(document.getElementById('mapa_ciudad'), { zoom: 4, center: map_center }); marker_objects = []; for (i = 0; i < markers.length; i++) { marker = new google.maps.Marker({ position: new google.maps.LatLng(markers[i][1], markers[i][2]), map: map, title: markers[i][0] }); var infowindow = new google.maps.InfoWindow(); google.maps.event.addListener(marker, "click", (function(marker) { return function(evt) { var content = marker.getTitle(); infowindow.setContent(content); infowindow.open(map, marker); } })(marker)); marker_objects.push(marker); } function AutoCenter() { var bounds = new google.maps.LatLngBounds(); $.each(marker_objects, function(index, marker) { bounds.extend(marker.position) }); map.fitBounds(bounds); } AutoCenter(); } google.maps.event.addDomListener(window, "load", initialize); var markers = [ ['mark 1', 33.890542, 151.274856, 'address 1'], ['mark 2', 33.923036, 151.259052, 'address 2'], ['mark 3', 34.028249, 151.157507, 'address 3'], ['mark 4', 33.800101, 151.287478, 'address 4'], ['mark 5', 33.950198, 151.259302, 'address 5'] ];
 html, body, #mapa_ciudad { height: 100%; width: 100%; margin: 0px; padding: 0px }
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script> <div id="mapa_ciudad"></div>

Typical errors like that are connected to wrong usage of API.像这样的典型错误与 API 的错误使用有关。 The problem here is that you are mixing Google Maps version two objects with Version 3 .这里的问题是您将Google Maps 版本两个对象与版本 3混合。 Double check the manual.仔细检查手册。

For your case, if you are using V.3, please reference this example , events handlers are invoked a bit differently.对于您的情况,如果您使用的是 V.3,请参考此示例,事件处理程序的调用方式略有不同。

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

相关问题 谷歌地图:google.maps.event.addListener - Google Maps : google.maps.event.addListener 使用命名函数在循环中添加google.maps.event.addListener - Adding google.maps.event.addListener in a loop with named function google.maps.event.addListener和Fall调用 - google.maps.event.addListener and fall calls 如何在google.maps.event.addListener中使用它 - How to use this in google.maps.event.addListener 在google.maps.event.addlistener(自动完成,...)中注册“点击”事件? - Registering a 'click' event with google.maps.event.addlistener(autocomplete, …)? google.maps.event.addListener for tileloaded 突然不触发 - google.maps.event.addListener for tilesloaded suddenly not firing 我是否必须使用google.maps.event.addListener(marker,&#39;click&#39;,function(){})? - Do I have to use google.maps.event.addListener(marker, 'click', function(){})? 将指向对象的方法安全地传递给google.maps.event.addListener - Passing a pointer to objects method safely to google.maps.event.addListener google.maps.event.addListener(map,“ idle”,function()-是否可以指定空闲时间? - google.maps.event.addListener(map, “idle”, function() - Is it Possible to specify The Idle Time? 仅在google.maps.event.addListener()中显示街道地址 - show just the street address in google.maps.event.addListener()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM