0

我正在使用 Google Maps API 和 KML 创建交互式校园地图。我有一张带有地面叠加层和一层 KML 标记的地图。我试图弄清楚如何让 KML 层切换。我目前将其设置为通过复选框进行切换,但仅在您第一次单击复选框时才会关闭。任何后续点击都不会执行任何操作。KML 层就消失了。我有一种感觉,这可能是一个简单的 javascript 修复程序,但我是 javascript 新手,我无法弄清楚。有人知道我在做什么错吗?在此先感谢您的帮助。

这是我的所有代码:

<script type="text/javascript">
  function initialize() {

  var map;
  var omaha = new google.maps.LatLng(41.265437, -95.947405);

  var MY_MAPTYPE_ID = 'blue';

  var stylez = [
    {
      featureType: "all",
      stylers: [
        { hue: "#004A96" },
      ]
    },
    {
      featureType: "all",
      elementType: "labels",
      stylers: [
        { hue: "#000000" },
      ]
    },
        {
      featureType: "road",
      elementType: "local",
      stylers: [
        { hue: "#24356B" },
        { saturation: 55 },
        { lightness: 20 }
      ]
    },
    {
      featureType: "poi.school",
      elementType: "geometry",
      stylers: [
        { hue: "#24356B" },
        { saturation: 55 },
        { lightness: 20 }
      ]
    }
  ];

  var imageBounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(41.2599,-95.9601),
      new google.maps.LatLng(41.2718,-95.9367));

  var mapOptions = {
    zoom: 15,
    center: omaha,
    mapTypeControlOptions: {
        mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID]
    },
    mapTypeId: MY_MAPTYPE_ID
  };

  var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

  var styledMapOptions = {
    name: "Blue"
  };

  var jayzMapType = new google.maps.StyledMapType(stylez, styledMapOptions);

  map.mapTypes.set(MY_MAPTYPE_ID, jayzMapType);

  var oldmap = new google.maps.GroundOverlay(
      "http://www.mcography.com/beta/CampusMap.png",
      imageBounds);
  oldmap.setMap(map);

  var kmlLayer01URL = 'http://www.mcography.com/beta/CUADA.kml';
  var kmlOptions = {
            preserveViewport: 1
    };
    kmlLayer01 = new google.maps.KmlLayer(kmlLayer01URL, kmlOptions);
    kmlLayer01.setMap(map); 
    // initially show KML Layer 01
    document.getElementById('show_hide_KML_Layer_01').checked = true; 

}

function toggleKMLLayer01() {
    if (!document.getElementById('show_hide_KML_Layer_01').checked)
      kmlLayer01.setMap(null);
    else
      kmlLayer01.setMap(map); }

</script>
</head>

<body onload="initialize()">

    <p><input type="checkbox" id="show_hide_KML_Layer_01" onclick="toggleKMLLayer01();" />ADA Layer</p> 
    <div id="map_canvas"></div>

</body>
4

1 回答 1

2

如果没有你的代码的顶部,我不能完全确定,但我的直觉是你需要使变量map全局,否则toggle不会。我写了以下内容,它有效:setMapmap

  var map;

  function initialize() {
    var mapOptions = { center: new google.maps.LatLng(0.0, 0.0), zoom: 2,
      mapTypeId: google.maps.MapTypeId.ROADMAP };

    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
    var kmlLayer01URL = 'http://www.mcography.com/beta/CUADA.kml';
    var kmlOptions = {
      preserveViewport: 1
    };
    kmlLayer01 = new google.maps.KmlLayer(kmlLayer01URL, kmlOptions);
    kmlLayer01.setMap(map); 
    // initially show KML Layer 01
    document.getElementById('show_hide_KML_Layer_01').checked = true; 
  }

  function toggleKMLLayer01() {
    if (!document.getElementById('show_hide_KML_Layer_01').checked)
      kmlLayer01.setMap(null);
    else
      kmlLayer01.setMap(map); 
  }
于 2012-05-30T15:02:26.947 回答