在google maps API v3中切换多个KML / KML图层

时间:2013-07-01 16:45:32

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

我一直想在Google地图中切换KML图层。我已经设置了这样,只有一个图层显示没有任何切换按钮。我在网上跟踪了很多例子,但似乎都没有。我到目前为止设置的代码如下:

<html>
<head>
<script type="text/javascript">
var layers=[];
layers[0] = new  google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5254.kml',
 {preserveViewport: true});
layers[1] = new google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5938.kmz',
{preserveViewport: true});
var map;

function initialize() {
    var latlng = new google.maps.LatLng(52.5028910, 13.41032740);
    var myOptions = {
            zoom: 3,
            center: latlng,
            mapTypeIds: google.maps.MapTypeId.ROADMAP
    }
            map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
    }

function toggleLayers(i)
{
        if(layers[i].getmap()==null){
                kmlLayer01.setMap(null);
        }
        else{
                    layers[i].setMap(null);
            }
}


</script> </head>
<body onload="initialize()">
Layer1 <input type="checkbox" id="layer_01" onclick="toggleLayers(0);"/>
Layer2 <input type="checkbox" id="layer_02" onclick="toggleLayers(1);"/>
<div id="map_canvas"></div>
</body>
</html>

请帮忙!

2 个答案:

答案 0 :(得分:8)

  1. 发布的代码不包含API,但缺少:

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    
  2. 有一些拼写错误:

    • javascript是区分大小写的getmap不是有效的方法名,应该是getMap。
    • 未定义kmlLayer01,可能应该是layers [i]

      <html>
      <head>
      <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
      
      <script type="text/javascript">
      var layers=[];
      
      layers[0] = new  google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5254.kml',
      {preserveViewport: true});
      
      layers[1] = new google.maps.KmlLayer('http://wrf1.geology.um.maine.edu/bipush/kml/plot_5938.kmz',
      {preserveViewport: true});
      var map;
      
      function initialize() {
          var latlng = new google.maps.LatLng(52.5028910, 13.41032740);
          var myOptions = {
                  zoom: 3,
                  center: latlng,
                  mapTypeIds: google.maps.MapTypeId.ROADMAP
          }
          map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
      }
      
      function toggleLayers(i)
      {
      
        if(layers[i].getMap()==null) {
           layers[i].setMap(map);
        }
        else {
           layers[i].setMap(null);
        }
        document.getElementById('status').innerHTML += "toggleLayers("+i+") [setMap("+layers[i].getMap()+"] returns status: "+layers[i].getStatus()+"<br>";
      }
      
      
      </script>
      </head>
      <body onload="initialize()">
      Layer1 <input type="checkbox" id="layer_01" onclick="toggleLayers(0);"/>
      Layer2 <input type="checkbox" id="layer_02" onclick="toggleLayers(1);"/>
      <div id="map_canvas" style="height:500px; width:600px;"></div>
      <div id="status"></div>
      </body>
      </html>
      
  3. working example

    代码段

    var layers = [];
    
    layers[0] = new google.maps.KmlLayer("http://www.geocodezip.com/geoxml3_test/utah-hunt_com_DeerSouthern_kml.xml",
      /*'http://wrf1.geology.um.maine.edu/bipush/kml/plot_5254.kml', original KML no longer available */
      {
        preserveViewport: true
      });
    
    layers[1] = new google.maps.KmlLayer("http://www.geocodezip.com/geoxml3_test/utah-hunt_com_DeerSoutheastern_kml.xml",
      /* 'http://wrf1.geology.um.maine.edu/bipush/kml/plot_5938.kmz', original KML no longer available */
      {
        preserveViewport: true
      });
    var map;
    
    function initialize() {
      var latlng = new google.maps.LatLng(38, -112 /*52.5028910, 13.41032740 */ );
      var myOptions = {
        zoom: 5,
        center: latlng,
        mapTypeIds: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }
    
    function toggleLayers(i) {
    
      if (layers[i].getMap() == null) {
        layers[i].setMap(map);
      } else {
        layers[i].setMap(null);
      }
      document.getElementById('status').innerHTML += "toggleLayers(" + i + ") [setMap(" + layers[i].getMap() + "] returns status: " + layers[i].getStatus() + "<br>";
    }
    
    google.maps.event.addDomListener(window, 'load', initialize);
    <script src="http://maps.google.com/maps/api/js"></script>
    Layer1
    <input type="checkbox" id="layer_01" onclick="toggleLayers(0);" />Layer2
    <input type="checkbox" id="layer_02" onclick="toggleLayers(1);" />
    <div id="map_canvas" style="height:500px; width:600px;"></div>
    <div id="status"></div>

答案 1 :(得分:0)

猎食,

如果您有两个不同的地图,请将地图的变量传递给函数,例如

function toggleLayers(i,theMap)
{

 if(layers[i].getMap()==null) {
     layers[i].setMap(theMap);
  }
  else {
     layers[i].setMap(null);
}
  document.getElementById('status').innerHTML += "toggleLayers("+i+") [setMap("+layers[i].getMap()+"] returns status: "+layers[i].getStatus()+"<br>";
}

LEA。