HondaKillrsx
HondaKillrsx

Reputation: 349

Removing Google Map Markers

I have an html checkbox that runs an if else javascript function depending on whether it is check or unchecked. When the checkbox is checked it loads the markers called "Fishmark" from the DB. This works fine.

What I need help with is getting the markers to remove when the box is unchecked. I know the if, else statement is correct so it has be with the actual Google API code. I successfully used the setMap(null) on another part of my site so I'm kinda lost as to why its not taking all of the Fishmark markers off the map.

var checkbox = document.getElementById("chbx");     
function addRemoveFishing(){
    if(checkbox.checked==true){
        fishingUrl("XML_Fishing.php", function(data) {
    var xml = data.responseXML;
    var markers = xml.documentElement.getElementsByTagName("marker");
    for (var i = 0; i < markers.length; i++) {
      var title = markers[i].getAttribute("title");
      var water_type = markers[i].getAttribute("water_type");
      var water_depth = markers[i].getAttribute("water_depth");
      var point = new google.maps.LatLng(
          parseFloat(markers[i].getAttribute("lat")),
          parseFloat(markers[i].getAttribute("lng")));
    var bait = markers[i].getAttribute("bait");
    var fish = markers[i].getAttribute("fish");
    var notes = markers[i].getAttribute("notes");

      var Fishhtml = "<b>" + title + "</b> <br/>" + water_type + "</b> <br/>" + water_depth + "</b> <br/>" + bait
      + "</b> <br/>" + fish + "</b> <br/>" + notes;
      var icon = FishingIcon;
     var Fishmark = new google.maps.Marker({
        map: map,
        position: point,
        icon: icon.icon
      });
      bindInfoWindow4(Fishmark, map, Fishinfo, Fishhtml);
                        }
    });
    }
    else {
        Fishmark.setMap(null);
    }
};

Upvotes: 1

Views: 628

Answers (1)

Nils
Nils

Reputation: 2061

You are creating several markers in a for loop. In your else statement you are only removing one (1) marker from the map.

Store the markers in an array for instance and in your else statement make a for loop that calls setMap(null) for each marker.

Upvotes: 2

Related Questions