谷歌地图禁用集群点击缩放

Mis*_*ian -1 javascript google-maps markerclusterer

我有一张带有基于聚类的标记的地图。我想要一种方法,当用户单击具有超过 100 个标记的集群时,不要放大并执行其他操作,例如打开弹出窗口;否则只是去簇。

在加载页面上,我正在调用服务并获取包含位置的客户列表。我正在将客户传递给集群功能。现在,当用户单击集群时,我想设置一些条件。如果为真,则打开一个弹出窗口,但不要放大两个除簇。我无法做到这一点。这是代码

客户.html

<div [hidden]="!MapView" style="height: 100%" #map id="map"></div>
Run Code Online (Sandbox Code Playgroud)

客户.ts

    @ViewChild('map') mapElement: ElementRef;

    ngOnInit() {


            this.initMap();

        }

    initMap = () => {

            this._customerservice.GetCustomersWithLocations().subscribe(res => {

                if (res != null || res != undefined) {

                    this.CustomersLocation = res;

                    let latLng = new google.maps.LatLng(-31.563910, 147.154312);

                    let mapOptions = {
                        center: latLng,
                        zoom: 6,
                        mapTypeId: google.maps.MapTypeId.TERRAIN,
                        fullscreenControl: false
                    }

                    setTimeout(() => { // LOAD THE MAP FIRST
                        this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
                    }, 1000);

                    setTimeout(() => { //LOAD THE CLUSTER
                        this.mapCluster.addCluster(res, this.map);
                    }, 3000);


                }

            }, error => {
            }, () => {
           });

    }
Run Code Online (Sandbox Code Playgroud)

GoogleMapsCluster.ts

addCluster(Customers: any, map) {
    //console.log(Customers);

    if (google.maps) {

      //Convert locations into array of markers
      let markers = Customers.map((location) => {

        var marker = new google.maps.Marker({
          position: location.loc,
          label: location.name,
        });

        return marker;

      });

      this.markerCluster = new MarkerClusterer(map, markers, { imagePath: 'assets/m' });

      google.maps.event.addListener(this.markerCluster, 'clusterclick', (cluster) => {

        var markers = cluster.getMarkers();

        var CustomerInsideCluster = [];

        for (var i = 0; i < markers.length; i++) {
          CustomerInsideCluster.push({BusinessName: markers[i].label})
        }
        this.OpenCustomerDetails(CustomerInsideCluster);
        //I OPEN A POPUP HERE. I DONT WANT TO ZOOM IN TO DECLUSTER. AT THEN END IF THE CLUSTER HAS MORE THAN 100 CUSTOMERS I DONT WANT TO ZOOM IN WHEN I CLICK ON IT.
        //map.setZoom(8); GIVING ME ERROR

      });


    } else {
      console.warn('Google maps needs to be loaded before adding a cluster');
    }

  }
  OpenCustomerDetails(Customers: any) {
    let popover = this.popoverCtrl.create(MapPopover, { Customers: Customers }, { cssClass: 'custom-popover' });
    popover.present({
      ev: event
    });
  }
Run Code Online (Sandbox Code Playgroud)

rag*_*hes 5

这是一个可能的解决方案。只需将markerClusterer属性设置zoomOnClickfalse.

var markerCluster = new MarkerClusterer(map, markers, { 
    imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m',
    zoomOnClick: false
  });
Run Code Online (Sandbox Code Playgroud)

在您的clusterclick事件中,将counter对象传递给 ,callback function并根据您的要求使用条件语句,打开一个infowindow(或者,如果您愿意,则为其他方式)。

markerCluster.addListener('clusterclick', function(cluster){
      if (markers.length > 5){ // change #5 if you need to test different scenarions
            infowindow.setPosition(cluster.getCenter());
            infowindow.setContent("Simple Test");
            infowindow.open(map);

            }
Run Code Online (Sandbox Code Playgroud)

如果标记比所需要的参数少跌多,重置zoomOnClicktrue再次使用以下mapclusterObject方法:

  else {
     markerCluster.zoomOnClick = true;
     map.fitBounds(cluster.getBounds());

        } 
Run Code Online (Sandbox Code Playgroud)

此 JSBIN 中的概念证明