谷歌地理编码中的标记群集

时间:2017-05-30 10:51:52

标签: javascript google-maps google-maps-api-3 google-geocoder google-geolocation

我正在使用谷歌地理编码根据谷歌地图上的网站名称显示标记,它工作正常。现在,我有两个相同的网站名称和图表只显示一个标记的情况。它覆盖了标记。所以,我想到集群,但是,我不知道如何在我的下面的代码中实现?。请帮助我

<script>
$(document).ready(function () {
var map;
var elevator;
var myOptions = {
    zoom: 4,
    center: new google.maps.LatLng(39.639537564366684, -97.03125),
    mapTypeId: 'terrain'
};
map = new google.maps.Map($('#map')[0], myOptions);

//info window
var infowindow = new google.maps.InfoWindow({});

var addresses = [<?php $numItems = count($val); $i=0; foreach($val as $data){ echo "['".$data['name']."','".$data['resp']."']"; if(++$i !== $numItems) {echo ",";}}?>];
//var colordot =[];
//alert(addresses);
for (var x = 0; x < addresses.length; x++) {
//alert(addresses[x][1]);
var resp = addresses[x][1];

            //alert(colordot[x]);
    $.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address=' + addresses[x][0] + '&sensor=false', null, function(resp) {
      return function(data) {
        var p = data.results[0].geometry.location;
        var address = data.results[0].formatted_address;
        var latlng = new google.maps.LatLng(p.lat, p.lng);
        if (resp >= 50) {

                 var img= 'img/red-dot.png';
            }
            else{
                var img= 'img/green-dot.png';
            }
        var marker = new google.maps.Marker({

          icon: img,

          position: latlng,
          map: map
        });


        // Adding a click event to the marker
        google.maps.event.addListener(marker, 'click', function() {
            infowindow.setContent('<div><strong>SiteName : ' + address + '</strong><br><strong>Avg. Response : ' + resp + '</strong>');
            infowindow.open(map, this);
        });

      }
    }(resp));
}

});
</script>

1 个答案:

答案 0 :(得分:0)

我认为你的意思是你有2个标记在彼此的顶部,因为它们共享完全相同的坐标,并且你想表明它们确实是单独的标记。

如果是这种情况,请使用MarkerClusterer https://developers.google.com/maps/documentation/javascript/marker-clustering