javascript - 我试图让每个澳大利亚州都出现在谷歌地图上,但只有 3 个是。这是 KML 文件的大小问题吗?

标签 javascript google-maps-api-3 kml

我希望有人能帮我解决这个问题。通过链接到我已上传到 Google 协作平台的 KML 文件,我可以获取在 Google map 上显示的 NT、SA 和 ACT 的选民数据,但文件较大的州没有显示。我虽然使用这个脚本可以解决大小问题。这是带有链接的代码:

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

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/ACT.kml',
      map: map
    });

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/NT.kml',
      map: map
    });

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/SA.kml',
      map: map
    });

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/TAS.kmz',
      map: map
    });

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/VIC.kml',
      map: map
    });

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/WA.kml',
      map: map
    });

    var ctaLayer = new google.maps.KmlLayer({
      url: 'http://sites.google.com/site/kmltestsitey/kml-page/NSW.kml',
      map: map
    });
}

如果是尺寸问题,有办法解决吗?

最佳答案

相关问题:

如果您检查图层的 KmlStatus,您将看到:

  • 状态 ACT=OK
  • 状态 NT=OK
  • 状态 SA=OK
  • 状态 WA=INVALID_DOCUMENT
  • status TAS=INVALID_DOCUMENT(看起来这不是一个 kmz 文件,它是一个重命名为 .kmz 的 .kml 文件)
  • status VIC=INVALID_DOCUMENT(此文件包含无效坐标,<coordinates>-167.827099999879,2407515.604 -5.93339999997988,2407479.8227,第二个坐标,纬度,必须介于 +90 和 -90 之间,它比那个大得多,可能是坐标转换失败)
  • 状态 NSW=INVALID_DOCUMENT

来自 the documentation :

Maximum fetched file size (raw KML, raw GeoRSS, or compressed KMZ)
3MB
Maximum uncompressed KML file size
10MB
Maximum number of network Links
10
Maximum number of total document-wide features
1,000
Number of KML layers
There is a limit on the number of KML Layers that can be displayed on a single Google Map. If you exceed this limit, none of your layers will appear on the map, and an error will be reported in your web browser's JavaScript console. The limit is based on a combination of the number of KMLLayer classes created and the total length of all the URLs used to create those layers. Each new KMLLayer you create will take up a portion of the limit for the layer and a further portion of the limit depending on the length of the URL where the KML file was loaded from. Consequently, the number of layers you can add will vary by application; on average, you should be able to load between 10 and 20 layers without hitting the limit. If you still hit the limit, use a URL shortner (such as https://goo.gl) to shorten the KML URLs. Alternatively, create a single KML file consisting of NetworkLinks to the individual KML URLs.

文件大小

  • 6,503,418 TAS.kmz(太大,大于 3MB)
  • 8,353,172 VIC.kml(太大,大于 3MB)
  • 5,994,333 WA.kml(太大,大于 3MB)
  • 13,325,408 NSW.kml(太大,大于 3MB)
  • 195,125 ACT.kml
  • 1,820,578 新台币.kml
  • 2,654,105 SA.kml

作为一种变通方法,您可以尝试压缩 VIC.kml、WA.kml 和 NSW.kml(压缩为 .kmz 文件),看看这是否会使它们小于 3MB。

  • 3,540,878 NSW.kmz
  • 1,512,043 西澳大利亚.kmz
  • 3,324,367 VIC.kmz

这适用于 WA 但不适用于 VIC 和 NSW。所以需要想办法把TAS、VIC和NSW的内容变小,或者用FusionTablesLayer (this example uses the Natural Earth data set)或第三方解析器来显示它们。

example fiddle

代码片段:

var map;

function initMap() {
  map = new google.maps.Map(document.getElementById('map'), {
    zoom: 11,
    center: {
      lat: -35.2809,
      lng: 149.1300
    }
  });
  var bounds = new google.maps.LatLngBounds();

  var ctaLayerACT = new google.maps.KmlLayer({
    url: 'http://sites.google.com/site/kmltestsitey/kml-page/ACT.kml',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerACT, "ACT", bounds);

  var ctaLayerNT = new google.maps.KmlLayer({
    url: 'http://sites.google.com/site/kmltestsitey/kml-page/NT.kml',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerNT, "NT", bounds);

  var ctaLayerSA = new google.maps.KmlLayer({
    url: 'http://sites.google.com/site/kmltestsitey/kml-page/SA.kml',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerSA, "SA", bounds);

  var ctaLayerTAS = new google.maps.KmlLayer({
    url: 'http://sites.google.com/site/kmltestsitey/kml-page/TAS.kmz',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerTAS, "TAS", bounds);

  var ctaLayerVIC = new google.maps.KmlLayer({
    url: 'http://www.geocodezip.com/geoxml3_test/kmz/VIC.kmz',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerVIC, "VIC", bounds);

  var ctaLayerWA = new google.maps.KmlLayer({
    url: 'http://www.geocodezip.com/geoxml3_test/kmz/WA.kmz',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerWA, "WA", bounds);

  var ctaLayerNSW = new google.maps.KmlLayer({
    url: 'http://www.geocodezip.com/geoxml3_test/kmz/NSW.kmz',
    map: map,
    preserveViewport: true
  });
  checkStatus(ctaLayerNSW, "NSW", bounds);
}
google.maps.event.addDomListener(window, "load", initMap);

var bndsRect;

function checkStatus(layer, idStr, bounds) {
  google.maps.event.addListener(layer, 'status_changed', function() {
    console.log("status " + idStr + "=" + layer.getStatus());
  });
  google.maps.event.addListener(layer, 'defaultviewport_changed', function() {
    var rect = new google.maps.Rectangle({
      bounds: layer.getDefaultViewport(),
      fillOpacity: 0.2,
      strokeOpacity: 0.2,
      map: map
    });
    // if (bounds.isEmpty()) bounds = layer.getDefaultViewport();
    // else 
    bounds.union(layer.getDefaultViewport());
    console.log("bounds after " + idStr + "=" + bounds.toUrlValue())
    if (!bndsRect) {
      bndsRect = new google.maps.Rectangle({
        bounds: bounds,
        fillColor: "blue",
        fillOpacity: 0.2,
        strokeColor: "blue",
        strokeOpacity: 0.2,
        map: map
      });
      // map.fitBounds(bounds);
    } else {
      bndsRect.setBounds(bounds);
      map.fitBounds(bounds);
    }
  });
}
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map"></div>

关于javascript - 我试图让每个澳大利亚州都出现在谷歌地图上,但只有 3 个是。这是 KML 文件的大小问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45519908/

相关文章:

javascript - 如何将标题(h1,h2 ...)按钮添加到具有高级主题,简单布局的tinyMCE?

javascript - npm:在上下文中执行?

javascript - 如何在 javascript 中用 promise 替换 'Async=false'?

javascript - 谷歌地图多边形减慢浏览器

java - jak 看起来不再可用,使用 JAXB 创建 kml

JavaScript - 从二进制 JPG 数据创建图像

javascript - 使 GetJson 数组中的循环数据成为链接

javascript - Google Maps API v3 不显示

r - 使用 ggplot2 从 KML 数据绘制等值线图

google-maps - 链接到 Google Drive 上的 kml/kmz 文件