Jaitnium
Jaitnium

Reputation: 650

Google Maps MarkerCluster API: How to get clusters outside of screen's view?

I have a webpage that uses Google maps and the MarkerCluster API. I need to be able to get all of the clusters on the map at a given zoom level. Take this code for example:

//Where the center of the screen will be
var center = new google.maps.LatLng(37.4419, -122.1419);
var options = {
  'zoom': 13,
  'center': center,
  //Google map type
  'mapTypeId': google.maps.MapTypeId.ROADMAP
};

//Create the google map
var map = new google.maps.Map(document.getElementById("map"), options);
//Create the marker clusters, where markers is an array of lat and longs
var mc = new MarkerClusterer(map, markers);
//Print all of the clusters at zoom level 13
console.log(mc.getTotalClusters());

The problem is if there are 10 clusters at zoom level 13, but only 7 are inside of my screen's bounds, then the above code would only print out 7. I need a way to get access to all clusters, even if they're not on the screen.

Simple example of how the MarkerClusterer works: https://googlemaps.github.io/js-marker-clusterer/examples/simple_example.html

Here are some references to the MarkerCluster API:

https://googlemaps.github.io/js-marker-clusterer/docs/reference.html

https://googlemaps.github.io/js-marker-clusterer/docs/examples.html

https://developers.google.com/maps/articles/toomanymarkers

Upvotes: 2

Views: 3753

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59338

Indeed, getTotalClusters function returns the number of clusters only for markers visible within a current viewport.

One option to get the total amount of clusters would be to disable the checking whether the marker is located within a current viewport by overriding the function that creates clusters:

MarkerClusterer.prototype.createClusters_ = function() {
  if (!this.ready_) {
    return;
  }

  for (var i = 0, marker; marker = this.markers_[i]; i++) {
    //if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) {    
    if (!marker.isAdded) {    
      this.addToClosestCluster_(marker);
    }
  }
};

Working example

MarkerClusterer.prototype.createClusters_ = function() {
  if (!this.ready_) {
    return;
  }

  for (var i = 0, marker; marker = this.markers_[i]; i++) {
    //if (!marker.isAdded && this.isMarkerInBounds_(marker, bounds)) {    
    if (!marker.isAdded) {    
      this.addToClosestCluster_(marker);
    }
  }
};


function initMap(data) {
    var center = new google.maps.LatLng(59.339025,18.065818);

    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 5,
        center: center,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    });

 
    var markers = [];
    for (var i = 0; i < data.photos.length; i++) {
        var dataPhoto = data.photos[i];
        var latLng = new google.maps.LatLng(dataPhoto.latitude,dataPhoto.longitude);
        var marker = new google.maps.Marker({
            position: latLng
        });
        markers.push(marker);
    }
    markerCluster = new MarkerClusterer(map, markers, { imagePath: 'https://cdn.rawgit.com/googlemaps/js-marker-clusterer/gh-pages/images/m' });

    google.maps.event.addListenerOnce(map, 'idle', function(){
        console.log("Total number of clusters: " + markerCluster.getTotalClusters());
    });
}
google.maps.event.addDomListener(window, 'load', 
function(){
    $.getJSON("https://gist.githubusercontent.com/vgrem/fb601469049c4be809ad4ea4bbcdc381/raw/data.json")
    .success(function(data) {
        initMap(data);
    });    
});
body {
        margin: 0;
        padding: 10px 20px 20px;
        font-family: Arial;
        font-size: 16px;
      }
      #map-container {
        padding: 6px;
        border-width: 1px;
        border-style: solid;
        border-color: #ccc #ccc #999 #ccc;
        -webkit-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
        -moz-box-shadow: rgba(64, 64, 64, 0.5) 0 2px 5px;
        box-shadow: rgba(64, 64, 64, 0.1) 0 2px 5px;
        width: 600px;
      }
      #map {
        width: 600px;
        height: 400px;
      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<script src="https://googlemaps.github.io/js-marker-clusterer/src/markerclusterer.js"></script>
<div id="map-container"><div id="map"></div></div>

Plunker

Upvotes: 2

Related Questions