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

1 回答 1

1

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

var markers = [];

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

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

至:

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

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

于 2013-03-28T05:36:07.623 回答