Javascript 谷歌地图:删除标记群集

Javascript 谷歌地图:删除标记群集,javascript,google-maps-api-3,markerclusterer,Javascript,Google Maps Api 3,Markerclusterer,希望使用浮动面板上的单击清除所有标记簇。我可以清除并显示所有标记,但不能显示标记聚集图标。我已经仔细研究了许多类似的问题,但肯定遗漏了一些东西 当我尝试使用下面的代码时,它会返回markerCluster,而不是在clearMarkers上定义的 JS代码: var map; var geocoder; var markerAll; var markers = []; //Code to load the map with center point of Monterey MA functio

希望使用浮动面板上的单击清除所有标记簇。我可以清除并显示所有标记,但不能显示标记聚集图标。我已经仔细研究了许多类似的问题,但肯定遗漏了一些东西

当我尝试使用下面的代码时,它会返回markerCluster,而不是在clearMarkers上定义的

JS代码:

var map;
var geocoder;
var markerAll;
var markers = [];

//Code to load the map with center point of Monterey MA
function initMap() {
    var monterey = {lat: 42.181613, lng: -73.215013};
    map = new google.maps.Map(document.getElementById('map'), {
        zoom: 10,
        center: monterey,
        mapTypeId: google.maps.MapTypeId.HYBRID,
        labels: true,
    });

    //collect customer data and geocoder object - declare geocoder as global
    var cusdata = JSON.parse(document.getElementById('data').innerHTML);
    geocoder = new google.maps.Geocoder();  
    codeAddress(cusdata);

    var allData = JSON.parse(document.getElementById('allData').innerHTML);
    showAllCustomers(allData);

    var searchData = JSON.parse(document.getElementById('searchData').innerHTML);
    showSearchedCustomer(searchData);
    
}

function showAllCustomers(allData) {
    //declare info window variable outside of loop to allow to clear when selecting other markers
    var infoWind = new google.maps.InfoWindow;

    //Create marker clusterer to group data
    var markerCluster = new MarkerClusterer(map, [], {
        imagePath: 'images/m'
      });


    Array.prototype.forEach.call(allData, function(data){
        var content = document.createElement('div');
        var strong = document.createElement('strong');
        
        strong.textContent = [data.lastName + ' ' + data.physicalAddress];
        content.appendChild(strong);

        //add image to infowindow - you are also able to add image path to mysql and then append dynamically
        // var img = document.createElement('img');
        // img.src = 'images/santahat.png';
        // img.style.width = '50px';
        // content.appendChild(img);

        //Create markers for customer locations and customize
        var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
          var markerAll = new google.maps.Marker({
          position: new google.maps.LatLng(data.latitude, data.longitude),
          map: map,
          icon: iconBase + 'homegardenbusiness.png'
        });

        //push markers to marker clusterer
        markerCluster.addMarker(markerAll);
        markers.push(markerAll);

    }) 

}

// Sets the map on all markers in the array.
function setMapOnAll(map) {
    for (let i = 0; i < markers.length; i++) {
      markers[i].setMap(map);
    }
  }
  // Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
    setMapOnAll(null);
    markerCluster.clearMarkers(markerAll);  <----- THIS IS WHAT I TRIED TO ADD TO CLEAR CLUSTERS
}
  
  // Shows any markers currently in the array.
  function showMarkers() {
    setMapOnAll(map);
  }
  
var映射;
var地理编码器;
var Markeral;
var标记=[];
//代码以蒙特利马州为中心点加载地图
函数initMap(){
var monterey={lat:42.181613,lng:-73.215013};
map=new google.maps.map(document.getElementById('map'){
缩放:10,
中心:蒙特利,
mapTypeId:google.maps.mapTypeId.HYBRID,
标签:对,
});
//收集客户数据和geocoder对象-将geocoder声明为全局
var cusdata=JSON.parse(document.getElementById('data').innerHTML);
geocoder=新的google.maps.geocoder();
代码地址(cusdata);
var allData=JSON.parse(document.getElementById('allData').innerHTML);
显示所有客户(所有数据);
var searchData=JSON.parse(document.getElementById('searchData').innerHTML);
showSearchedCustomer(搜索数据);
}
函数showAllCustomers(所有数据){
//在循环外部声明信息窗口变量,以允许在选择其他标记时清除
var infoWind=new google.maps.InfoWindow;
//创建标记聚类器以对数据进行分组
var markerCluster=新的MarkerClusterer(映射,[]{
imagePath:'images/m'
});
Array.prototype.forEach.call(allData,函数(data)){
var content=document.createElement('div');
var strong=document.createElement('strong');
strong.textContent=[data.lastName+''+data.physicalAddress];
内容:儿童(强);
//将图像添加到infowindow—您还可以将图像路径添加到mysql,然后动态追加
//var img=document.createElement('img');
//img.src='images/santahat.png';
//img.style.width='50px';
//内容。附加子项(img);
//为客户位置创建标记并自定义
iconBase变量https://maps.google.com/mapfiles/kml/shapes/';
var markerAll=new google.maps.Marker({
位置:新的google.maps.LatLng(数据.纬度,数据.经度),
地图:地图,
图标:iconBase+“homegardbusiness.png”
});
//将标记器推到标记器群集器
markerCluster.addMarker(markerAll);
markers.push(markerAll);
}) 
}
//在阵列中的所有标记上设置贴图。
函数setMapOnAll(映射){
for(设i=0;imarkerCluster.clearMarkers(markerAll);您需要将markerCluster放入全局范围,以便它可以用于clearMarkers函数(可能是在全局范围中定义的)

代码片段:

var映射;
var地理编码器;
var Markeral;
var标记=[];
//代码以蒙特利马州为中心点加载地图
函数initMap(){
var monterey={
纬度:42.181613,
液化天然气:-73.215013
};
map=new google.maps.map(document.getElementById('map'){
缩放:3,
中心:{
lat:-28.024,
液化天然气:140.887
},
mapTypeId:google.maps.mapTypeId.HYBRID,
标签:对,
});
//收集客户数据和geocoder对象-将geocoder声明为全局
var cusdata=JSON.parse(document.getElementById('data').innerHTML);
geocoder=新的google.maps.geocoder();
//代码地址(cusdata);
var allData=JSON.parse(document.getElementById('allData').innerHTML);
所有数据=位置;
显示所有客户(所有数据);
var searchData=JSON.parse(document.getElementById('searchData').innerHTML);
//搜索数据=位置;
//showSearchedCustomer(搜索数据);
}
var-markerCluster;
函数showAllCustomers(所有数据){
//创建标记聚类器以对数据进行分组
markerCluster=新的MarkerClusterer(映射,[]{
图像路径:“https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m",
});
//在循环外部声明信息窗口变量,以允许在选择其他标记时清除
var infoWind=new google.maps.InfoWindow;
Array.prototype.forEach.call(allData,函数(data)){
var content=document.createElement('div');
var strong=document.createElement('strong');
strong.textContent=[data.lastName+''+data.physicalAddress];
内容:儿童(强);
//将图像添加到infowindow—您还可以将图像路径添加到mysql,然后动态追加
//var img=document.createElement('img');
//img.src='images/santahat.png';
//img.style.width='50px';
//内容。附加子项(img);
//为客户位置创建标记并自定义
iconBase变量https://maps.google.com/mapfiles/kml/shapes/';
var markerAll=new google.maps.Marker({
位置:新的google.maps.LatLng(数据.纬度,数据.经度),
地图:地图,
//图标:iconBase+“homegardbusiness.png”
});
//将标记器推到标记器群集器
markerCluster.addMarker(markerAll);
markers.push(markerAll);
})
google.maps.event.addDomListener(document.getElementById('btn'),'click',clearMarkers);
}
//在阵列中的所有标记上设置贴图。
函数setMapOnAll(映射){
for(设i=0;ivar markerCluster;
function showAllCustomers(allData) {
    //Create marker clusterer to group data
    markerCluster = new MarkerClusterer(map, [], {
        imagePath:
      "https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m",
      });