bos570 bos570 - 3 months ago 37
Javascript Question

Google Maps API V3: Exclude single marker from clustering

I am using the google maps api and using grid clustering for the markers. I wanted to know if there is a way to exclude a single marker from clustering. I want a "You are here" marker that is always visible. I tried using a different array for just that marker and not including it the cluster function but that didn't work.

Does anyone have a solution for this?

Here is how i am doing the clustering

$(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);

Answer

The GMaps clusters all the markers you add to it with the addMarker method (if you provide a MarkerClusterer).

One option: add your "special" marker (the one that you don't want clustered) to the map manually, so it isn't added to the MarkerClusterer:

The GMaps.map property is a reference to the Google Maps Javascript API v3 map object. So this will add a marker to the map without letting the GMaps library know about it:

  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

code snippet:

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
  }]
};
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<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>

Comments