GoogleMaps Javascript API不显示KML

时间:2016-07-14 17:28:26

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

我正在尝试使用GoogleMaps Javascript API在我的网站上显示KML。 kml托管在:https://sites.google.com/a/consultwilliams.com/kml/files

我正在使用的代码显示地图但不检索KML:

    <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <title>KML Layers</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

     function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 6,
          center: {lat: 41.876, lng: -87.624}
        });

        var ctaLayer = new google.maps.KmlLayer({
          url:'https://sites.google.com/a/consultwilliams.com/kml/files/MILWAUKEE.KML',
          map: map
        });
      }

    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyChJk7GigtgPqgEuKI2Ps7O9ApfTr9PROc&callback=initMap">
    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

文件的网址不正确。 KmlLayer返回的状态为DOCUMENT_NOT_FOUND。这对我有用:

https://sites.google.com/a/consultwilliams.com/kml/files/Milwaukee.kml?attredirects=0&d=1'

代码段

&#13;
&#13;
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 6,
    center: {
      lat: 41.876,
      lng: -87.624
    }
  });

  var ctaLayer = new google.maps.KmlLayer({
    // url:'https://sites.google.com/a/consultwilliams.com/kml/files/MILWAUKEE.KML',
    url: 'https://sites.google.com/a/consultwilliams.com/kml/files/Milwaukee.kml?attredirects=0&d=1',
    map: map
  });
  google.maps.event.addListener(ctaLayer, 'status_changed', function() {
    document.getElementById('status').innerHTML = ctaLayer.getStatus();
  })
}

google.maps.event.addDomListener(window, "load", initMap);
&#13;
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#map {
  height: 100%;
}
&#13;
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="status"></div>
<div id="map"></div>
&#13;
&#13;
&#13;