Skip to content Skip to sidebar Skip to footer

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' mar

Solution 1:

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=newgoogle.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:

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

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

  functionloadResults(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 = newGMaps({
      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 = newMarkerClusterer(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
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><scriptsrc="https://maps.googleapis.com/maps/api/js"></script><scriptsrc="https://rawgit.com/HPNeo/gmaps/master/gmaps.js"></script><scriptsrc="https://cdn.rawgit.com/googlemaps/v3-utility-library/master/markerclustererplus/src/markerclusterer.js"></script><inputid="mapbut"type="button"value="map" /><divid="map"></div>

Solution 2:

To get around this is relatively simple, just after I send the markers array to MarkerClusterer I then add my location.

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

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

Thanks

Post a Comment for "Google Maps Api V3: Exclude Single Marker From Clustering"