【问题标题】:KML Layer not displaying on MapKML 图层未显示在地图上
【发布时间】:2017-02-10 00:29:46
【问题描述】:

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

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

这是我的代码,以及我的 KML 的 URL。 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">

【问题讨论】:

标签: javascript google-maps kml


【解决方案1】:

我使用该 KML 获得 Kml Status:INVALID_DOCUMENT (link)。您的 KML 太大(为 5.8 MB),不符合 the documentation 的要求:

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

我可以用没有这个限制的geoxml3 加载它。

或者我可以压缩它(使其成为 .kmz 文件),它可以工作(它是 1.3 MB):

loading it as a KMZ file

【讨论】:

    【解决方案2】:

    如果 kml 中的 xml 不是无效的,并且 kml url 将从浏览器下载,则尝试重命名您的 kml 文件。重命名是唯一可行的解​​决方案。这不是我的浏览器缓存,因为我清除了很多次。谷歌似乎必须在他们的最后使用 api 进行某种缓存。

    【讨论】:

      【解决方案3】:

      我的朋友,google maps javascript API,KML 层,将 KML 大小限制为 10MB。 超过 10MB,您将在 kmllayer 对象上获得无效的文档状态。

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

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

      【讨论】:

        猜你喜欢
        • 2019-02-08
        • 2019-08-06
        • 2016-01-15
        • 2019-03-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多