KML 图层未显示在地图上

KML Layer not displaying on Map

我正在尝试在 Google 地图上显示 KML。地图加载完美,但 KML 根本不显示。另外,我在控制台中没有收到任何错误消息。

我从 gadm.org 下载了一个 .kmz 文件,解压缩并提取了 .kml 文件。

这是我的代码,URL 是我的 KML。 http://locallocal.com/endline/mex2.kml

<script>
  var map;
  var src = 'http://locallocal.com/endline/mex2.kml';

  /**
   * Initializes the map and calls the function that loads the KML layer.
   */
  function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(-19.257753, 146.823688),
      zoom: 2,
      mapTypeId: 'roadmap'
    });
    loadKmlLayer(src, map);
  }

  /**
   * Adds a KMLLayer based on the URL passed. Clicking on a marker
   * results in the balloon content being loaded into the right-hand div.
   * @param {string} src A URL for a KML file.
   */
  function loadKmlLayer(src, map) {
    var kmlLayer = new google.maps.KmlLayer(src, {
      suppressInfoWindows: true,
      preserveViewport: false,
      map: map
    });
    google.maps.event.addListener(kmlLayer, 'click', function(event) {
      var content = event.featureData.infoWindowHtml;
      var testimonial = document.getElementById('capture');
      testimonial.innerHTML = content;
    });
  }
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=[MYKEY]&callback=initMap">

我得到 Kml Status:INVALID_DOCUMENT (link) with that KML. Your KML is too large (it is 5.8 MB), it doesn't meet this requirement from the documentation:

  • 最大提取文件大小(原始 KML、原始 GeoRSS 或压缩 KMZ)3MB

我可以用 geoxml3 加载它,它没有那个限制。

或者我可以将它压缩(将它变成一个 .kmz 文件),它可以工作(它是 1.3 MB):

loading it as a KMZ file

如果它在 kml 中不是无效 xml 并且 kml url 将从浏览器下载然后尝试重命名您的 kml 文件。重命名是唯一有效的解决方案。这不是我的浏览器缓存,因为我清除了很多次。看来 google 必须在 api 的末端进行某种缓存。

朋友们,google地图javascriptAPI,KML图层,限制KML大小为10MB。 超过 10MB,您将在 kmllayer 对象上获得无效文档状态。

您的 KML 必须先加载到 google KML 服务,然后 return 将平铺图像返回给您。 因此您的 KML 大小必须小于 10MB。

https://developers.google.com/maps/documentation/javascript/kmllayer#restrictions