KML图层未在地图

时间:2017-02-10 00:29:47

标签: javascript google-maps kml

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

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

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

3 个答案:

答案 0 :(得分:3)

我使用该KML获得Kml状态:INVALID_DOCUMENT(link)。你的KML太大了(它是5.8 MB),它不能满足the documentation的这个要求:

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

我可以使用geoxml3加载它,但没有限制。

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

loading it as a KMZ file

答案 1 :(得分:0)

如果kml中的xml无效,并且kml网址将从浏览器下载,请尝试重命名您的kml文件。重命名是唯一有效的解决方案。它不是我的浏览器缓存,因为我清理了很多次。似乎谷歌必须使用api对它们进行某种缓存。

答案 2 :(得分:0)

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

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

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

enter image description here

相关问题