简体   繁体   English

Google Maps API InfoWindows

[英]Google Maps API InfoWindows

I have a Google Map with multiple markers. 我有一个带有多个标记的Google地图。 I'm trying to show info in my InfoWindow for each marker: 我试图在我的InfoWindow中显示每个标记的信息:

function initializeMaps() {
      var latlng = new google.maps.LatLng(59.4920, 37.3010);
      var myOptions = {
          zoom: 6,
          center: latlng,
          disableDefaultUI: false,
          scrollwheel: false,
          mapTypeId: google.maps.MapTypeId.ROADMAP,
      };
      var infoWindow = new google.maps.InfoWindow();
      var latlngbounds = new google.maps.LatLngBounds();
      var map = new google.maps.Map(document.getElementById('map'),myOptions);

      var mc = new MarkerClusterer(map);
      var marker, i;
        for (i = 0; i < data.markers.length; i++) { 
              var image = "/images/markers/green-marker.png";
              marker = new google.maps.Marker({
              position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lng),
              map: map,
              title: data.markers[i].name,
              icon: image,
            });
            /*jshint loopfunc: true */
            (function (marker, data) {
            google.maps.event.addListener(marker, "click", function (e) {

                for (i=0; i<data.markers.length; i++) {
                    infoWindow.setContent("<div style = 'min-width:200px;min-height:40px'>" + '<img src="' + data.markers[i].link + '" style="height:150px;width:250px;" />' + "<br />" + data.markers[i].description + "<br />" + "<button>Read More</button>" + "</div>");
                    infoWindow.open(map, marker);
                }

            });
        })(marker, data);
        latlngbounds.extend(marker.position);

            mc.addMarker(marker);
        }
        var bounds = new google.maps.LatLngBounds();
    }

But it shows me only last element information of my array on every single marker infoWindow on the map. 但是它在地图上的每个标记infoWindow上只显示了我的数组的最后一个元素信息。 Below my JSON file: 在我的JSON文件下面:

var data = {
"markers":[
    {
        "city": "City Sample Text",
        "name": "Name Sample Text",
        "shortname": "redsquare_location",
        "description": "Descr Sample Text",
        "lat": "51.4910",
        "lng": "31.2985",
        "link": "images/infowindows/rs_rp.png",
    },
    {
        "city": "City Sample Text",
        "name": "Name Sample Text",
        "shortname": "pchurch_location",
        "description": "Descr Sample Text",
        "lat": "51.4925",
        "lng": "31.3007",
        "link": "images/infowindows/pc_rp.png",
    },
    {
        "city": "City Sample Text",
        "name": "Name Sample Text",
        "shortname": "muschool_location",
        "description": "Decr Sample Text",
        "lat": "51.4932",
        "lng": "31.3054",
        "link": "images/infowindows/ms_rp.png",
    }       
]
}

What my mistake is? 我的错是什么?

You can see only the last element information because of this 因此,您只能看到最后一个元素信息

var marker, i;
for (i = 0; i < data.markers.length; i++) { 
      var image = "/images/markers/green-marker.png";
      marker = new google.maps.Marker({
          position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lng),
          map: map,
          title: data.markers[i].name,
          icon: image,
      });

      google.maps.event.addListener(marker, "click", function (e) {
          ...
      });
}

You're trying to set event listener inside of the "for" loop. 您正在尝试在“for”循环内设置事件侦听器。 So, your case is very similar to Javascript multiple dynamic addEventListener created in for loop - passing parameters not working . 所以,你的情况非常类似于在for循环中创建的Javascript多动态addEventListener - 传递参数不起作用

This code will work if you'll use something like this: 如果您使用以下内容,此代码将起作用:

data.markers.forEach(function(marker){
    var image = "/images/markers/green-marker.png";
    var mapMarker = new google.maps.Marker({
          position: new google.maps.LatLng(marker.lat, marker.lng),
          map: map,
          title: marker.name,
          icon: image,
    });
    google.maps.event.addListener(marker, "click", function(e) {
      ...
    });
    mc.addMaker(mapMarker);
})

Duplicate of the issue in question: Google Maps JS API v3 - Simple Multiple Marker Example 问题重复: Google Maps JS API v3 - 简单多标记示例

You aren't getting function closure on the i variable in the click listener and going through the loop inside the click listener will always leave the information in the infowindow at the last entry in the data: 您不会在点击侦听器中的i变量上获得函数闭包,并且通过点击侦听器内部的循环将始终将信息保留在信息窗口中数据的最后一个条目中:

(function (marker, data) {
    google.maps.event.addListener(marker, "click", function (e) {

        for (i=0; i<data.markers.length; i++) {
            infoWindow.setContent("<div style = 'min-width:200px;min-height:40px'>" + '<img src="' + data.markers[i].link + '" style="height:150px;width:250px;" />' + "<br />" + data.markers[i].description + "<br />" + "<button>Read More</button>" + "</div>");
            infoWindow.open(map, marker);
        }

    });
})(marker, data);

Simplest solution it to get function closure on the data required to populate the infowindow, so you don't need to iterate through the whole set of input data on each marker click: 最简单的解决方案是对填充信息窗口所需的数据进行函数关闭,因此您无需在每次标记单击时都遍历整个输入数据集:

(function(marker, data) {
  google.maps.event.addListener(marker, "click", function(e) {

    infoWindow.setContent("<div style = 'min-width:200px;min-height:40px'>" + '<img src="' + data.link + '" style="height:150px;width:250px;" />' + "<br />" + data.description + "<br />" + "<button>Read More</button>" + "</div>");
    infoWindow.open(map, marker);
  });
})(marker, data.markers[i]);

proof of concept fiddle 概念证明小提琴

code snippet: 代码段:

 function initializeMaps() { var latlng = new google.maps.LatLng(59.4920, 37.3010); var myOptions = { zoom: 6, center: latlng, disableDefaultUI: false, scrollwheel: false, mapTypeId: google.maps.MapTypeId.ROADMAP, }; var infoWindow = new google.maps.InfoWindow(); var latlngbounds = new google.maps.LatLngBounds(); var map = new google.maps.Map(document.getElementById('map'), myOptions); var mc = new MarkerClusterer(map); var marker, i; for (i = 0; i < data.markers.length; i++) { var image = "http://maps.google.com/mapfiles/ms/icons/green.png"; var marker = new google.maps.Marker({ position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lng), map: map, title: data.markers[i].name, icon: image, }); /*jshint loopfunc: true */ (function(marker, data) { google.maps.event.addListener(marker, "click", function(e) { infoWindow.setContent("<div style = 'min-width:200px;min-height:40px'>" + '<img src="' + data.link + '" style="height:150px;width:250px;" />' + "<br />" + data.description + "<br />" + "<button>Read More</button>" + "</div>"); infoWindow.open(map, marker); }); })(marker, data.markers[i]); latlngbounds.extend(marker.position); mc.addMarker(marker); } // var bounds = new google.maps.LatLngBounds(); map.fitBounds(latlngbounds); } google.maps.event.addDomListener(window, "load", initializeMaps); var data = { "markers": [{ "city": "City Sample Text", "name": "Name Sample Text", "shortname": "redsquare_location", "description": "Descr Sample Text 0", "lat": "51.4910", "lng": "31.2985", "link": "images/infowindows/rs_rp.png", }, { "city": "City Sample Text", "name": "Name Sample Text", "shortname": "pchurch_location", "description": "Descr Sample Text 1", "lat": "51.4925", "lng": "31.3007", "link": "images/infowindows/pc_rp.png", }, { "city": "City Sample Text", "name": "Name Sample Text", "shortname": "muschool_location", "description": "Decr Sample Text 2", "lat": "51.4932", "lng": "31.3054", "link": "images/infowindows/ms_rp.png", }] }; 
 html, body, #map { height: 100%; width: 100%; margin: 0px; padding: 0px } 
 <script src="https://maps.googleapis.com/maps/api/js"></script> <script src="https://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclustererplus/src/markerclusterer.js"></script> <div id="map"></div> 

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

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