Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing balloons google maps: Uncaught ReferenceError: markers is not defined

I have created a toggle to clear my map of all markers. When the toggle is clicked it calls getMapVisit()

I am getting this error when I click the toggle:

Uncaught ReferenceError: markers is not defined 

in this javascript code on the line marked below by stars:

function load() {
      var map = new google.maps.Map(document.getElementById("map"), {

        zoom: 13,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("getMapMarkers.php", function(data) {
        var bounds = new google.maps.LatLngBounds();
        var xml = data.responseXML;
        var markers = xml.documentElement.getElementsByTagName("marker");
        for (var i = 0; i < markers.length; i++) {
          var name = markers[i].getAttribute("name");
          var type = markers[i].getAttribute("type");
          var point = new google.maps.LatLng(
              parseFloat(markers[i].getAttribute("lat")),
              parseFloat(markers[i].getAttribute("lng")));
          var html = "<b>" + name + "</b> <br/>" ;
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon,
            shadow: icon.shadow
          });
          bindInfoWindow(marker, map, infoWindow, html);
          bounds.extend(point);

        }
        map.fitBounds(bounds);
      });


    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }

    function downloadUrl(url, callback) {
      var request = window.ActiveXObject ?
          new ActiveXObject('Microsoft.XMLHTTP') :
          new XMLHttpRequest;

      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          request.onreadystatechange = doNothing;
          callback(request, request.status);
        }
      };

      request.open('GET', url, true);
      request.send(null);
    }

    function getMapVisit()
    {
        alert("in clear new clear al lmarkers");
        //clear markers
        clearOverlays();
        markers = [];

        return false;
    }

function clearOverlays() {
        setAllMap(null);
      }

function setAllMap(map) {
    for (var i = 0; i < markers.length; i++) {  **************
        markers[i].setMap(map);
    }
}

    function doNothing() {}

My php which is displaying the map and toggle is:

$page->content .= '<p> <h3> Your Beer Map 
<form class="form-horizontal pull-right" style="width: 400px !important; padding-right: 20px !important;">
    <div class="control-group">
        <div class="controls btn disabled switch switch-two">
            <input id="week9" name="view" type="radio" checked>
            <label for="week9" onclick="getMapDrank()">Drank Beer From</label>

            <input id="month10" name="view" type="radio">   
            <label for="month10" onclick="getMapVisit()">Visited</label>

            <span class="slide-button btn btn-warning"></span>
        </div>
    </div>
</form>
</h3> 



</p>';  




$page->content .= '<div id="map" style="width: 700px; height: 500px"></div>';
like image 643
Mike Avatar asked Nov 27 '25 16:11

Mike


1 Answers

markers is a local variable inside the downloadUrl() callback function. It looks like you want it to be a global variable. You might try adding this line at the very top, outside of any function:

var markers = [];

and change this line inside the downloadUrl() callback from:

var markers = xml.documentElement.getElementsByTagName("marker");

to:

markers = xml.documentElement.getElementsByTagName("marker");

That may not be the only problem in the code, but it's one problem anyway.

like image 104
Michael Geary Avatar answered Nov 30 '25 05:11

Michael Geary



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!