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.
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}; 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}; var markers = [...]; // Create the markers you want to add and collect them into a array. var mc = new MarkerClusterer(map, markers, mcOptions);
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);
View example (simple_example.html)
With this example, you can test MarkerClusterer
with
different max zoom levels, grid size and styles, all the options.
View example (advanced_example.html)
This example will compare adding markers with
MarkerClusterer
to the normal method of adding markers, and
display the time for each.