麻烦包括infoWindow到gmaps markerclusterer

时间:2018-02-23 10:15:09

标签: javascript google-maps google-maps-markers infowindow markerclusterer

我在将infoWindow添加到现有gmaps markerclusterer项目时遇到问题。我知道有类似的问题,但我看到的答案还没有奏效。这可能是因为我对此很新,并且缺少一些东西。

下面是没有infoWindow的代码。每个标记都是从.jason数据文件中的lat / long生成的。 “尝试前”'代码:

    <script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="data.json"></script>
    <script type="text/javascript" src="markerclusterer.js"></script>

    <script>
      function initialize() {
        var center = new google.maps.LatLng(34.777491, 64.5852620);

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: center,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var markers = [];
        for (var i = 0; i < 100; i++) {
          var dataPhoto = data.photos[i];
          var latLng = new google.maps.LatLng(dataPhoto.latitude,
              dataPhoto.longitude);
          var marker = new google.maps.Marker({
            position: latLng,
            icon: "images/gps.svg"
          });
          markers.push(marker);
        }
        var markerCluster = new MarkerClusterer(map, markers, 
        {imagePath:'images/m'});
      }
      google.maps.event.addDomListener(window, 'load', initialize);
      </script>

      <script>
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-12846745-20']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 
      'text/javascript'; ga.async = true;
        ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 
      'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; 
      s.parentNode.insertBefore(ga, s);
      })();
    </script>

  <body>
    <div id="map-container"><div id="map"></div></div>
  </body>

尝试但不工作的代码:

<script src="https://maps.googleapis.com/maps/api/js"></script>
    <script src="data.json"></script>
    <script type="text/javascript" src="markerclusterer.js"></script>

    <script>
      function initialize() {
        var center = new google.maps.LatLng(34.777491, 64.5852620);

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 3,
          center: center,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        var markers = [];
        for (var i = 0; i < 100; i++) {
          var dataPhoto = data.photos[i];
          var infoWin = new google.maps.InfoWindow();
          var latLng = new google.maps.LatLng(dataPhoto.latitude,
              dataPhoto.longitude);
          var marker = new google.maps.Marker({
            position: latLng,
            icon: "images/gps.svg"
          });
          google.maps.event.addListener(marker, 'click', function(evt) {
          infoWin.setContent('content here');
          infoWin.open(map, marker);
  })
          markers.push(marker);
        }
        var markerCluster = new MarkerClusterer(map, markers, {imagePath:'images/m'});
      }
      google.maps.event.addDomListener(window, 'load', initialize);
    </script>

    <script>
      var _gaq = _gaq || [];
      _gaq.push(['_setAccount', 'UA-12846745-20']);
      _gaq.push(['_trackPageview']);

      (function() {
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
        ga.src = ('https:' === document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
      })();
    </script>

  <body>
    <div id="map-container"><div id="map"></div></div>
  </body>

我不知道我是否遗漏了明显的东西,或者是否与点击缩放功能存在冲突。要清楚我只想在一个设备可点击时实现infoWindow,而不是集群。感谢您的帮助,我真的很感激。

更新

var data = { "count": 10785236,
 "photos": [{"photo_id": 27932, "photo_title": "Atardecer en Embalse", "photo_url": "http://www.panoramio.com/photo/27932", "photo_file_url": "http://mw2.google.com/mw-panoramio/photos/medium/27932.jpg", "longitude": -64.404945, "latitude": -32.202924, "width": 500, "height": 375, "upload_date": "25 June 2006", "owner_id": 4483, "owner_name": "Miguel Coranti", "owner_url": "http://www.panoramio.com/user/4483"}

上面显示的var数据是.json文件中的一个示例。这是许多条目之一。

1 个答案:

答案 0 :(得分:1)

enter image description here您的for loop function closure内部遗失了,因此您无法捕获信息窗对象并将其分配给相关标记,而无法单独显示其内容。

改变这个:

google.maps.event.addListener(marker, 'click', function(evt) {
      infoWin.setContent('content here');
      infoWin.open(map, marker);
})

为此:

(function(m, infoWindow, idx){
    google.maps.event.addListener(m, 'click', function(evt) {
    infoWindow.setContent('marker ' + idx);
    infoWindow.open(map, m);

        })
 })(marker, infoWin, i);

为了重现这个问题,我在JSON对象中使用了100个不同的lat / lon,就像for循环中的迭代次数一样。

Get the JSBIN with code sample here.