清除气球谷歌地图:未捕获的ReferenceError:未定义标记

时间:2013-03-28 04:57:17

标签: javascript ajax google-maps google-maps-api-3

我创建了一个切换按钮来清除所有标记的地图。单击切换时,它调用getMapVisit()

单击切换时出现此错误:

Uncaught ReferenceError: markers is not defined 

这个javascript代码在下面标有星号的行上:

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() {}

显示地图和切换的我的php是:

$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>';

1 个答案:

答案 0 :(得分:1)

markersdownloadUrl()回调函数中的局部变量。看起来你希望它成为一个全局变量。您可以尝试在任何函数之外的最顶部添加此行:

var markers = [];

并在downloadUrl()回调中更改此行:

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

为:

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

这可能不是代码中唯一的问题,但无论如何都是一个问题。