谷歌地图MarkerClusterer显示数字而不是标记

时间:2017-04-28 15:09:28

标签: javascript google-maps google-maps-api-3 markerclusterer

它在特定位置显示多个标记。如何显示标记而不是数字?下面给出了代码和输出图像。请帮帮我怎样才能得到预期的结果?

注意:

4个标记具有相同的纬度和经度。我想显示4个不同的标记而不是单个标记。

JS代码:

<script>
    var map, infoBubble;

    function initialize() {
        var mapCenter = new google.maps.LatLng(52.5167, 13.3833);
        $('#user_latitude').val(52.5167);
        $('#user_longitude').val(13.3833);

        var mapOptions = {
            zoom: 3,
            center: mapCenter,
            zoomControl: true,
            zoomControlOptions: {
                position: google.maps.ControlPosition.LEFT_CENTER
            },
            streetViewControl: true,
            streetViewControlOptions: {
                position: google.maps.ControlPosition.LEFT_TOP
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            minZoom: 3,
            scrollwheel: false
        };


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


        map = new google.maps.Map(document.getElementById("map"), mapOptions);
        var markers = [];
        <?php foreach($pets as $pet):?>
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(<?php echo $pet['pet_lat']?>, <?php echo $pet['pet_long']?>),
            /*<?php //if():?>
            icon: 'http://cdn.com/my-custom-icon.png',
            <?php //else:?>
            icon: 'http://cdn.com/my-custom-icon.png',
            <?php //endif;?>*/
        });
        markers.push(marker);
        <?php endforeach;?>
        var markerCluster = new MarkerClusterer(map, markers);


    }


    google.maps.event.addDomListener(window, 'load', initialize);


</script>

enter image description here 输出

2 个答案:

答案 0 :(得分:1)

我不知道您正在使用的MarkerClusterer版本(MarkerClustererMarkerClustererPlus)但它可能是群集默认图标的网址无效了。
检查浏览器开发工具,png文件应该有404错误。

您必须初始化MarkerCluster whith选项以定义图标。

MarkerClustererPlus的示例

var options  = {
            ignoreHidden: true,
            clusterClass: cssClass,
            maxZoom: 19,
            styles: [
                {
                    height: 32,
                    width: 32,
                    textSize: 11,
                    url: "/myhost/myicon1.png"
                },
                {
                    height: 36,
                    width: 36,
                    textSize: 12,
                    url: "/myhost/myicon2.png"
                },
                {
                    height: 40,
                    width: 40,
                    textSize: 13,
                    url: "/myhost/myicon3.png"
                },
                {
                    height: 40,
                    width: 40,
                    textSize: 13,
                    url: "/myhost/myicon4.png"
                },
                {
                    height: 48,
                    width: 48,
                    textSize: 15,
                    url: "/myhost/myicon5.png"
                }
            ]
        };

cluster = new MarkerClusterer(map, [], options);

答案 1 :(得分:1)

您需要正确设置MarkerClusterer的imagePath属性(到包含群集图像的位置)。一个可能的来源是谷歌样本:

var markerCluster = new MarkerClusterer(map, markers,
   {imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});

proof of conceptr fiddle

enter image description here

代码段

&#13;
&#13;
var map, infoBubble;

function initialize() {
  var mapCenter = new google.maps.LatLng(52.5167, 13.3833);
  $('#user_latitude').val(52.5167);
  $('#user_longitude').val(13.3833);

  var mapOptions = {
    zoom: 3,
    center: mapCenter,
    zoomControl: true,
    zoomControlOptions: {
      position: google.maps.ControlPosition.LEFT_CENTER
    },
    streetViewControl: true,
    streetViewControlOptions: {
      position: google.maps.ControlPosition.LEFT_TOP
    },
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    minZoom: 3,
    scrollwheel: false
  };


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


  map = new google.maps.Map(document.getElementById("map"), mapOptions);
  var markers = [];
  //       <?php foreach($pets as $pet):?>
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(52.5167, 13.3833),
    /*<?php //if():?>
    icon: 'http://cdn.com/my-custom-icon.png',
    <?php //else:?>
    icon: 'http://cdn.com/my-custom-icon.png',
    <?php //endif;?>*/
  });
  markers.push(marker);
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(52.5167, 13.38),
    /*<?php //if():?>
    icon: 'http://cdn.com/my-custom-icon.png',
    <?php //else:?>
    icon: 'http://cdn.com/my-custom-icon.png',
    <?php //endif;?>*/
  });
  markers.push(marker);
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(52.51, 13.3833),
    /*<?php //if():?>
    icon: 'http://cdn.com/my-custom-icon.png',
    <?php //else:?>
    icon: 'http://cdn.com/my-custom-icon.png',
    <?php //endif;?>*/
  });
  markers.push(marker);
  //        <?php endforeach;?>
  var markerCluster = new MarkerClusterer(map, markers, {
    imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
  });



}


google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
&#13;
<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"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js"></script>
<div id="map"></div>
<input id="user_latitude" />
<input id="user_longitude" />
&#13;
&#13;
&#13;