vendor: 4,490 bytes, lines 1-152
1// ==ClosureCompiler== 2// @compilation_level ADVANCED_OPTIMIZATIONS 3// @externs_url http://closure-compiler.googlecode.com/svn/trunk/contrib/externs/maps/google_maps_api_v3.js 4// ==/ClosureCompiler== 5 6/** 7 * @name MarkerClusterer for Google Maps v3 8 * @version version 1.0 9 * @author Luke Mahe 10 * @fileoverview 11 * The library creates and manages per-zoom-level clusters for large amounts of 12 * markers. 13 * <br/> 14 * This is a v3 implementation of the 15 * <a href="http://gmaps-utility-library-dev.googlecode.com/svn/tags/markerclusterer/" 16 * >v2 MarkerClusterer</a>. 17 */ 18 19/** 20 * Licensed under the Apache License, Version 2.0 (the "License"); 21 * you may not use this file except in compliance with the License. 22 * You may obtain a copy of the License at 23 * 24 * http://www.apache.org/licenses/LICENSE-2.0 25 * 26 * Unless required by applicable law or agreed to in writing, software 27 * distributed under the License is distributed on an "AS IS" BASIS, 28 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 29 * See the License for the specific language governing permissions and 30 * limitations under the License. 31 */ 32 33 34/** 35 * A Marker Clusterer that clusters markers. 36 * 37 * @param {google.maps.Map} map The Google map to attach to. 38 * @param {Array.<google.maps.Marker>=} opt_markers Optional markers to add to 39 * the cluster. 40 * @param {Object=} opt_options support the following options: 41 * 'gridSize': (number) The grid size of a cluster in pixels. 42 * 'maxZoom': (number) The maximum zoom level that a marker can be part of a 43 * cluster. 44 * 'zoomOnClick': (boolean) Whether the default behaviour of clicking on a 45 * cluster is to zoom into it. 46 * 'averageCenter': (boolean) Wether the center of each cluster should be 47 * the average of all markers in the cluster. 48 * 'minimumClusterSize': (number) The minimum number of markers to be in a 49 * cluster before the markers are hidden and a count 50 * is shown. 51 * 'styles': (object) An object that has style properties: 52 * 'url': (string) The image url. 53 * 'height': (number) The image height. 54 * 'width': (number) The image width. 55 * 'anchor': (Array) The anchor position of the label text. 56 * 'textColor': (string) The text color. 57 * 'textSize': (number) The text size. 58 * 'backgroundPosition': (string) The position of the backgound x, y. 59 * @constructor 60 * @extends google.maps.OverlayView 61 */ 62function MarkerClusterer(map, opt_markers, opt_options) { 63 // MarkerClusterer implements google.maps.OverlayView interface. We use the 64 // extend function to extend MarkerClusterer with google.maps.OverlayView 65 // because it might not always be available when the code is defined so we 66 // look for it at the last possible moment. If it doesn't exist now then 67 // there is no point going ahead :) 68 this.extend(MarkerClusterer, google.maps.OverlayView); 69 this.map_ = map; 70 71 /** 72 * @type {Array.<google.maps.Marker>} 73 * @private 74 */ 75 this.markers_ = []; 76 77 /** 78 * @type {Array.<Cluster>} 79 */ 80 this.clusters_ = []; 81 82 this.sizes = [53, 56, 66, 78, 90]; 83 84 /** 85 * @private 86 */ 87 this.styles_ = []; 88 89 /** 90 * @type {boolean} 91 * @private 92 */ 93 this.ready_ = false; 94 95 var options = opt_options || {}; 96 97 /** 98 * @type {number} 99 * @private 100 */ 101 this.gridSize_ = options['gridSize'] || 60; 102 103 /** 104 * @private 105 */ 106 this.minClusterSize_ = options['minimumClusterSize'] || 2; 107 108 109 /** 110 * @type {?number} 111 * @private 112 */ 113 this.maxZoom_ = options['maxZoom'] || null; 114 115 this.styles_ = options['styles'] || []; 116 117 /** 118 * @type {string} 119 * @private 120 */ 121 this.imagePath_ = options['imagePath'] || 122 this.MARKER_CLUSTER_IMAGE_PATH_; 123 124 /** 125 * @type {string} 126 * @private 127 */ 128 this.imageExtension_ = options['imageExtension'] || 129 this.MARKER_CLUSTER_IMAGE_EXTENSION_; 130 131 /** 132 * @type {boolean} 133 * @private 134 */ 135 this.zoomOnClick_ = true; 136 137 if (options['zoomOnClick'] != undefined) { 138 this.zoomOnClick_ = options['zoomOnClick']; 139 } 140 141 /** 142 * @type {boolean} 143 * @private 144 */ 145 this.averageCenter_ = false; 146 147 if (options['averageCenter'] != undefined) { 148 this.averageCenter_ = options['averageCenter']; 149 } 150 151 this.setupStyles_(); 152
153 this.setMap(map); 154 155 /** 156 * @type {number} 157 * @private 158 */ 159 this.prevZoom_ = this.map_.getZoom(); 160 161 // Add the map event listeners 162 var that = this; 163 google.maps.event.addListener(this.map_, 'zoom_changed', function() { 164 var maxZoom = that.map_.mapTypes[that.map_.getMapTypeId()].maxZoom; 165 var zoom = that.map_.getZoom(); 166 if (zoom < 0 || zoom > maxZoom) { 167 return; 168 } 169 170 if (that.prevZoom_ != zoom) { 171 that.prevZoom_ = that.map_.getZoom(); 172 that.resetViewport(); 173 } 174 }); 175 176 google.maps.event.addListener(this.map_, 'idle', function() { 177 that.redraw(); 178 }); 179 180 // Finally, add the markers 181 if (opt_markers && opt_markers.length) { 182 this.addMarkers(opt_markers, false); 183 } 184} 185 186 187/** 188 * The marker cluster image path. 189 * 190 * @type {string} 191 * @private 192 */ 193MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_PATH_ = 194 '/portal_upload/images/googlemap/m'; 195 // 'http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/' + 196 // 'images/m'; 197 198/** 199 * The marker cluster image path. 200 * 201 * @type {string} 202 * @private 203 */ 204MarkerClusterer.prototype.MARKER_CLUSTER_IMAGE_EXTENSION_ = 'png'; 205 206 207/** 208 * Extends a objects prototype by anothers. 209 * 210 * @param {Object} obj1 The object to be extended. 211 * @param {Object} obj2 The object to extend with. 212 * @return {Object} The new extended object. 213 * @ignore 214 */ 215MarkerClusterer.prototype.extend = function(obj1, obj2) { 216 return (function(object) { 217 for (var property in object.prototype) { 218 this.prototype[property] = object.prototype[property]; 219 } 220 return this; 221 }).apply(obj1, [obj2]); 222}; 223 224 225/** 226 * Implementaion of the interface method. 227 * @ignore 228 */ 229MarkerClusterer.prototype.onAdd = function() { 230 this.setReady_(true); 231}; 232 233/** 234 * Implementaion of the interface method. 235 * @ignore 236 */ 237MarkerClusterer.prototype.draw = function() {}; 238 239/** 240 * Sets up the styles object. 241 * 242 * @private 243 */ 244MarkerClusterer.prototype.setupStyles_ = function() { 245 if (this.styles_.length) { 246 return; 247 } 248 249 for (var i = 0, size; size = this.sizes[i]; i++) { 250 this.styles_.push({ 251 url: this.imagePath_ + (i + 1) + '.' + this.imageExtension_, 252 height: size, 253 width: size 254 }); 255 } 256}; 257 258/** 259 * Fit the map to the bounds of the markers in the clusterer. 260 */ 261MarkerClusterer.prototype.fitMapToMarkers = function() { 262 var markers = this.getMarkers(); 263 var bounds = new google.maps.LatLngBounds(); 264 for (var i = 0, marker; marker = markers[i]; i++) { 265 bounds.extend(marker.getPosition()); 266 } 267 268 this.map_.fitBounds(bounds); 269}; 270 271 272/** 273 * Sets the styles. 274 * 275 * @param {Object} styles The style to set. 276 */ 277MarkerClusterer.prototype.setStyles = function(styles) { 278 this.styles_ = styles; 279}; 280 281 282/** 283 * Gets the styles. 284 * 285 * @return {Object} The styles object. 286 */ 287MarkerClusterer.prototype.getStyles = function() { 288 return this.styles_; 289}; 290 291 292/** 293 * Whether zoom on click is set. 294 * 295 * @return {boolean} True if zoomOnClick_ is set. 296 */ 297MarkerClusterer.prototype.isZoomOnClick = function() { 298 return this.zoomOnClick_; 299}; 300 301/** 302 * Whether average center is set. 303 * 304 * @return {boolean} True if averageCenter_ is set. 305 */ 306MarkerClusterer.prototype.isAverageCenter = function() { 307 return this.averageCenter_; 308}; 309 310 311/** 312 * Returns the array of markers in the clusterer. 313 * 314 * @return {Array.<google.maps.Marker>} The markers. 315 */ 316MarkerClusterer.prototype.getMarkers = function() { 317 return this.markers_; 318}; 319 320 321/** 322 * Returns the number of markers in the clusterer 323 * 324 * @return {Number} The number of markers. 325 */ 326MarkerClusterer.prototype.getTotalMarkers = function() { 327 return this.markers_.length; 328}; 329 330 331/** 332 * Sets the max zoom for the clusterer. 333 * 334 * @param {number} maxZoom The max zoom level. 335 */ 336MarkerClusterer.prototype.setMaxZoom = function(maxZoom) { 337 this.maxZoom_ = maxZoom; 338}; 339 340 341/** 342 * Gets the max zoom for the clusterer. 343 * 344 * @return {number} The max zoom level. 345 */ 346MarkerClusterer.prototype.getMaxZoom = function() { 347 return this.maxZoom_ || this.map_.mapTypes[this.map_.getMapTypeId()].maxZoom; 348}; 349 350 351/** 352 * The function for calculating the cluster icon image. 353 * 354 * @param {Array.<google.maps.Marker>} markers The markers in the clusterer. 355 * @param {number} numStyles The number of styles available. 356 * @return {Object} A object properties: 'text' (string) and 'index' (number). 357 * @private 358 */ 359MarkerClusterer.prototype.calculator_ = function(markers, numStyles) { 360 var index = 0; 361 var count = markers.length; 362 var dv = count; 363 while (dv !== 0) { 364 dv = parseInt(dv / 10, 10); 365 index++; 366 } 367 368 index = Math.min(index, numStyles); 369 return { 370 text: count, 371 index: index 372 }; 373};
374 375 376/** 377 * Set the calculator function. 378 * 379 * @param {function(Array, number)} calculator The function to set as the 380 * calculator. The function should return a object properties: 381 * 'text' (string) and 'index' (number). 382 * 383 */ 384MarkerClusterer.prototype.setCalculator = function(calculator) { 385 this.calculator_ = calculator; 386}; 387 388 389/** 390 * Get the calculator function. 391 * 392 * @return {function(Array, number)} the calculator function. 393 */ 394MarkerClusterer.prototype.getCalculator = function() { 395 return this.calculator_; 396}; 397 398 399/** 400 * Add an array of markers to the clusterer. 401 * 402 * @param {Array.<google.maps.Marker>} markers The markers to add. 403 * @param {boolean=} opt_nodraw Whether to redraw the clusters. 404 */ 405MarkerClusterer.prototype.addMarkers = function(markers, opt_nodraw) { 406 for (var i = 0, marker; marker = markers[i]; i++) { 407 this.pushMarkerTo_(marker); 408 } 409 if (!opt_nodraw) { 410 this.redraw(); 411 } 412}; 413 414 415/** 416 * Pushes a marker to the clusterer. 417 * 418 * @param {google.maps.Marker} marker The marker to add. 419 * @private 420 */ 421MarkerClusterer.prototype.pushMarkerTo_ = function(marker) { 422 marker.isAdded = false; 423 if (marker['draggable']) { 424 // If the marker is draggable add a listener so we update the clusters on 425 // the drag end. 426 var that = this; 427 google.maps.event.addListener(marker, 'dragend', function() { 428 marker.isAdded = false; 429 that.repaint(); 430 }); 431 } 432 this.markers_.push(marker); 433}; 434 435 436/** 437 * Adds a marker to the clusterer and redraws if needed. 438 * 439 * @param {google.maps.Marker} marker The marker to add. 440 * @param {boolean=} opt_nodraw Whether to redraw the clusters. 441 */ 442MarkerClusterer.prototype.addMarker = function(marker, opt_nodraw) { 443 this.pushMarkerTo_(marker); 444 if (!opt_nodraw) { 445 this.redraw(); 446 } 447}; 448 449 450/** 451 * Removes a marker and returns true if removed, false if not 452 * 453 * @param {google.maps.Marker} marker The marker to remove 454 * @return {boolean} Whether the marker was removed or not 455 * @private 456 */ 457MarkerClusterer.prototype.removeMarker_ = function(marker) { 458 var index = -1; 459 if (this.markers_.indexOf) { 460 index = this.markers_.indexOf(marker); 461 } else { 462 for (var i = 0, m; m = this.markers_[i]; i++) { 463 if (m == marker) { 464 index = i; 465 break; 466 } 467 } 468 } 469 470 if (index == -1) { 471 // Marker is not in our list of markers. 472 return false; 473 } 474 475 this.markers_.splice(index, 1); 476 477 return true; 478}; 479 480 481/** 482 * Remove a marker from the cluster. 483 * 484 * @param {google.maps.Marker} marker The marker to remove. 485 * @param {boolean=} opt_nodraw Optional boolean to force no redraw. 486 * @return {boolean} True if the marker was removed. 487 */ 488MarkerClusterer.prototype.removeMarker = function(marker, opt_nodraw) { 489 var removed = this.removeMarker_(marker); 490 491 if (!opt_nodraw && removed) { 492 this.resetViewport(); 493 this.redraw(); 494 return true; 495 } else { 496 return false; 497 } 498}; 499 500 501/** 502 * Removes an array of markers from the cluster. 503 * 504 * @param {Array.<google.maps.Marker>} markers The markers to remove. 505 * @param {boolean=} opt_nodraw Optional boolean to force no redraw. 506 */ 507MarkerClusterer.prototype.removeMarkers = function(markers, opt_nodraw) { 508 var removed = false; 509 510 for (var i = 0, marker; marker = markers[i]; i++) { 511 var r = this.removeMarker_(marker); 512 removed = removed || r; 513 } 514 515 if (!opt_nodraw && removed) { 516 this.resetViewport(); 517 this.redraw(); 518 return true; 519 } 520}; 521 522 523/** 524 * Sets the clusterer's ready state. 525 * 526 * @param {boolean} ready The state. 527 * @private 528 */ 529MarkerClusterer.prototype.setReady_ = function(ready) { 530 if (!this.ready_) { 531 this.ready_ = ready; 532 this.createClusters_(); 533 } 534}; 535 536 537/** 538 * Returns the number of clusters in the clusterer. 539 * 540 * @return {number} The number of clusters. 541 */ 542MarkerClusterer.prototype.getTotalClusters = function() { 543 return this.clusters_.length; 544}; 545 546 547/** 548 * Returns the google map that the clusterer is associated with. 549 * 550 * @return {google.maps.Map} The map. 551 */ 552MarkerClusterer.prototype.getMap = function() { 553 return this.map_; 554}; 555 556 557/** 558 * Sets the google map that the clusterer is associated with. 559 * 560 * @param {google.maps.Map} map The map. 561 */ 562MarkerClusterer.prototype.setMap = function(map) { 563 this.map_ = map; 564}; 565 566 567/** 568 * Returns the size of the grid. 569 * 570 * @return {number} The grid size. 571 */ 572MarkerClusterer.prototype.getGridSize = function() { 573 return this.gridSize_; 574}; 575 576 577/** 578 * Sets the size of the grid. 579 * 580 * @param {number} size The grid size. 581 */ 582MarkerClusterer.prototype.setGridSize = function(size) { 583 this.gridSize_ = size; 584}; 585 586 587/** 588 * Returns the min cluster size. 589 * 590 * @return {number} The grid size. 591 */ 592MarkerClusterer.prototype.getMinClusterSize = function() { 593 return this.minClusterSize_; 594}; 595 596/** 597 * Sets the min cluster size. 598 * 599 * @param {number} size The grid size. 600 */ 601MarkerClusterer.prototype.setMinClusterSize = function(size) { 602 this.minClusterSize_ = size; 603}; 604 605 606/** 607 * Extends a bounds object by the grid size. 608 * 609 * @param {google.maps.LatLngBounds} bounds The bounds to extend. 610 * @return {google.maps.LatLngBounds}
610 The extended bounds. 611 */ 612MarkerClusterer.prototype.getExtendedBounds = function(bounds) { 613 var projection = this.getProjection(); 614 615 // Turn the bounds into latlng. 616 var tr = new google.maps.LatLng(bounds.getNorthEast().lat(), 617 bounds.getNorthEast().lng()); 618 var bl = new google.maps.LatLng(bounds.getSouthWest().lat(), 619 bounds.getSouthWest().lng()); 620 621 // Convert the points to pixels and the extend out by the grid size. 622 var trPix = projection.fromLatLngToDivPixel(tr); 623 trPix.x += this.gridSize_; 624 trPix.y -= this.gridSize_; 625 626 var blPix = projection.fromLatLngToDivPixel(bl); 627 blPix.x -= this.gridSize_; 628 blPix.y += this.gridSize_; 629 630 // Convert the pixel points back to LatLng 631 var ne = projection.fromDivPixelToLatLng(trPix); 632 var sw = projection.fromDivPixelToLatLng(blPix); 633 634 // Extend the bounds to contain the new bounds. 635 bounds.extend(ne); 636 bounds.extend(sw); 637 638 return bounds; 639}; 640 641 642/** 643 * Determins if a marker is contained in a bounds. 644 * 645 * @param {google.maps.Marker} marker The marker to check. 646 * @param {google.maps.LatLngBounds} bounds The bounds to check against. 647 * @return {boolean} True if the marker is in the bounds. 648 * @private 649 */ 650MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) { 651 return bounds.contains(marker.getPosition()); 652}; 653 654 655/** 656 * Clears all clusters and markers from the clusterer. 657 */ 658MarkerClusterer.prototype.clearMarkers = function() { 659 this.resetViewport(true); 660 661 // Set the markers a empty array. 662 this.markers_ = []; 663}; 664 665 666/** 667 * Clears all existing clusters and recreates them. 668 * @param {boolean} opt_hide To also hide the marker. 669 */ 670MarkerClusterer.prototype.resetViewport = function(opt_hide) { 671 // Remove all the clusters 672 for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { 673 cluster.remove(); 674 } 675 676 // Reset the markers to not be added and to be invisible. 677 for (var i = 0, marker; marker = this.markers_[i]; i++) { 678 marker.isAdded = false; 679 if (opt_hide) { 680 marker.setMap(null); 681 } 682 } 683 684 this.clusters_ = []; 685}; 686 687/** 688 * 689 */ 690MarkerClusterer.prototype.repaint = function() { 691 var oldClusters = this.clusters_.slice(); 692 this.clusters_.length = 0; 693 this.resetViewport(); 694 this.redraw(); 695 696 // Remove the old clusters. 697 // Do it in a timeout so the other clusters have been drawn first. 698 window.setTimeout(function() { 699 for (var i = 0, cluster; cluster = oldClusters[i]; i++) { 700 cluster.remove(); 701 } 702 }, 0); 703}; 704 705 706/** 707 * Redraws the clusters. 708 */ 709MarkerClusterer.prototype.redraw = function() { 710 this.createClusters_(); 711}; 712 713 714/** 715 * Calculates the distance between two latlng locations in km. 716 * @see http://www.movable-type.co.uk/scripts/latlong.html 717 * 718 * @param {google.maps.LatLng} p1 The first lat lng point. 719 * @param {google.maps.LatLng} p2 The second lat lng point. 720 * @return {number} The distance between the two points in km. 721 * @private 722*/ 723MarkerClusterer.prototype.distanceBetweenPoints_ = function(p1, p2) { 724 if (!p1 || !p2) { 725 return 0; 726 } 727 728 var R = 6371; // Radius of the Earth in km 729 var dLat = (p2.lat() - p1.lat()) * Math.PI / 180; 730 var dLon = (p2.lng() - p1.lng()) * Math.PI / 180; 731 var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + 732 Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) * 733 Math.sin(dLon / 2) * Math.sin(dLon / 2); 734 var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); 735 var d = R * c; 736 return d; 737}; 738 739 740/** 741 * Add a marker to a cluster, or creates a new cluster. 742 * 743 * @param {google.maps.Marker} marker The marker to add. 744 * @private 745 */ 746MarkerClusterer.prototype.addToClosestCluster_ = function(marker) { 747 var distance = 40000; // Some large number 748 var clusterToAddTo = null; 749 var pos = marker.getPosition(); 750 for (var i = 0, cluster; cluster = this.clusters_[i]; i++) { 751 var center = cluster.getCenter(); 752 if (center) { 753 var d = this.distanceBetweenPoints_(center, marker.getPosition()); 754 if (d < distance) { 755 distance = d; 756 clusterToAddTo = cluster; 757 } 758 } 759 } 760 761 if (clusterToAddTo && clusterToAddTo.isMarkerInClusterBounds(marker)) { 762 clusterToAddTo.addMarker(marker); 763 } else { 764 var cluster = new Cluster(this); 765 cluster.addMarker(marker);
766 this.clusters_.push(cluster); 767 } 768}; 769 770 771/** 772 * Creates the clusters. 773 * 774 * @private 775 */ 776MarkerClusterer.prototype.createClusters_ = function() { 777 if (!this.ready_) { 778 return; 779 } 780 781 // Get our current map view bounds. 782 // Create a new bounds object so we don't affect the map. 783 var mapBounds = new google.maps.LatLngBounds(this.map_.getBounds().getSouthWest(), 784 this.map_.getBounds().getNorthEast()); 785 var bounds = this.getExtendedBounds(mapBounds); 786 787 for (var i = 0, marker; marker = this.markers_[i]; i++) { 788 if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) { 789 this.addToClosestCluster_(marker); 790 } 791 } 792}; 793 794 795/** 796 * A cluster that contains markers. 797 * 798 * @param {MarkerClusterer} markerClusterer The markerclusterer that this 799 * cluster is associated with. 800 * @constructor 801 * @ignore 802 */ 803function Cluster(markerClusterer) { 804 this.markerClusterer_ = markerClusterer; 805 this.map_ = markerClusterer.getMap(); 806 this.gridSize_ = markerClusterer.getGridSize(); 807 this.minClusterSize_ = markerClusterer.getMinClusterSize(); 808 this.averageCenter_ = markerClusterer.isAverageCenter(); 809 this.center_ = null; 810 this.markers_ = []; 811 this.bounds_ = null; 812 this.clusterIcon_ = new ClusterIcon(this, markerClusterer.getStyles(), 813 markerClusterer.getGridSize()); 814} 815 816/** 817 * Determins if a marker is already added to the cluster. 818 * 819 * @param {google.maps.Marker} marker The marker to check. 820 * @return {boolean} True if the marker is already added. 821 */ 822Cluster.prototype.isMarkerAlreadyAdded = function(marker) { 823 if (this.markers_.indexOf) { 824 return this.markers_.indexOf(marker) != -1; 825 } else { 826 for (var i = 0, m; m = this.markers_[i]; i++) { 827 if (m == marker) { 828 return true; 829 } 830 } 831 } 832 return false; 833}; 834 835 836/** 837 * Add a marker the cluster. 838 * 839 * @param {google.maps.Marker} marker The marker to add. 840 * @return {boolean} True if the marker was added. 841 */ 842Cluster.prototype.addMarker = function(marker) { 843 if (this.isMarkerAlreadyAdded(marker)) { 844 return false; 845 } 846 847 if (!this.center_) { 848 this.center_ = marker.getPosition(); 849 this.calculateBounds_(); 850 } else { 851 if (this.averageCenter_) { 852 var l = this.markers_.length + 1; 853 var lat = (this.center_.lat() * (l-1) + marker.getPosition().lat()) / l; 854 var lng = (this.center_.lng() * (l-1) + marker.getPosition().lng()) / l; 855 this.center_ = new google.maps.LatLng(lat, lng); 856 this.calculateBounds_(); 857 } 858 } 859 860 marker.isAdded = true; 861 this.markers_.push(marker); 862 863 var len = this.markers_.length; 864 if (len < this.minClusterSize_ && marker.getMap() != this.map_) { 865 // Min cluster size not reached so show the marker. 866 marker.setMap(this.map_); 867 } 868 869 if (len == this.minClusterSize_) { 870 // Hide the markers that were showing. 871 for (var i = 0; i < len; i++) { 872 this.markers_[i].setMap(null); 873 } 874 } 875 876 if (len >= this.minClusterSize_) { 877 marker.setMap(null); 878 } 879 880 this.updateIcon(); 881 return true; 882}; 883 884 885/** 886 * Returns the marker clusterer that the cluster is associated with. 887 * 888 * @return {MarkerClusterer} The associated marker clusterer. 889 */ 890Cluster.prototype.getMarkerClusterer = function() { 891 return this.markerClusterer_; 892}; 893 894 895/** 896 * Returns the bounds of the cluster. 897 * 898 * @return {google.maps.LatLngBounds} the cluster bounds. 899 */ 900Cluster.prototype.getBounds = function() { 901 var bounds = new google.maps.LatLngBounds(this.center_, this.center_); 902 var markers = this.getMarkers(); 903 for (var i = 0, marker; marker = markers[i]; i++) { 904 bounds.extend(marker.getPosition()); 905 } 906 return bounds; 907}; 908 909 910/** 911 * Removes the cluster 912 */ 913Cluster.prototype.remove = function() { 914 this.clusterIcon_.remove(); 915 this.markers_.length = 0; 916 delete this.markers_; 917}; 918 919 920/** 921 * Returns the center of the cluster. 922 * 923 * @return {number} The cluster center. 924 */ 925Cluster.prototype.getSize = function() { 926 return this.markers_.length; 927}; 928 929 930/** 931 * Returns the center of the cluster. 932 * 933 * @return {Array.<google.maps.Marker>} The cluster center.
934 */ 935Cluster.prototype.getMarkers = function() { 936 return this.markers_; 937}; 938 939 940/** 941 * Returns the center of the cluster. 942 * 943 * @return {google.maps.LatLng} The cluster center. 944 */ 945Cluster.prototype.getCenter = function() { 946 return this.center_; 947}; 948 949 950/** 951 * Calculated the extended bounds of the cluster with the grid. 952 * 953 * @private 954 */ 955Cluster.prototype.calculateBounds_ = function() { 956 var bounds = new google.maps.LatLngBounds(this.center_, this.center_); 957 this.bounds_ = this.markerClusterer_.getExtendedBounds(bounds); 958}; 959 960 961/** 962 * Determines if a marker lies in the clusters bounds. 963 * 964 * @param {google.maps.Marker} marker The marker to check. 965 * @return {boolean} True if the marker lies in the bounds. 966 */ 967Cluster.prototype.isMarkerInClusterBounds = function(marker) { 968 return this.bounds_.contains(marker.getPosition()); 969}; 970 971 972/** 973 * Returns the map that the cluster is associated with. 974 * 975 * @return {google.maps.Map} The map. 976 */ 977Cluster.prototype.getMap = function() { 978 return this.map_; 979}; 980 981 982/** 983 * Updates the cluster icon 984 */ 985Cluster.prototype.updateIcon = function() { 986 var zoom = this.map_.getZoom(); 987 var mz = this.markerClusterer_.getMaxZoom(); 988 989 if (zoom > mz) { 990 // The zoom is greater than our max zoom so show all the markers in cluster. 991 for (var i = 0, marker; marker = this.markers_[i]; i++) { 992 marker.setMap(this.map_); 993 } 994 return; 995 } 996 997 if (this.markers_.length < this.minClusterSize_) { 998 // Min cluster size not yet reached. 999 this.clusterIcon_.hide(); 1000 return; 1001 } 1002 1003 var numStyles = this.markerClusterer_.getStyles().length; 1004 var sums = this.markerClusterer_.getCalculator()(this.markers_, numStyles); 1005 this.clusterIcon_.setCenter(this.center_); 1006 this.clusterIcon_.setSums(sums); 1007 this.clusterIcon_.show(); 1008}; 1009 1010 1011/** 1012 * A cluster icon 1013 * 1014 * @param {Cluster} cluster The cluster to be associated with. 1015 * @param {Object} styles An object that has style properties: 1016 * 'url': (string) The image url. 1017 * 'height': (number) The image height. 1018 * 'width': (number) The image width. 1019 * 'anchor': (Array) The anchor position of the label text. 1020 * 'textColor': (string) The text color. 1021 * 'textSize': (number) The text size. 1022 * 'backgroundPosition: (string) The background postition x, y. 1023 * @param {number=} opt_padding Optional padding to apply to the cluster icon. 1024 * @constructor 1025 * @extends google.maps.OverlayView 1026 * @ignore 1027 */ 1028function ClusterIcon(cluster, styles, opt_padding) { 1029 cluster.getMarkerClusterer().extend(ClusterIcon, google.maps.OverlayView); 1030 1031 this.styles_ = styles; 1032 this.padding_ = opt_padding || 0; 1033 this.cluster_ = cluster; 1034 this.center_ = null; 1035 this.map_ = cluster.getMap(); 1036 this.div_ = null; 1037 this.sums_ = null; 1038 this.visible_ = false; 1039 1040 this.setMap(this.map_); 1041} 1042 1043 1044/** 1045 * Triggers the clusterclick event and zoom's if the option is set. 1046 */ 1047ClusterIcon.prototype.triggerClusterClick = function() { 1048 var markerClusterer = this.cluster_.getMarkerClusterer(); 1049 1050 // Trigger the clusterclick event. 1051 google.maps.event.trigger(markerClusterer, 'clusterclick', this.cluster_); 1052 1053 if (markerClusterer.isZoomOnClick()) { 1054 // Zoom into the cluster. 1055 this.map_.fitBounds(this.cluster_.getBounds()); 1056 } 1057}; 1058 1059 1060/** 1061 * Adding the cluster icon to the dom. 1062 * @ignore 1063 */ 1064ClusterIcon.prototype.onAdd = function() { 1065 this.div_ = document.createElement('DIV'); 1066 if (this.visible_) { 1067 var pos = this.getPosFromLatLng_(this.center_); 1068 this.div_.style.cssText = this.createCss(pos); 1069 this.div_.innerHTML = this.sums_.text; 1070 } 1071 1072 var panes = this.getPanes(); 1073 panes.overlayMouseTarget.appendChild(this.div_); 1074 1075 var that = this; 1076 google.maps.event.addDomListener(this.div_, 'click', function() { 1077 that.triggerClusterClick(); 1078 }); 1079}; 1080 1081 1082/** 1083 * Returns the position to place the div dending on the latlng. 1084 * 1085 * @param {google.maps.LatLng} latlng The position in latlng. 1086 * @return {google.maps.Point} The position in pixels. 1087 * @private 1088 */ 1089ClusterIcon.prototype.getPosFromLatLng_ = function(latlng) { 1090 var pos = this.getProjection().fromLatLngToDivPixel(latlng); 1091 pos.x -= parseInt(this.width_ / 2, 10); 1092 pos.y -= parseInt(this.height_ / 2, 10); 1093 return pos; 1094}; 1095 1096 1097/** 1098 * Draw the icon. 1099 * @ignore 1100 */ 1101ClusterIcon.prototype.draw = function() { 1102 if (this.visible_) { 1103 var pos = this.getPosFromLatLng_(this.center_); 1104 this.div_.style.top = pos.y + 'px'; 1105 this.div_.style.left = pos.x + 'px'; 1106 } 1107}; 1108 1109 1110/** 1111 * Hide the icon. 1112 */ 1113ClusterIcon.prototype.hide = function() { 1114 if (this.div_) { 1115 this.div_.style.display = 'none'; 1116 } 1117 this.visible_ = false; 1118}; 1119 1120 1121/** 1122 * Position and show the icon. 1123 */ 1124ClusterIcon.prototype.show = function() { 1125 if (this.div_) { 1126 var pos = this.getPosFromLatLng_(this.center_); 1127 this.div_.style.cssText = this.createCss(pos); 1128 this.div_.style.display = ''; 1129 } 1130 this.visible_ = true; 1131}; 1132 1133 1134/** 1135 * Remove the icon from the map 1136 */ 1137ClusterIcon.prototype.remove = function() { 1138 this.setMap(null); 1139}; 1140 1141 1142/** 1143 * Implementation of the onRemove interface. 1144 * @ignore 1145 */ 1146ClusterIcon.prototype.onRemove = function() { 1147 if (this.div_ && this.div_.parentNode) { 1148 this.hide(); 1149 this.div_.parentNode.removeChild(this.div_); 1150 this.div_ = null; 1151 } 1152}; 1153 1154 1155/** 1156 * Set the sums of the icon. 1157 * 1158 * @param {Object} sums The sums containing: 1159 * 'text': (string) The text to display in the icon. 1160 * 'index': (number) The style index of the icon. 1161 */ 1162ClusterIcon.prototype.setSums = function(sums) { 1163 this.sums_ = sums; 1164 this.text_ = sums.text; 1165 this.index_ = sums.index; 1166 if (this.div_) { 1167 this.div_.innerHTML = sums.text; 1168 } 1169 1170 this.useStyle(); 1171}; 1172 1173 1174/** 1175 * Sets the icon to the the styles. 1176 */ 1177ClusterIcon.prototype.useStyle = function() { 1178 var index = Math.max(0, this.sums_.index - 1); 1179 index = Math.min(this.styles_.length - 1, index); 1180 var style = this.styles_[index]; 1181 this.url_ = style['url']; 1182 this.height_ = style['height']; 1183 this.width_ = style['width']; 1184 this.textColor_ = style['textColor']; 1185 this.anchor_ = style['anchor']; 1186 this.textSize_ = style['textSize']; 1187 this.backgroundPosition_ = style['backgroundPosition']; 1188};
1189 1190 1191/** 1192 * Sets the center of the icon. 1193 * 1194 * @param {google.maps.LatLng} center The latlng to set as the center. 1195 */ 1196ClusterIcon.prototype.setCenter = function(center) { 1197 this.center_ = center; 1198}; 1199 1200 1201/** 1202 * Create the css text based on the position of the icon. 1203 * 1204 * @param {google.maps.Point} pos The position. 1205 * @return {string} The css style text. 1206 */ 1207ClusterIcon.prototype.createCss = function(pos) { 1208 var style = []; 1209 if (document.all && document.namespaces) { 1210 style.push('filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(' + 1211 'sizingMethod=scale,src="' + this.url_ + '");'); 1212 } else { 1213 style.push('background-image:url(' + this.url_ + ');'); 1214 var backgroundPosition = this.backgroundPosition_ ? this.backgroundPosition_ : '0 0'; 1215 style.push('background-position:' + backgroundPosition + ';'); 1216 } 1217 1218 if (typeof this.anchor_ === 'object') { 1219 if (typeof this.anchor_[0] === 'number' && this.anchor_[0] > 0 && 1220 this.anchor_[0] < this.height_) { 1221 style.push('height:' + (this.height_ - this.anchor_[0]) + 1222 'px; padding-top:' + this.anchor_[0] + 'px;'); 1223 } else { 1224 style.push('height:' + this.height_ + 'px; line-height:' + this.height_ + 1225 'px;'); 1226 } 1227 if (typeof this.anchor_[1] === 'number' && this.anchor_[1] > 0 && 1228 this.anchor_[1] < this.width_) { 1229 style.push('width:' + (this.width_ - this.anchor_[1]) + 1230 'px; padding-left:' + this.anchor_[1] + 'px;'); 1231 } else { 1232 style.push('width:' + this.width_ + 'px; text-align:center;'); 1233 } 1234 } else { 1235 style.push('height:' + this.height_ + 'px; line-height:' + 1236 this.height_ + 'px; width:' + this.width_ + 'px; text-align:center;'); 1237 } 1238 1239 var txtColor = this.textColor_ ? this.textColor_ : 'black'; 1240 var txtSize = this.textSize_ ? this.textSize_ : 11; 1241 1242 style.push('cursor:pointer; top:' + pos.y + 'px; left:' + 1243 pos.x + 'px; color:' + txtColor + '; position:absolute; font-size:' + 1244 txtSize + 'px; font-family:Arial,sans-serif; font-weight:bold'); 1245 return style.join(''); 1246}; 1247 1248 1249// Export Symbols for Closure 1250// If you are not going to compile with closure then you can remove the 1251// code below. 1252window['MarkerClusterer'] = MarkerClusterer; 1253MarkerClusterer.prototype['addMarker'] = MarkerClusterer.prototype.addMarker; 1254MarkerClusterer.prototype['addMarkers'] = MarkerClusterer.prototype.addMarkers; 1255MarkerClusterer.prototype['clearMarkers'] = 1256 MarkerClusterer.prototype.clearMarkers; 1257MarkerClusterer.prototype['fitMapToMarkers'] = 1258 MarkerClusterer.prototype.fitMapToMarkers; 1259MarkerClusterer.prototype['getCalculator'] = 1260 MarkerClusterer.prototype.getCalculator; 1261MarkerClusterer.prototype['getGridSize'] = 1262 MarkerClusterer.prototype.getGridSize; 1263MarkerClusterer.prototype['getExtendedBounds'] = 1264 MarkerClusterer.prototype.getExtendedBounds; 1265MarkerClusterer.prototype['getMap'] = MarkerClusterer.prototype.getMap; 1266MarkerClusterer.prototype['getMarkers'] = MarkerClusterer.prototype.getMarkers; 1267MarkerClusterer.prototype['getMaxZoom'] = MarkerClusterer.prototype.getMaxZoom; 1268MarkerClusterer.prototype['getStyles'] = MarkerClusterer.prototype.getStyles; 1269MarkerClusterer.prototype['getTotalClusters'] = 1270 MarkerClusterer.prototype.getTotalClusters; 1271MarkerClusterer.prototype['getTotalMarkers'] = 1272 MarkerClusterer.prototype.getTotalMarkers; 1273MarkerClusterer.prototype['redraw'] = MarkerClusterer.prototype.redraw; 1274MarkerClusterer.prototype['removeMarker'] = 1275 MarkerClusterer.prototype.removeMarker; 1276MarkerClusterer.prototype['removeMarkers'] = 1277 MarkerClusterer.prototype.removeMarkers; 1278MarkerClusterer.prototype['resetViewport'] = 1279 MarkerClusterer.prototype.resetViewport; 1280MarkerClusterer.prototype['repaint'] = 1281 MarkerClusterer.prototype.repaint; 1282MarkerClusterer.prototype['setCalculator'] = 1283 MarkerClusterer.prototype.setCalculator; 1284MarkerClusterer.prototype['setGridSize'] = 1285 MarkerClusterer.prototype.setGridSize; 1286MarkerClusterer.prototype['onAdd'] = MarkerClusterer.prototype.onAdd;
1287MarkerClusterer.prototype['draw'] = MarkerClusterer.prototype.draw; 1288 1289Cluster.prototype['getCenter'] = Cluster.prototype.getCenter; 1290Cluster.prototype['getSize'] = Cluster.prototype.getSize; 1291Cluster.prototype['getMarkers'] = Cluster.prototype.getMarkers; 1292 1293ClusterIcon.prototype['onAdd'] = ClusterIcon.prototype.onAdd; 1294ClusterIcon.prototype['draw'] = ClusterIcon.prototype.draw; 1295ClusterIcon.prototype['onRemove'] = ClusterIcon.prototype.onRemove;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.