jbdemonte / gmap3

jQuery plugin to create Google maps
http://gmap3.net
668 stars 198 forks source link

fit is not working with cluster #130

Open imran-saleem834 opened 7 years ago

imran-saleem834 commented 7 years ago

When i am use maker function then fit is work perfect, but on time of cluster is not working

var mapOpts = {
        center: [46.578498, 2.457275],
        zoom: 1,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
var entries: [
            {position: [48.8620722, 2.352047], info: 'A'},
            {position: [44.28952958093682, 6.152559438984804], info: 'B'},
            {position: [49.28952958093682, -1.1501188139848408], info: 'C'},
            {position: [44.28952958093682, -1.1501188139848408], info: 'D'}
          ];
$('#map').gmap3(mapOpts)
            .infowindow({
                content: ''
            })
            .then(function (iw) {
                infowindow = iw;
            })
            .cluster({
                size: 200,
                markers: entries,
                cb: function (markers) {
                    if (markers.length > 1) { // 1 marker stay unchanged (because cb returns nothing)
                        if (markers.length < 20) {
                            return {
                                content: "<div class='cluster cluster-1'>" + markers.length + "</div>",
                                x: -26,
                                y: -26
                            };
                        }
                        if (markers.length < 50) {
                            return {
                                content: "<div class='cluster cluster-2'>" + markers.length + "</div>",
                                x: -26,
                                y: -26
                            };
                        }
                        return {
                            content: "<div class='cluster cluster-3'>" + markers.length + "</div>",
                            x: -33,
                            y: -33
                        };
                    }
                }
            })
            .on('click', function (marker, clusterOverlay, cluster, event) {
                if (marker) {
                    infowindow.setContent(marker.info);
                    infowindow.open(marker.getMap(), marker);
                }
            })
            .wait(2000) // to let you appreciate the current zoom & center
            .fit();
zotopteam commented 7 years ago

i have this problem too

old code not effect in v7

map.panTo(cluster.main.getPosition());

At last i change the v7 source in self.fit

self.fit = chainToPromise(function () {
      var bounds = gmElement('LatLngBounds');
      foreach(previousResults, function (instances) {
        if (instances !== map) {
          foreach(instances, function (instance) {
            if (instance) {
              if (instance.getPosition && instance.getPosition()) {
                bounds.extend(instance.getPosition());
              } else if (instance.getBounds && instance.getBounds()) {
                bounds.extend(instance.getBounds().getNorthEast());
                bounds.extend(instance.getBounds().getSouthWest());
              } else if (instance.getPaths && instance.getPaths()) {
                foreach(instance.getPaths().getArray(), function (path) {
                  foreach(path.getArray(), function (latLng) {
                    bounds.extend(latLng);
                  });
                });
              } else if (instance.getPath && instance.getPath()) {
                foreach(instance.getPath().getArray(), function (latLng) {
                  bounds.extend(latLng);
                });
              } else if (instance.getCenter && instance.getCenter()) {
                bounds.extend(instance.getCenter());
              } else if ( instance.markers && instance.markers() ) {
                  foreach(instance.markers(), function (marker) {
                    bounds.extend(marker.position);
                  });                
              }
            }
          });
        }
      });
      if (!bounds.isEmpty()) {
        map.fitBounds(bounds);
      }
      return true;
    });

add elseif for cluster :

else if ( instance.markers && instance.markers() ) {
                  foreach(instance.markers(), function (marker) {
                    bounds.extend(marker.position);
                  });