如何从现有的谷歌地图获取图层ID

时间:2015-07-12 00:06:22

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

我尝试使用google V3 API切换在maps.google.com上创建的现有地图的图层。

使用.setMap函数切换(显示/隐藏)图层,使用map / null作为参数。

我的问题是我找不到获取现有图层列表的方法。所以我在mapLayerNames中对这些值进行了硬编码,但我更愿意从地图'中获取它们。变量。我从文档中找不到哪个API?

这是我的代码:盖子是我在网址中寻找的图层...

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>KML Layers</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>


var mapLayerNames = [
    "http://www.google.com/maps/d/u/1/kml?mid=z_wfVyI764I4.kPvsE6js2GAg&lid=z_wfVyI764I4.kIQKW-gOFhmU",
    "http://www.google.com/maps/d/u/1/kml?mid=z_wfVyI764I4.kPvsE6js2GAg&lid=z_wfVyI764I4.k2u4vYNOkUIA",
    "http://www.google.com/maps/d/u/1/kml?mid=z_wfVyI764I4.kPvsE6js2GAg&lid=z_wfVyI764I4.kcPBYmgvyhbI"
    ];

var map;
var ctaLayer = [];

function initialize() {
  var paris = new google.maps.LatLng(43.5742118,1.4538968);
  var mapOptions = {
    zoom: 11,
    center: paris
  }

  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  for (var i=0; i<mapLayerNames.length; i++) {

    ctaLayer = new google.maps.KmlLayer({
      url: mapLayerNames[i]
    });
    ctaLayer.setMap(map);
  }
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

0 个答案:

没有答案