MarkerClusterer v3 Examples

To use a marker clusterer, create a MarkerClusterer object. In the simplest case, just pass a map to it.

var center = new google.maps.LatLng(37.4419, -122.1419);
var options = {
  'zoom': 13,
  'center': center,
  'mapTypeId': google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map"), options);
var mc = new MarkerClusterer(map);

You may also specify a number of options to fine-tune the marker manager's performance. These options are passed via a object.

The imagePath option needs to be set to the location of your local marker images.

var center = new google.maps.LatLng(37.4419, -122.1419);
var options = {
  'zoom': 13,
  'center': center,
  'mapTypeId': google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map"), options);

var mcOptions = {gridSize: 50, maxZoom: 15, imagePath: 'images/m'};
var mc = new MarkerClusterer(map, [], mcOptions);

Once you create a marker cluster, you will want to add markers to it. MarkerClusterer supports adding markers using the addMarker() and addMarkers()method or by providing a array of markers to the constructor:

var center = new google.maps.LatLng(37.4419, -122.1419);
var options = {
  'zoom': 13,
  'center': center,
  'mapTypeId': google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map"), options);

var mcOptions = {gridSize: 50, maxZoom: 15, imagePath: 'images/m'};
var markers = [...]; // Create the markers you want to add and collect them into a array.
var mc = new MarkerClusterer(map, markers, mcOptions);

A Simple MarkerClusterer Example:

This example will show 100 markers on map.

var center = new google.maps.LatLng(37.4419, -122.1419);
var options = {
  'zoom': 13,
  'center': center,
  'mapTypeId': google.maps.MapTypeId.ROADMAP
};

var map = new google.maps.Map(document.getElementById("map"), options);

var markers = [];
for (var i = 0; i < 100; i++) {
  var latLng = new google.maps.LatLng(data.photos[i].latitude,
      data.photos[i].longitude);
  var marker = new google.maps.Marker({'position': latLng});
  markers.push(marker);
}
var markerCluster = new MarkerClusterer(map, markers, {imagePath: 'images/m'});

View example (simple_example.html)

Advanced MarkerClusterer Example:

With this example, you can test MarkerClusterer with different max zoom levels, grid size and styles, all the options.

View example (advanced_example.html)

Speed Test Example

This example will compare adding markers with MarkerClusterer to the normal method of adding markers, and display the time for each.

View example (speed_test_example.html)