谷歌地图:切换后标记不再出现

时间:2014-10-15 12:09:03

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

我是谷歌地图的初学者。

我正在尝试根据类型切换谷歌地图标记。 当我取消选中复选框时,标记会消失,再次检查时,标记不会显示在地图上。

我尝试在toggleGroup函数中将setmap更改为setvisible,但这也没有用。

    </style>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
    <title>Beautiful India</title>
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js"></script>
    <script type="text/javascript">
    //<![CDATA[

    var customIcons = {
      restaurant: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png'
      },
      bar: {
        icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png'
      }
    };

    function load() {
      var map = new google.maps.Map(document.getElementById("map"), {
        center: new google.maps.LatLng(47.6145, -122.3418),
        zoom: 13,
        mapTypeId: 'roadmap'
      });
      var infoWindow = new google.maps.InfoWindow;

      // Change this depending on the name of your PHP file
      downloadUrl("parsingxml.php", function(data) {
        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 address = markers[i].getAttribute("address");
          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/>" + address;
          html = "<div style=\"position: relative; float: left; width: 225px; height: 80px; border: 0px coral solid;\">" + html + "</div>"; 
          var icon = customIcons[type] || {};
          var marker = new google.maps.Marker({
            map: map,
            position: point,
            icon: icon.icon
          });
          markerGroups[type].push(marker);
          bindInfoWindow(marker, map, infoWindow, html);
        }
      });
    }

    function bindInfoWindow(marker, map, infoWindow, html) {
      google.maps.event.addListener(marker, 'click', function() {
        infoWindow.setContent(html);
        infoWindow.open(map, marker);
      });
    }
     var markerGroups = { "bar": [], "restaurant": [] };
    function toggleGroup(type) {
    for (var i = 0; i < markerGroups[type].length; i++) {
    var marker = markerGroups[type][i];
    if (marker.getMap()==null) {
    marker.setVisible(true);
    } else {
    marker.setVisible(false);
    }
    }
    }


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

    //]]>

  </script>

  </head>

  <body onload="load()">

  <div id="panel">

    <input type="checkbox" id="bar"  onclick="toggleGroup('bar')"CHECKED/>

  bar 
      <input type="checkbox" id="bar"  onclick="toggleGroup('restaurant')"CHECKED/>

  restaurant 

    </div>
    <div id="map" </div>
  </body>

</html>

1 个答案:

答案 0 :(得分:1)

您的问题:

您在这里混合了两件不同的东西:getMap()getVisible()

使用方法:

要在地图上渲染标记,请使用:

marker.setMap(map); // where "map" is your map instance

从地图中删除标记:

marker.setMap(null);

显示隐藏的标记:

marker.setVisible(true);

隐藏标记:

marker.setVisible(false);

如何解决:

您应该相应调整toggleGroup功能。

<强>详细信息:

marker.setMap(null);
marker.setVisible(true); // marker will not be shown since it's not on the map anymore