Even after removing circles on the Google Map, the labels still persist. I use InfoBox to display labels on circles like this:
myOptions.content = datadetail[i].Count;
var ibLabel = new InfoBox(myOptions);
ibLabel.open(map);
I am trying to clear the circles in the ZoomIn/ZoomOut event under certain conditions. The circles are cleared but the labels remain visible. Here is my complete script. Thank you.
<script>
var gmarkers = [];
var gmlist = [];
var fill_color_val = '#3888ff';
var labelText = "1";
var currentZoom = 0;
$(function () {
function initialize_map() {
var mapProp = {
minZoom: 11,
maxZoom: 20,
center: new google.maps.LatLng(16.7803886, 96.1844148),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map"), mapProp);
bindMarkerOnMap("level_0");
}
initialize_map();
map.addListener('zoom_changed', function () {
currentZoom = map.getZoom();
if (currentZoom <= 12) {
ClearMapMaker();
console.log(map.getZoom());
bindMarkerOnMap("level_0");
} else if (currentZoom >= 13) {
console.log("level_1 fired + " + currentZoom);
ClearMapMaker()
console.log(map.getZoom());
bindMarkerOnMap("level_1");
}else if (currentZoom >= 15) {
bindMarkerOnMap("level_2");
}
});
});
function bindMarkerOnMap(level) {
console.log(level);
if (level == "level_0") {
console.log("level_0 binding");
for (var i = 0; i < data.length; i++) {
var latlng = new google.maps.LatLng(data[i].Lat, data[i].Long);
var myOptions = {
content: labelText,
boxStyle: {
border: "none",
textAlign: "center",
fontSize: "10pt",
width: "50px"
},
disableAutoPan: true,
pixelOffset: new google.maps.Size(-25, -5),
position: latlng,
closeBoxURL: "",
isHidden: false,
pane: "floatPane",
enableEventPropagation: true
};
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 1600,
fillColor: fill_color_val,
strokeColor: '#69DAED',
strokeWeight: 2,
fillOpacity: 1,
});
var marker = new google.maps.Marker({
position: latlng,
title: data[i].Author,
draggable: false,
map: map
});
marker.setVisible(false);
circle.bindTo('center', marker, 'position');
google.maps.event.addListener(circle, 'click', getInfoCallback(map, latlng));
myOptions.content = data[i].Count;
var ibLabel = new InfoBox(myOptions);
ibLabel.open(map);
gmarkers.push(circle);
gmlist.push(marker);
}
} else if (level == "level_1") {
console.log("level_1 binding");
for (var i = 0; i < datadetail.length; i++) {
var latlng = new google.maps.LatLng(datadetail[i].Lat, datadetail[i].Long);
var myOptions = {
content: labelText,
boxStyle: {
border: "none",
textAlign: "center",
fontSize: "10pt",
width: "50px"
},
disableAutoPan: true,
pixelOffset: new google.maps.Size(-25, -5),
position: latlng,
closeBoxURL: "",
isHidden: false,
pane: "floatPane",
enableEventPropagation: true
};
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 800,
fillColor: fill_color_val,
strokeColor: '#69DAED',
strokeWeight: 2,
fillOpacity: 1,
});
var marker = new google.maps.Marker({
position: latlng,
title: datadetail[i].Author,
draggable: false,
map: map
});
marker.setVisible(false);
circle.bindTo('center', marker, 'position');
google.maps.event.addListener(circle, 'click', getInfoCallback(map, latlng));
myOptions.content = datadetail[i].Count;
var ibLabel = new InfoBox(myOptions);
ibLabel.open(map);
gmarkers.push(circle);
gmlist.push(marker);
}
}
}
function getInfoCallback(map, latlng) {
return function () {
map.setZoom(15);
map.setCenter(latlng);
};
}
var data = [ ... ]; // Data Array
var datadetail = [ ... ]; // Detail Data Array
function ClearMapMaker() {
console.log(gmlist);
for (i = 0; i < gmlist.length; i++) {
gmlist[i].setMap(null);
}
gmlist = [];
console.log(gmarkers);
for (i = 0; i < gmarkers.length; i++) {
gmarkers[i].setOptions({ fillOpacity: 0, strokeOpacity: 0 });
gmarkers[i].setMap(null);
}
}
</script>