本文介绍了尝试在多个标记上显示来自JSON的InfoWindow数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个标记从JSON文件加载到Google Maps v3 api中。但是,它只将最后一个JSON条目加载到最后一个标记的地图infoWindow中。其他标记点击时不会显示任何信息窗口。



我找到了下面的相关文章,但仍然看不到这个工作。





和尝试了解关闭


I have multiple markers loading from a JSON file into google maps v3 api. However it is only loading the last JSON entry into the map infoWindow on the last marker. The other markers do not show any info window when clicked.

I have found the below related posts but still cannot seem to get this to work.

..3979486/infowindow-help-on-google-maps-api-3

..2357323/trying-to-bind-multiple-infowindows-to-multiple-markers-on-a-google-map-and-faili

I am guessing it has something to do with location of the addListener?

Here is what I got:

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript" src="markerclusterer.js"></script>
<script src="jquery-1.5.1.min.js" type="text/javascript"></script>


<script type="text/javascript">
function initialize() {
  var myLatlng = new google.maps.LatLng(39.632906,-106.524591);
  var myOptions = {
     zoom: 8,
     center: myLatlng,
     mapTypeControl: true,
     mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
     navigationControl: true,
     navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
     mapTypeId: google.maps.MapTypeId.ROADMAP
     };

  var map = new google.maps.Map(document.getElementById("mapdiv"), myOptions);

    var url = "eventdata.json";
      $.getJSON(url,
        function(data){
              $.each(data.markers, function(i,markers){

                  var mylati = data.markers[i].lati;
                  var mylong = data.markers[i].long;
                  var mytitle = data.markers[i].title;
                  var myaddress = data.markers[i].address;
                  var myhtml = data.markers[i].html;

    var mycontent = "<div class='infowin'><h3>" + mytitle + "</h3>" + "<h4>"
    + myaddress + "</h4>" + myhtml + "</div>";

    var myLatlng = new google.maps.LatLng(parseFloat(mylati),parseFloat(mylong));



                <!--Load Event Markers-->
                    var markers = [];
                    for (var i = 0, dataMarkers; dataMarkers = data.markers[i]; i++) {
                      var latLng = new google.maps.LatLng(dataMarkers.lat, dataMarkers.lng);
                      var theTitle = dataMarkers.title;
                      var marker = new google.maps.Marker({
                        position: latLng,
                        clickable: true,
                        title: theTitle,
                      });
                      markers.push(marker);
                    }

                        var markerClusterer = new MarkerClusterer(map, markers, {
                          maxZoom: 15,
                          gridSize: 30,
                        });

                    var infowindow = new google.maps.InfoWindow({
                        content: mycontent
                    });

        google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map,marker);
        });




       });
    });

    }

</script>

Any help is greatly appreciated

解决方案

Please take a look at this and try to understand closures

这篇关于尝试在多个标记上显示来自JSON的InfoWindow数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:22