I've been trying to toggle the display of a series of dynamically added google maps markers via a dynamically created checkbox for awhile now. I've been able to toggle only one of the points, but unsuccessful at all the others.
markers(the markers will be dynamically added via location updates)
mapPoints.push(new google.maps.LatLng(33.730362 , -85.792725));
names.push("Alpha");
times.push("1425059747829");
colors.push("Red");
mapPoints.push(new google.maps.LatLng(33.7304572 , -85.792498));
names.push("Alpha");
times.push("1425059747829");
colors.push("Red");
mapPoints.push(new google.maps.LatLng(33.7304346 , -85.792634));
names.push("Alpha");
times.push("1425059747829");
colors.push("Red");
mapPoints.push(new google.maps.LatLng(33.73041 , -85.79264));
names.push("Alpha");
times.push("1425059172108");
colors.push("Blue");
mapPoints.push(new google.maps.LatLng(33.730312 , -85.792654));
names.push("Delta");
times.push("1425059747723");
colors.push("Blue");
mapPoints.push(new google.maps.LatLng(33.73023 , -85.79246));
names.push("Foxtrot");
times.push("1425059172145");
colors.push("Purple");
mapPoints.push(new google.maps.LatLng(33.72476 , -85.788185));
names.push("Golf");
times.push("1425050587395");
colors.push("Green");
//remove duplicates from names array and pass them to updateCheckbox
for (var i=0; i<names.length; i++) {
if (names[i] !=names[i-1]){
updateCheckbox(names[i],markers[i]);
}
}
Now here is the function to dynamically create a checkbox based on squad names and toggle visibility for all the squads associated with the name:
function updateCheckbox(names,markers){
var checkbox = $("#checkBoxes");
//check if names return null if names !=null create a dynamic list of checkboxes
//based on live squads
if(names!=null){
var $ctrl = $('<input/>').attr({type:'checkbox', checked:'yes',name:'chk'});
$("#checkBoxes").append($ctrl);
//designate squad name to each checkbox
$($ctrl).after(names);
console.log(names);
// toggle display of the squads
}
console.log(markers);
$($ctrl).click(function(){
if(this.checked)
{
if (markers)
{
markers.setVisible(true);
}
}
else{
if (markers)
{
markers.setVisible(false);
}
}
});
}
When I click a the checkbox associated with the squads name it only checks off marker instead of all. Any ideas?
Aucun commentaire:
Enregistrer un commentaire