Google Maps API V3:从群集中排除单个标记

时间:2016-01-15 16:36:59

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

我正在使用谷歌地图api并使用网格聚类作为标记。我想知道是否有办法从群集中排除单个标记。我想要一个始终可见的“你在这里”标记。我尝试使用不同的数组仅用于该标记,并且不包括它的集群功能,但这不起作用。

有人有解决方案吗?

以下是我如何进行聚类

$(document).on('click', '#mapbut', function() {

var items, distances, you_are_here = [], markers_data = [], markers_data2 = [], fred, clust1, markss;

  you_are_here.push({
      lat : Geo.lat,
      lng : Geo .lng,
      animation: google.maps.Animation.DROP,
      title : 'Your are here',
      icon: {
  path: google.maps.SymbolPath.CIRCLE,
  scale: 10
},
infoWindow: {
  content: '<p>You are Here</p>'
}
});

function loadResults (data) {

    if (data.map.length > 0) {
        items = data.map;

        for (var i = 0; i < items.length; i++)
        {
            var item = items[i];
            var distances = [];
            var dist2;

            if (item.Lat != undefined && item.Lng != undefined)
            {

                markers_data.push({
                  lat : item.Lat,
                  lng : item.Lng,
                  title : item.Site,
                        infoWindow: {
                          content: '<p>' + item.Site + '</p><p>' + Math.round(item.distance) + ' miles away</p>'
                             }
                 });

             }
        }
    }


  map.addMarkers(markers_data);

  map = new GMaps({
       el: '#map',
       lat: Geo.lat,
       lng: Geo.lng,
       zoom: 10,
       mapTypeControl: false,
       zoomControl: true,
       zoomControlOptions: {
   position: google.maps.ControlPosition.LEFT_CENTER
       },
       markerClusterer: function(map) {
      options = {
        gridSize: 50
      }

    clust1 = new MarkerClusterer(map,[], options);
    return clust1;
  },

       scaleControl: true,
       streetViewControl: false

});

map.addMarkers(you_are_here);

2 个答案:

答案 0 :(得分:3)

GMaps使用addMarker方法对您添加到其中的所有标记进行聚类(如果您提供MarkerClusterer)。

一个选项:添加您的&#34;特殊&#34;标记(您不希望群集的标记)手动添加到地图中,因此不会将其添加到MarkerClusterer

GMaps.map属性是对Google Maps Javascript API v3地图对象的引用。因此,这将为地图添加标记,而不让GMaps库知道它:

  you_are_here = new google.maps.Marker({
    position: {lat: Geo.lat,lng: Geo.lng},
    animation: google.maps.Animation.DROP,
    title: 'Your are here',
    icon: {
      path: google.maps.SymbolPath.CIRCLE,
      scale: 10
    },
    map: map.map
  });    

proof of concept fiddle

代码段

&#13;
&#13;
var Geo = {
  lat: 40.7281575,
  lng: -74.07764
};
$(document).on('click', '#mapbut', function() {

  var items, distances, you_are_here = [],
    markers_data = [],
    markers_data2 = [],
    fred, clust1, markss;

  function loadResults(data) {

    if (data.map.length > 0) {
      items = data.map;

      for (var i = 0; i < items.length; i++) {
        var item = items[i];
        var distances = [];
        var dist2;

        if (item.Lat != undefined && item.Lng != undefined) {

          markers_data.push({
            lat: item.Lat,
            lng: item.Lng,
            title: item.Site,
            infoWindow: {
              content: '<p>' + item.Site + '</p><p>' + Math.round(item.distance) + ' miles away</p>'
            }
          });

        }
      }
    }
    map = new GMaps({
      el: '#map',
      lat: Geo.lat,
      lng: Geo.lng,
      zoom: 8,
      mapTypeControl: false,
      zoomControl: true,
      zoomControlOptions: {
        position: google.maps.ControlPosition.LEFT_CENTER
      },
      markerClusterer: function(map) {
        options = {
          gridSize: 50,
          imagePath: "https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/images/m"
        }

        clust1 = new MarkerClusterer(map, [], options);
        return clust1;
      },

      scaleControl: true,
      streetViewControl: false

    });
    map.addMarkers(markers_data);

    you_are_here = new google.maps.Marker({
      position: {
        lat: Geo.lat,
        lng: Geo.lng
      },
      animation: google.maps.Animation.DROP,
      title: 'Your are here',
      icon: {
        path: google.maps.SymbolPath.CIRCLE,
        scale: 10
      },
      infoWindow: {
        content: '<p>You are Here</p>'
      },
      map: map.map
    });

    // map.addMarkers(you_are_here);
  }
  loadResults(data);
});
var data = {
  map: [{
    Lat: 40.7127837,
    Lng: -74.005941,
    Site: "New York, NY",
    distance: 1
  }, {
    Site: "Newark, NJ",
    Lat: 40.735657,
    Lng: -74.1723667,
    distance: 2
  }]
};
&#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://rawgit.com/HPNeo/gmaps/master/gmaps.js"></script>
<script src="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/src/markerclusterer.js"></script>
<input id="mapbut" type="button" value="map" />
<div id="map"></div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

为了解决这个问题相对简单,就在我将markers数组发送到MarkerClusterer后,我就添加了我的位置。

// Setup cluster markers
var markerCluster = new MarkerClusterer( gmap.map, options )

// add my location    
gmap.addMarker({
      lat: data.latitude,
      lng: data.longitude
      ...
    })

由于