diff --git a/README.md b/README.md index 5faf0a1..ed4126d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ -Marker Clusterer – A Google Maps JavaScript API utility library -============== +### PREAMBLE: + +> This repository is a clone of the original [googlemaps/js-marker-clusterer](https://github.com/googlemaps/js-marker-clusterer). Since Google [doesn't maintain](https://github.com/googlemaps/js-marker-clusterer/commit/dc326b9b9234ce964861eb35b416a6be1ca7d7cf) it's own repository anymore, this repository has the mission to keep this useful tool alive. More Information: https://github.com/googlemaps/js-marker-clusterer/issues/98._ + +# Marker Clusterer – A Google Maps JavaScript API utility library A Google Maps JavaScript API v3 library to create and manage per-zoom-level clusters for large amounts of markers. ![Analytics](https://ga-beacon.appspot.com/UA-12846745-20/js-marker-clusterer/readme?pixel) diff --git a/src/markerclusterer.js b/src/markerclusterer.js index 445cd1b..825a126 100755 --- a/src/markerclusterer.js +++ b/src/markerclusterer.js @@ -55,128 +55,136 @@ * 'anchor': (Array) The anchor position of the label text. * 'textColor': (string) The text color. * 'textSize': (number) The text size. - * 'backgroundPosition': (string) The position of the backgound x, y. + * 'backgroundPosition': (string) The position of the background x, y. + * 'cssClass': (string) One or more CSS class for styling this marker. * @constructor * @extends google.maps.OverlayView */ function MarkerClusterer(map, opt_markers, opt_options) { - // MarkerClusterer implements google.maps.OverlayView interface. We use the - // extend function to extend MarkerClusterer with google.maps.OverlayView - // because it might not always be available when the code is defined so we - // look for it at the last possible moment. If it doesn't exist now then - // there is no point going ahead :) - this.extend(MarkerClusterer, google.maps.OverlayView); - this.map_ = map; - - /** - * @type {Array.} - * @private - */ - this.markers_ = []; - - /** - * @type {Array.} - */ - this.clusters_ = []; - - this.sizes = [53, 56, 66, 78, 90]; - - /** - * @private - */ - this.styles_ = []; - - /** - * @type {boolean} - * @private - */ - this.ready_ = false; - - var options = opt_options || {}; - - /** - * @type {number} - * @private - */ - this.gridSize_ = options['gridSize'] || 60; - - /** - * @private - */ - this.minClusterSize_ = options['minimumClusterSize'] || 2; - - - /** - * @type {?number} - * @private - */ - this.maxZoom_ = options['maxZoom'] || null; - - this.styles_ = options['styles'] || []; - - /** - * @type {string} - * @private - */ - this.imagePath_ = options['imagePath'] || - this.MARKER_CLUSTER_IMAGE_PATH_; - - /** - * @type {string} - * @private - */ - this.imageExtension_ = options['imageExtension'] || - this.MARKER_CLUSTER_IMAGE_EXTENSION_; - - /** - * @type {boolean} - * @private - */ - this.zoomOnClick_ = true; - - if (options['zoomOnClick'] != undefined) { - this.zoomOnClick_ = options['zoomOnClick']; - } - - /** - * @type {boolean} - * @private - */ - this.averageCenter_ = false; - - if (options['averageCenter'] != undefined) { - this.averageCenter_ = options['averageCenter']; - } - - this.setupStyles_(); - - this.setMap(map); - - /** - * @type {number} - * @private - */ - this.prevZoom_ = this.map_.getZoom(); - - // Add the map event listeners - var that = this; - google.maps.event.addListener(this.map_, 'zoom_changed', function() { - var zoom = that.map_.getZoom(); - - if (that.prevZoom_ != zoom) { - that.prevZoom_ = zoom; - that.resetViewport(); + // MarkerClusterer implements google.maps.OverlayView interface. We use the + // extend function to extend MarkerClusterer with google.maps.OverlayView + // because it might not always be available when the code is defined so we + // look for it at the last possible moment. If it doesn't exist now then + // there is no point going ahead :) + this.extend(MarkerClusterer, google.maps.OverlayView); + this.map_ = map; + + /** + * @type {Array.} + * @private + */ + this.markers_ = []; + + /** + * @type {Array.} + */ + this.clusters_ = []; + + this.sizes = [53, 56, 66, 78, 90]; + + /** + * @private + */ + this.styles_ = []; + + /** + * @private + */ + this.cssClass_ = ''; + + /** + * @type {boolean} + * @private + */ + this.ready_ = false; + + var options = opt_options || {}; + + /** + * @type {number} + * @private + */ + this.gridSize_ = options['gridSize'] || 60; + + /** + * @private + */ + this.minClusterSize_ = options['minimumClusterSize'] || 2; + + + /** + * @type {?number} + * @private + */ + this.maxZoom_ = options['maxZoom'] || null; + + this.styles_ = options['styles'] || []; + + this.cssClass_ = options['cssClass'] || null; + + /** + * @type {string} + * @private + */ + this.imagePath_ = options['imagePath'] || + this.MARKER_CLUSTER_IMAGE_PATH_; + + /** + * @type {string} + * @private + */ + this.imageExtension_ = options['imageExtension'] || + this.MARKER_CLUSTER_IMAGE_EXTENSION_; + + /** + * @type {boolean} + * @private + */ + this.zoomOnClick_ = true; + + if (options['zoomOnClick'] != undefined) { + this.zoomOnClick_ = options['zoomOnClick']; } - }); - google.maps.event.addListener(this.map_, 'idle', function() { - that.redraw(); - }); + /** + * @type {boolean} + * @private + */ + this.averageCenter_ = false; - // Finally, add the markers - if (opt_markers && opt_markers.length) { - this.addMarkers(opt_markers, false); - } + if (options['averageCenter'] != undefined) { + this.averageCenter_ = options['averageCenter']; + } + + this.setupStyles_(); + + this.setMap(map); + + /** + * @type {number} + * @private + */ + this.prevZoom_ = this.map_.getZoom(); + + // Add the map event listeners + var that = this; + google.maps.event.addListener(this.map_, 'zoom_changed', function() { + var zoom = that.map_.getZoom(); + + if (that.prevZoom_ != zoom) { + that.prevZoom_ = zoom; + that.resetViewport(); + } + }); + + google.maps.event.addListener(this.map_, 'idle', function() { + that.redraw(); + }); + + // Finally, add the markers + if (opt_markers && opt_markers.length) { + this.addMarkers(opt_markers, false); + } } @@ -209,12 +217,12 @@ MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_EXTENSION_ = 'png'; * @ignore */ MarkerClusterer.prototype.extend = function(obj1, obj2) { - return (function(object) { - for (var property in object.prototype) { - this.prototype[property] = object.prototype[property]; - } - return this; - }).apply(obj1, [obj2]); + return (function(object) { + for (var property in object.prototype) { + this.prototype[property] = object.prototype[property]; + } + return this; + }).apply(obj1, [obj2]); }; @@ -223,7 +231,7 @@ MarkerClusterer.prototype.extend = function(obj1, obj2) { * @ignore */ MarkerClusterer.prototype.onAdd = function() { - this.setReady_(true); + this.setReady_(true); }; /** @@ -238,30 +246,30 @@ MarkerClusterer.prototype.draw = function() {}; * @private */ MarkerClusterer.prototype.setupStyles_ = function() { - if (this.styles_.length) { - return; - } - - for (var i = 0, size; size = this.sizes[i]; i++) { - this.styles_.push({ - url: this.imagePath_ + (i + 1) + '.' + this.imageExtension_, - height: size, - width: size - }); - } + if (this.styles_.length) { + return; + } + + for (var i = 0, size; size = this.sizes[i]; i++) { + this.styles_.push({ + url: this.imagePath_ + (i + 1) + '.' + this.imageExtension_, + height: size, + width: size + }); + } }; /** * Fit the map to the bounds of the markers in the clusterer. */ MarkerClusterer.prototype.fitMapToMarkers = function() { - var markers = this.getMarkers(); - var bounds = new google.maps.LatLngBounds(); - for (var i = 0, marker; marker = markers[i]; i++) { - bounds.extend(marker.getPosition()); - } + var markers = this.getMarkers(); + var bounds = new google.maps.LatLngBounds(); + for (var i = 0, marker; marker = markers[i]; i++) { + bounds.extend(marker.getPosition()); + } - this.map_.fitBounds(bounds); + this.map_.fitBounds(bounds); }; @@ -271,7 +279,7 @@ MarkerClusterer.prototype.fitMapToMarkers = function() { * @param {Object} styles The style to set. */ MarkerClusterer.prototype.setStyles = function(styles) { - this.styles_ = styles; + this.styles_ = styles; }; @@ -281,7 +289,7 @@ MarkerClusterer.prototype.setStyles = function(styles) { * @return {Object} The styles object. */ MarkerClusterer.prototype.getStyles = function() { - return this.styles_; + return this.styles_; }; @@ -291,7 +299,7 @@ MarkerClusterer.prototype.getStyles = function() { * @return {boolean} True if zoomOnClick_ is set. */ MarkerClusterer.prototype.isZoomOnClick = function() { - return this.zoomOnClick_; + return this.zoomOnClick_; }; /** @@ -300,7 +308,7 @@ MarkerClusterer.prototype.isZoomOnClick = function() { * @return {boolean} True if averageCenter_ is set. */ MarkerClusterer.prototype.isAverageCenter = function() { - return this.averageCenter_; + return this.averageCenter_; }; @@ -310,7 +318,7 @@ MarkerClusterer.prototype.isAverageCenter = function() { * @return {Array.} The markers. */ MarkerClusterer.prototype.getMarkers = function() { - return this.markers_; + return this.markers_; }; @@ -320,7 +328,7 @@ MarkerClusterer.prototype.getMarkers = function() { * @return {Number} The number of markers. */ MarkerClusterer.prototype.getTotalMarkers = function() { - return this.markers_.length; + return this.markers_.length; }; @@ -330,7 +338,7 @@ MarkerClusterer.prototype.getTotalMarkers = function() { * @param {number} maxZoom The max zoom level. */ MarkerClusterer.prototype.setMaxZoom = function(maxZoom) { - this.maxZoom_ = maxZoom; + this.maxZoom_ = maxZoom; }; @@ -340,7 +348,7 @@ MarkerClusterer.prototype.setMaxZoom = function(maxZoom) { * @return {number} The max zoom level. */ MarkerClusterer.prototype.getMaxZoom = function() { - return this.maxZoom_; + return this.maxZoom_; }; @@ -353,19 +361,19 @@ MarkerClusterer.prototype.getMaxZoom = function() { * @private */ MarkerClusterer.prototype.calculator_ = function(markers, numStyles) { - var index = 0; - var count = markers.length; - var dv = count; - while (dv !== 0) { - dv = parseInt(dv / 10, 10); - index++; - } - - index = Math.min(index, numStyles); - return { - text: count, - index: index - }; + var index = 0; + var count = markers.length; + var dv = count; + while (dv !== 0) { + dv = parseInt(dv / 10, 10); + index++; + } + + index = Math.min(index, numStyles); + return { + text: count, + index: index + }; }; @@ -378,7 +386,7 @@ MarkerClusterer.prototype.calculator_ = function(markers, numStyles) { * */ MarkerClusterer.prototype.setCalculator = function(calculator) { - this.calculator_ = calculator; + this.calculator_ = calculator; }; @@ -388,7 +396,7 @@ MarkerClusterer.prototype.setCalculator = function(calculator) { * @return {function(Array, number)} the calculator function. */ MarkerClusterer.prototype.getCalculator = function() { - return this.calculator_; + return this.calculator_; }; @@ -399,12 +407,12 @@ MarkerClusterer.prototype.getCalculator = function() { * @param {boolean=} opt_nodraw Whether to redraw the clusters. */ MarkerClusterer.prototype.addMarkers = function(markers, opt_nodraw) { - for (var i = 0, marker; marker = markers[i]; i++) { - this.pushMarkerTo_(marker); - } - if (!opt_nodraw) { - this.redraw(); - } + for (var i = 0, marker; marker = markers[i]; i++) { + this.pushMarkerTo_(marker); + } + if (!opt_nodraw) { + this.redraw(); + } }; @@ -415,17 +423,17 @@ MarkerClusterer.prototype.addMarkers = function(markers, opt_nodraw) { * @private */ MarkerClusterer.prototype.pushMarkerTo_ = function(marker) { - marker.isAdded = false; - if (marker['draggable']) { - // If the marker is draggable add a listener so we update the clusters on - // the drag end. - var that = this; - google.maps.event.addListener(marker, 'dragend', function() { - marker.isAdded = false; - that.repaint(); - }); - } - this.markers_.push(marker); + marker.isAdded = false; + if (marker['draggable']) { + // If the marker is draggable add a listener so we update the clusters on + // the drag end. + var that = this; + google.maps.event.addListener(marker, 'dragend', function() { + marker.isAdded = false; + that.repaint(); + }); + } + this.markers_.push(marker); }; @@ -436,10 +444,10 @@ MarkerClusterer.prototype.pushMarkerTo_ = function(marker) { * @param {boolean=} opt_nodraw Whether to redraw the clusters. */ MarkerClusterer.prototype.addMarker = function(marker, opt_nodraw) { - this.pushMarkerTo_(marker); - if (!opt_nodraw) { - this.redraw(); - } + this.pushMarkerTo_(marker); + if (!opt_nodraw) { + this.redraw(); + } }; @@ -451,28 +459,28 @@ MarkerClusterer.prototype.addMarker = function(marker, opt_nodraw) { * @private */ MarkerClusterer.prototype.removeMarker_ = function(marker) { - var index = -1; - if (this.markers_.indexOf) { - index = this.markers_.indexOf(marker); - } else { - for (var i = 0, m; m = this.markers_[i]; i++) { - if (m == marker) { - index = i; - break; - } + var index = -1; + if (this.markers_.indexOf) { + index = this.markers_.indexOf(marker); + } else { + for (var i = 0, m; m = this.markers_[i]; i++) { + if (m == marker) { + index = i; + break; + } + } } - } - if (index == -1) { - // Marker is not in our list of markers. - return false; - } + if (index == -1) { + // Marker is not in our list of markers. + return false; + } - marker.setMap(null); + marker.setMap(null); - this.markers_.splice(index, 1); + this.markers_.splice(index, 1); - return true; + return true; }; @@ -484,15 +492,15 @@ MarkerClusterer.prototype.removeMarker_ = function(marker) { * @return {boolean} True if the marker was removed. */ MarkerClusterer.prototype.removeMarker = function(marker, opt_nodraw) { - var removed = this.removeMarker_(marker); + var removed = this.removeMarker_(marker); - if (!opt_nodraw && removed) { - this.resetViewport(); - this.redraw(); - return true; - } else { - return false; - } + if (!opt_nodraw && removed) { + this.resetViewport(); + this.redraw(); + return true; + } else { + return false; + } }; @@ -503,18 +511,18 @@ MarkerClusterer.prototype.removeMarker = function(marker, opt_nodraw) { * @param {boolean=} opt_nodraw Optional boolean to force no redraw. */ MarkerClusterer.prototype.removeMarkers = function(markers, opt_nodraw) { - var removed = false; + var removed = false; - for (var i = 0, marker; marker = markers[i]; i++) { - var r = this.removeMarker_(marker); - removed = removed || r; - } + for (var i = 0, marker; marker = markers[i]; i++) { + var r = this.removeMarker_(marker); + removed = removed || r; + } - if (!opt_nodraw && removed) { - this.resetViewport(); - this.redraw(); - return true; - } + if (!opt_nodraw && removed) { + this.resetViewport(); + this.redraw(); + return true; + } }; @@ -525,10 +533,10 @@ MarkerClusterer.prototype.removeMarkers = function(markers, opt_nodraw) { * @private */ MarkerClusterer.prototype.setReady_ = function(ready) { - if (!this.ready_) { - this.ready_ = ready; - this.createClusters_(); - } + if (!this.ready_) { + this.ready_ = ready; + this.createClusters_(); + } }; @@ -538,7 +546,7 @@ MarkerClusterer.prototype.setReady_ = function(ready) { * @return {number} The number of clusters. */ MarkerClusterer.prototype.getTotalClusters = function() { - return this.clusters_.length; + return this.clusters_.length; }; @@ -548,7 +556,7 @@ MarkerClusterer.prototype.getTotalClusters = function() { * @return {google.maps.Map} The map. */ MarkerClusterer.prototype.getMap = function() { - return this.map_; + return this.map_; }; @@ -558,7 +566,7 @@ MarkerClusterer.prototype.getMap = function() { * @param {google.maps.Map} map The map. */ MarkerClusterer.prototype.setMap = function(map) { - this.map_ = map; + this.map_ = map; }; @@ -568,7 +576,7 @@ MarkerClusterer.prototype.setMap = function(map) { * @return {number} The grid size. */ MarkerClusterer.prototype.getGridSize = function() { - return this.gridSize_; + return this.gridSize_; }; @@ -578,7 +586,7 @@ MarkerClusterer.prototype.getGridSize = function() { * @param {number} size The grid size. */ MarkerClusterer.prototype.setGridSize = function(size) { - this.gridSize_ = size; + this.gridSize_ = size; }; @@ -588,7 +596,7 @@ MarkerClusterer.prototype.setGridSize = function(size) { * @return {number} The grid size. */ MarkerClusterer.prototype.getMinClusterSize = function() { - return this.minClusterSize_; + return this.minClusterSize_; }; /** @@ -597,7 +605,7 @@ MarkerClusterer.prototype.getMinClusterSize = function() { * @param {number} size The grid size. */ MarkerClusterer.prototype.setMinClusterSize = function(size) { - this.minClusterSize_ = size; + this.minClusterSize_ = size; }; @@ -608,32 +616,32 @@ MarkerClusterer.prototype.setMinClusterSize = function(size) { * @return {google.maps.LatLngBounds} The extended bounds. */ MarkerClusterer.prototype.getExtendedBounds = function(bounds) { - var projection = this.getProjection(); + var projection = this.getProjection(); - // Turn the bounds into latlng. - var tr = new google.maps.LatLng(bounds.getNorthEast().lat(), - bounds.getNorthEast().lng()); - var bl = new google.maps.LatLng(bounds.getSouthWest().lat(), - bounds.getSouthWest().lng()); + // Turn the bounds into latlng. + var tr = new google.maps.LatLng(bounds.getNorthEast().lat(), + bounds.getNorthEast().lng()); + var bl = new google.maps.LatLng(bounds.getSouthWest().lat(), + bounds.getSouthWest().lng()); - // Convert the points to pixels and the extend out by the grid size. - var trPix = projection.fromLatLngToDivPixel(tr); - trPix.x += this.gridSize_; - trPix.y -= this.gridSize_; + // Convert the points to pixels and the extend out by the grid size. + var trPix = projection.fromLatLngToDivPixel(tr); + trPix.x += this.gridSize_; + trPix.y -= this.gridSize_; - var blPix = projection.fromLatLngToDivPixel(bl); - blPix.x -= this.gridSize_; - blPix.y += this.gridSize_; + var blPix = projection.fromLatLngToDivPixel(bl); + blPix.x -= this.gridSize_; + blPix.y += this.gridSize_; - // Convert the pixel points back to LatLng - var ne = projection.fromDivPixelToLatLng(trPix); - var sw = projection.fromDivPixelToLatLng(blPix); + // Convert the pixel points back to LatLng + var ne = projection.fromDivPixelToLatLng(trPix); + var sw = projection.fromDivPixelToLatLng(blPix); - // Extend the bounds to contain the new bounds. - bounds.extend(ne); - bounds.extend(sw); + // Extend the bounds to contain the new bounds. + bounds.extend(ne); + bounds.extend(sw); - return bounds; + return bounds; }; @@ -646,7 +654,7 @@ MarkerClusterer.prototype.getExtendedBounds = function(bounds) { * @private */ MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) { - return bounds.contains(marker.getPosition()); + return bounds.contains(marker.getPosition()); }; @@ -654,10 +662,10 @@ MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) { * Clears all clusters and markers from the clusterer. */ MarkerClusterer.prototype.clearMarkers = function() { - this.resetViewport(true); + this.resetViewport(true); - // Set the markers a empty array. - this.markers_ = []; + // Set the markers a empty array. + this.markers_ = []; }; @@ -666,38 +674,38 @@ MarkerClusterer.prototype.clearMarkers = function() { * @param {boolean} opt_hide To also hide the marker. */ MarkerClusterer.prototype.resetViewport = function(opt_hide) { - // Remove all the clusters - for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { - cluster.remove(); - } + // Remove all the clusters + for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { + cluster.remove(); + } - // Reset the markers to not be added and to be invisible. - for (var i = 0, marker; marker = this.markers_[i]; i++) { - marker.isAdded = false; - if (opt_hide) { - marker.setMap(null); + // Reset the markers to not be added and to be invisible. + for (var i = 0, marker; marker = this.markers_[i]; i++) { + marker.isAdded = false; + if (opt_hide) { + marker.setMap(null); + } } - } - this.clusters_ = []; + this.clusters_ = []; }; /** * */ MarkerClusterer.prototype.repaint = function() { - var oldClusters = this.clusters_.slice(); - this.clusters_.length = 0; - this.resetViewport(); - this.redraw(); - - // Remove the old clusters. - // Do it in a timeout so the other clusters have been drawn first. - window.setTimeout(function() { - for (var i = 0, cluster; cluster = oldClusters[i]; i++) { - cluster.remove(); - } - }, 0); + var oldClusters = this.clusters_.slice(); + this.clusters_.length = 0; + this.resetViewport(); + this.redraw(); + + // Remove the old clusters. + // Do it in a timeout so the other clusters have been drawn first. + window.setTimeout(function() { + for (var i = 0, cluster; cluster = oldClusters[i]; i++) { + cluster.remove(); + } + }, 0); }; @@ -705,7 +713,7 @@ MarkerClusterer.prototype.repaint = function() { * Redraws the clusters. */ MarkerClusterer.prototype.redraw = function() { - this.createClusters_(); + this.createClusters_(); }; @@ -717,21 +725,21 @@ MarkerClusterer.prototype.redraw = function() { * @param {google.maps.LatLng} p2 The second lat lng point. * @return {number} The distance between the two points in km. * @private -*/ + */ MarkerClusterer.prototype.distanceBetweenPoints_ = function(p1, p2) { - if (!p1 || !p2) { - return 0; - } - - var R = 6371; // Radius of the Earth in km - var dLat = (p2.lat() - p1.lat()) * Math.PI / 180; - var dLon = (p2.lng() - p1.lng()) * Math.PI / 180; - var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + - Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) * - Math.sin(dLon / 2) * Math.sin(dLon / 2); - var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); - var d = R * c; - return d; + if (!p1 || !p2) { + return 0; + } + + var R = 6371; // Radius of the Earth in km + var dLat = (p2.lat() - p1.lat()) * Math.PI / 180; + var dLon = (p2.lng() - p1.lng()) * Math.PI / 180; + var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + + Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) * + Math.sin(dLon / 2) * Math.sin(dLon / 2); + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); + var d = R * c; + return d; }; @@ -742,27 +750,27 @@ MarkerClusterer.prototype.distanceBetweenPoints_ = function(p1, p2) { * @private */ MarkerClusterer.prototype.addToClosestCluster_ = function(marker) { - var distance = 40000; // Some large number - var clusterToAddTo = null; - var pos = marker.getPosition(); - for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { - var center = cluster.getCenter(); - if (center) { - var d = this.distanceBetweenPoints_(center, marker.getPosition()); - if (d < distance) { - distance = d; - clusterToAddTo = cluster; - } + var distance = 40000; // Some large number + var clusterToAddTo = null; + var pos = marker.getPosition(); + for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { + var center = cluster.getCenter(); + if (center) { + var d = this.distanceBetweenPoints_(center, marker.getPosition()); + if (d < distance) { + distance = d; + clusterToAddTo = cluster; + } + } + } + + if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) { + clusterToAddTo.addMarker(marker); + } else { + var cluster = new Cluster(this); + cluster.addMarker(marker); + this.clusters_.push(cluster); } - } - - if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) { - clusterToAddTo.addMarker(marker); - } else { - var cluster = new Cluster(this); - cluster.addMarker(marker); - this.clusters_.push(cluster); - } }; @@ -772,21 +780,21 @@ MarkerClusterer.prototype.addToClosestCluster_ = function(marker) { * @private */ MarkerClusterer.prototype.createClusters_ = function() { - if (!this.ready_) { - return; - } - - // Get our current map view bounds. - // Create a new bounds object so we don't affect the map. - var mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(), - this.map_.getBounds().getNorthEast()); - var bounds = this.getExtendedBounds(mapBounds); - - for (var i = 0, marker; marker = this.markers_[i]; i++) { - if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) { - this.addToClosestCluster_(marker); + if (!this.ready_) { + return; + } + + // Get our current map view bounds. + // Create a new bounds object so we don't affect the map. + var mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(), + this.map_.getBounds().getNorthEast()); + var bounds = this.getExtendedBounds(mapBounds); + + for (var i = 0, marker; marker = this.markers_[i]; i++) { + if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) { + this.addToClosestCluster_(marker); + } } - } }; @@ -799,16 +807,16 @@ MarkerClusterer.prototype.createClusters_ = function() { * @ignore */ function Cluster(markerClusterer) { - this.markerClusterer_ = markerClusterer; - this.map_ = markerClusterer.getMap(); - this.gridSize_ = markerClusterer.getGridSize(); - this.minClusterSize_ = markerClusterer.getMinClusterSize(); - this.averageCenter_ = markerClusterer.isAverageCenter(); - this.center_ = null; - this.markers_ = []; - this.bounds_ = null; - this.clusterIcon_ = new ClusterIcon(this, markerClusterer.getStyles(), - markerClusterer.getGridSize()); + this.markerClusterer_ = markerClusterer; + this.map_ = markerClusterer.getMap(); + this.gridSize_ = markerClusterer.getGridSize(); + this.minClusterSize_ = markerClusterer.getMinClusterSize(); + this.averageCenter_ = markerClusterer.isAverageCenter(); + this.center_ = null; + this.markers_ = []; + this.bounds_ = null; + this.clusterIcon_ = new ClusterIcon(this, markerClusterer.getStyles(), + markerClusterer.getGridSize()); } /** @@ -818,16 +826,16 @@ function Cluster(markerClusterer) { * @return {boolean} True if the marker is already added. */ Cluster.prototype.isMarkerAlreadyAdded = function(marker) { - if (this.markers_.indexOf) { - return this.markers_.indexOf(marker) != -1; - } else { - for (var i = 0, m; m = this.markers_[i]; i++) { - if (m == marker) { - return true; - } + if (this.markers_.indexOf) { + return this.markers_.indexOf(marker) != -1; + } else { + for (var i = 0, m; m = this.markers_[i]; i++) { + if (m == marker) { + return true; + } + } } - } - return false; + return false; }; @@ -838,45 +846,45 @@ Cluster.prototype.isMarkerAlreadyAdded = function(marker) { * @return {boolean} True if the marker was added. */ Cluster.prototype.addMarker = function(marker) { - if (this.isMarkerAlreadyAdded(marker)) { - return false; - } - - if (!this.center_) { - this.center_ = marker.getPosition(); - this.calculateBounds_(); - } else { - if (this.averageCenter_) { - var l = this.markers_.length + 1; - var lat = (this.center_.lat() * (l-1) + marker.getPosition().lat()) / l; - var lng = (this.center_.lng() * (l-1) + marker.getPosition().lng()) / l; - this.center_ = new google.maps.LatLng(lat, lng); - this.calculateBounds_(); + if (this.isMarkerAlreadyAdded(marker)) { + return false; } - } - marker.isAdded = true; - this.markers_.push(marker); + if (!this.center_) { + this.center_ = marker.getPosition(); + this.calculateBounds_(); + } else { + if (this.averageCenter_) { + var l = this.markers_.length + 1; + var lat = (this.center_.lat() * (l - 1) + marker.getPosition().lat()) / l; + var lng = (this.center_.lng() * (l - 1) + marker.getPosition().lng()) / l; + this.center_ = new google.maps.LatLng(lat, lng); + this.calculateBounds_(); + } + } - var len = this.markers_.length; - if (len < this.minClusterSize_ && marker.getMap() != this.map_) { - // Min cluster size not reached so show the marker. - marker.setMap(this.map_); - } + marker.isAdded = true; + this.markers_.push(marker); - if (len == this.minClusterSize_) { - // Hide the markers that were showing. - for (var i = 0; i < len; i++) { - this.markers_[i].setMap(null); + var len = this.markers_.length; + if (len < this.minClusterSize_ && marker.getMap() != this.map_) { + // Min cluster size not reached so show the marker. + marker.setMap(this.map_); } - } - if (len >= this.minClusterSize_) { - marker.setMap(null); - } + if (len == this.minClusterSize_) { + // Hide the markers that were showing. + for (var i = 0; i < len; i++) { + this.markers_[i].setMap(null); + } + } - this.updateIcon(); - return true; + if (len >= this.minClusterSize_) { + marker.setMap(null); + } + + this.updateIcon(); + return true; }; @@ -886,7 +894,7 @@ Cluster.prototype.addMarker = function(marker) { * @return {MarkerClusterer} The associated marker clusterer. */ Cluster.prototype.getMarkerClusterer = function() { - return this.markerClusterer_; + return this.markerClusterer_; }; @@ -896,12 +904,12 @@ Cluster.prototype.getMarkerClusterer = function() { * @return {google.maps.LatLngBounds} the cluster bounds. */ Cluster.prototype.getBounds = function() { - var bounds = new google.maps.LatLngBounds(this.center_, this.center_); - var markers = this.getMarkers(); - for (var i = 0, marker; marker = markers[i]; i++) { - bounds.extend(marker.getPosition()); - } - return bounds; + var bounds = new google.maps.LatLngBounds(this.center_, this.center_); + var markers = this.getMarkers(); + for (var i = 0, marker; marker = markers[i]; i++) { + bounds.extend(marker.getPosition()); + } + return bounds; }; @@ -909,9 +917,9 @@ Cluster.prototype.getBounds = function() { * Removes the cluster */ Cluster.prototype.remove = function() { - this.clusterIcon_.remove(); - this.markers_.length = 0; - delete this.markers_; + this.clusterIcon_.remove(); + this.markers_.length = 0; + delete this.markers_; }; @@ -921,7 +929,7 @@ Cluster.prototype.remove = function() { * @return {number} The cluster center. */ Cluster.prototype.getSize = function() { - return this.markers_.length; + return this.markers_.length; }; @@ -931,7 +939,7 @@ Cluster.prototype.getSize = function() { * @return {Array.} The cluster center. */ Cluster.prototype.getMarkers = function() { - return this.markers_; + return this.markers_; }; @@ -941,7 +949,7 @@ Cluster.prototype.getMarkers = function() { * @return {google.maps.LatLng} The cluster center. */ Cluster.prototype.getCenter = function() { - return this.center_; + return this.center_; }; @@ -951,8 +959,8 @@ Cluster.prototype.getCenter = function() { * @private */ Cluster.prototype.calculateBounds_ = function() { - var bounds = new google.maps.LatLngBounds(this.center_, this.center_); - this.bounds_ = this.markerClusterer_.getExtendedBounds(bounds); + var bounds = new google.maps.LatLngBounds(this.center_, this.center_); + this.bounds_ = this.markerClusterer_.getExtendedBounds(bounds); }; @@ -963,7 +971,7 @@ Cluster.prototype.calculateBounds_ = function() { * @return {boolean} True if the marker lies in the bounds. */ Cluster.prototype.isMarkerInClusterBounds = function(marker) { - return this.bounds_.contains(marker.getPosition()); + return this.bounds_.contains(marker.getPosition()); }; @@ -973,7 +981,7 @@ Cluster.prototype.isMarkerInClusterBounds = function(marker) { * @return {google.maps.Map} The map. */ Cluster.prototype.getMap = function() { - return this.map_; + return this.map_; }; @@ -981,28 +989,28 @@ Cluster.prototype.getMap = function() { * Updates the cluster icon */ Cluster.prototype.updateIcon = function() { - var zoom = this.map_.getZoom(); - var mz = this.markerClusterer_.getMaxZoom(); + var zoom = this.map_.getZoom(); + var mz = this.markerClusterer_.getMaxZoom(); + + if (mz && zoom > mz) { + // The zoom is greater than our max zoom so show all the markers in cluster. + for (var i = 0, marker; marker = this.markers_[i]; i++) { + marker.setMap(this.map_); + } + return; + } - if (mz && zoom > mz) { - // The zoom is greater than our max zoom so show all the markers in cluster. - for (var i = 0, marker; marker = this.markers_[i]; i++) { - marker.setMap(this.map_); + if (this.markers_.length < this.minClusterSize_) { + // Min cluster size not yet reached. + this.clusterIcon_.hide(); + return; } - return; - } - - if (this.markers_.length < this.minClusterSize_) { - // Min cluster size not yet reached. - this.clusterIcon_.hide(); - return; - } - - var numStyles = this.markerClusterer_.getStyles().length; - var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles); - this.clusterIcon_.setCenter(this.center_); - this.clusterIcon_.setSums(sums); - this.clusterIcon_.show(); + + var numStyles = this.markerClusterer_.getStyles().length; + var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles); + this.clusterIcon_.setCenter(this.center_); + this.clusterIcon_.setSums(sums); + this.clusterIcon_.show(); }; @@ -1024,18 +1032,18 @@ Cluster.prototype.updateIcon = function() { * @ignore */ function ClusterIcon(cluster, styles, opt_padding) { - cluster.getMarkerClusterer().extend(ClusterIcon, google.maps.OverlayView); - - this.styles_ = styles; - this.padding_ = opt_padding || 0; - this.cluster_ = cluster; - this.center_ = null; - this.map_ = cluster.getMap(); - this.div_ = null; - this.sums_ = null; - this.visible_ = false; - - this.setMap(this.map_); + cluster.getMarkerClusterer().extend(ClusterIcon, google.maps.OverlayView); + + this.styles_ = styles; + this.padding_ = opt_padding || 0; + this.cluster_ = cluster; + this.center_ = null; + this.map_ = cluster.getMap(); + this.div_ = null; + this.sums_ = null; + this.visible_ = false; + + this.setMap(this.map_); } @@ -1043,15 +1051,15 @@ function ClusterIcon(cluster, styles, opt_padding) { * Triggers the clusterclick event and zoom's if the option is set. */ ClusterIcon.prototype.triggerClusterClick = function() { - var markerClusterer = this.cluster_.getMarkerClusterer(); + var markerClusterer = this.cluster_.getMarkerClusterer(); - // Trigger the clusterclick event. - google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_); + // Trigger the clusterclick event. + google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_); - if (markerClusterer.isZoomOnClick()) { - // Zoom into the cluster. - this.map_.fitBounds(this.cluster_.getBounds()); - } + if (markerClusterer.isZoomOnClick()) { + // Zoom into the cluster. + this.map_.fitBounds(this.cluster_.getBounds()); + } }; @@ -1060,20 +1068,24 @@ ClusterIcon.prototype.triggerClusterClick = function() { * @ignore */ ClusterIcon.prototype.onAdd = function() { - this.div_ = document.createElement('DIV'); - if (this.visible_) { - var pos = this.getPosFromLatLng_(this.center_); - this.div_.style.cssText = this.createCss(pos); - this.div_.innerHTML = this.sums_.text; - } - - var panes = this.getPanes(); - panes.overlayMouseTarget.appendChild(this.div_); - - var that = this; - google.maps.event.addDomListener(this.div_, 'click', function() { - that.triggerClusterClick(); - }); + this.div_ = document.createElement('DIV'); + if (this.visible_) { + var pos = this.getPosFromLatLng_(this.center_); + this.div_.style.cssText = this.createCss(pos); + this.div_.innerHTML = this.sums_.text; + var markerClusterer = this.cluster_.getMarkerClusterer(); + if (markerClusterer.cssClass_) { + this.div_.className = markerClusterer.cssClass_; + } + } + + var panes = this.getPanes(); + panes.overlayMouseTarget.appendChild(this.div_); + + var that = this; + google.maps.event.addDomListener(this.div_, 'click', function() { + that.triggerClusterClick(); + }); }; @@ -1085,10 +1097,10 @@ ClusterIcon.prototype.onAdd = function() { * @private */ ClusterIcon.prototype.getPosFromLatLng_ = function(latlng) { - var pos = this.getProjection().fromLatLngToDivPixel(latlng); - pos.x -= parseInt(this.width_ / 2, 10); - pos.y -= parseInt(this.height_ / 2, 10); - return pos; + var pos = this.getProjection().fromLatLngToDivPixel(latlng); + pos.x -= parseInt(this.width_ / 2, 10); + pos.y -= parseInt(this.height_ / 2, 10); + return pos; }; @@ -1097,11 +1109,11 @@ ClusterIcon.prototype.getPosFromLatLng_ = function(latlng) { * @ignore */ ClusterIcon.prototype.draw = function() { - if (this.visible_) { - var pos = this.getPosFromLatLng_(this.center_); - this.div_.style.top = pos.y + 'px'; - this.div_.style.left = pos.x + 'px'; - } + if (this.visible_) { + var pos = this.getPosFromLatLng_(this.center_); + this.div_.style.top = pos.y + 'px'; + this.div_.style.left = pos.x + 'px'; + } }; @@ -1109,10 +1121,10 @@ ClusterIcon.prototype.draw = function() { * Hide the icon. */ ClusterIcon.prototype.hide = function() { - if (this.div_) { - this.div_.style.display = 'none'; - } - this.visible_ = false; + if (this.div_) { + this.div_.style.display = 'none'; + } + this.visible_ = false; }; @@ -1120,12 +1132,12 @@ ClusterIcon.prototype.hide = function() { * Position and show the icon. */ ClusterIcon.prototype.show = function() { - if (this.div_) { - var pos = this.getPosFromLatLng_(this.center_); - this.div_.style.cssText = this.createCss(pos); - this.div_.style.display = ''; - } - this.visible_ = true; + if (this.div_) { + var pos = this.getPosFromLatLng_(this.center_); + this.div_.style.cssText = this.createCss(pos); + this.div_.style.display = ''; + } + this.visible_ = true; }; @@ -1133,7 +1145,7 @@ ClusterIcon.prototype.show = function() { * Remove the icon from the map */ ClusterIcon.prototype.remove = function() { - this.setMap(null); + this.setMap(null); }; @@ -1142,11 +1154,11 @@ ClusterIcon.prototype.remove = function() { * @ignore */ ClusterIcon.prototype.onRemove = function() { - if (this.div_ && this.div_.parentNode) { - this.hide(); - this.div_.parentNode.removeChild(this.div_); - this.div_ = null; - } + if (this.div_ && this.div_.parentNode) { + this.hide(); + this.div_.parentNode.removeChild(this.div_); + this.div_ = null; + } }; @@ -1158,14 +1170,14 @@ ClusterIcon.prototype.onRemove = function() { * 'index': (number) The style index of the icon. */ ClusterIcon.prototype.setSums = function(sums) { - this.sums_ = sums; - this.text_ = sums.text; - this.index_ = sums.index; - if (this.div_) { - this.div_.innerHTML = sums.text; - } - - this.useStyle(); + this.sums_ = sums; + this.text_ = sums.text; + this.index_ = sums.index; + if (this.div_) { + this.div_.innerHTML = sums.text; + } + + this.useStyle(); }; @@ -1173,16 +1185,16 @@ ClusterIcon.prototype.setSums = function(sums) { * Sets the icon to the the styles. */ ClusterIcon.prototype.useStyle = function() { - var index = Math.max(0, this.sums_.index - 1); - index = Math.min(this.styles_.length - 1, index); - var style = this.styles_[index]; - this.url_ = style['url']; - this.height_ = style['height']; - this.width_ = style['width']; - this.textColor_ = style['textColor']; - this.anchor_ = style['anchor']; - this.textSize_ = style['textSize']; - this.backgroundPosition_ = style['backgroundPosition']; + var index = Math.max(0, this.sums_.index - 1); + index = Math.min(this.styles_.length - 1, index); + var style = this.styles_[index]; + this.url_ = style['url']; + this.height_ = style['height']; + this.width_ = style['width']; + this.textColor_ = style['textColor']; + this.anchor_ = style['anchor']; + this.textSize_ = style['textSize']; + this.backgroundPosition_ = style['backgroundPosition']; }; @@ -1192,7 +1204,7 @@ ClusterIcon.prototype.useStyle = function() { * @param {google.maps.LatLng} center The latlng to set as the center. */ ClusterIcon.prototype.setCenter = function(center) { - this.center_ = center; + this.center_ = center; }; @@ -1203,39 +1215,43 @@ ClusterIcon.prototype.setCenter = function(center) { * @return {string} The css style text. */ ClusterIcon.prototype.createCss = function(pos) { - var style = []; - style.push('background-image:url(' + this.url_ + ');'); - var backgroundPosition = this.backgroundPosition_ ? this.backgroundPosition_ : '0 0'; - style.push('background-position:' + backgroundPosition + ';'); - - if (typeof this.anchor_ === 'object') { - if (typeof this.anchor_[0] === 'number' && this.anchor_[0] > 0 && - this.anchor_[0] < this.height_) { - style.push('height:' + (this.height_ - this.anchor_[0]) + - 'px; padding-top:' + this.anchor_[0] + 'px;'); - } else { - style.push('height:' + this.height_ + 'px; line-height:' + this.height_ + - 'px;'); - } - if (typeof this.anchor_[1] === 'number' && this.anchor_[1] > 0 && - this.anchor_[1] < this.width_) { - style.push('width:' + (this.width_ - this.anchor_[1]) + - 'px; padding-left:' + this.anchor_[1] + 'px;'); + var style = []; + var markerClusterer = this.cluster_.getMarkerClusterer(); + if (!markerClusterer.cssClass_) { + style.push('background-image:url(' + this.url_ + ');'); + var backgroundPosition = this.backgroundPosition_ ? this.backgroundPosition_ : '0 0'; + style.push('background-position:' + backgroundPosition + ';'); + + if (typeof this.anchor_ === 'object') { + if (typeof this.anchor_[0] === 'number' && this.anchor_[0] > 0 && + this.anchor_[0] < this.height_) { + style.push('height:' + (this.height_ - this.anchor_[0]) + + 'px; padding-top:' + this.anchor_[0] + 'px;'); + } else { + style.push('height:' + this.height_ + 'px; line-height:' + this.height_ + 'px;'); + } + if (typeof this.anchor_[1] === 'number' && this.anchor_[1] > 0 && + this.anchor_[1] < this.width_) { + style.push('width:' + (this.width_ - this.anchor_[1]) + + 'px; padding-left:' + this.anchor_[1] + 'px;'); + } else { + style.push('width:' + this.width_ + 'px; text-align:center;'); + } + } else { + style.push('height:' + this.height_ + 'px; line-height:' + + this.height_ + 'px; width:' + this.width_ + 'px; text-align:center;'); + } + + var txtColor = this.textColor_ ? this.textColor_ : 'black'; + var txtSize = this.textSize_ ? this.textSize_ : 11; + + style.push('cursor:pointer; color:' + txtColor + '; position:absolute; font-size:' + + txtSize + 'px; font-family:Arial,sans-serif; font-weight:bold'); } else { - style.push('width:' + this.width_ + 'px; text-align:center;'); + style.push('top:' + pos.y + 'px; left:' + + pos.x + 'px;'); } - } else { - style.push('height:' + this.height_ + 'px; line-height:' + - this.height_ + 'px; width:' + this.width_ + 'px; text-align:center;'); - } - - var txtColor = this.textColor_ ? this.textColor_ : 'black'; - var txtSize = this.textSize_ ? this.textSize_ : 11; - - style.push('cursor:pointer; top:' + pos.y + 'px; left:' + - pos.x + 'px; color:' + txtColor + '; position:absolute; font-size:' + - txtSize + 'px; font-family:Arial,sans-serif; font-weight:bold'); - return style.join(''); + return style.join(''); }; @@ -1287,4 +1303,4 @@ Cluster.prototype['getMarkers'] = Cluster.prototype.getMarkers; ClusterIcon.prototype['onAdd'] = ClusterIcon.prototype.onAdd; ClusterIcon.prototype['draw'] = ClusterIcon.prototype.draw; -ClusterIcon.prototype['onRemove'] = ClusterIcon.prototype.onRemove; +ClusterIcon.prototype['onRemove'] = ClusterIcon.prototype.onRemove; \ No newline at end of file