【问题标题】:Dynamically change color of polygon in linked KML file动态更改链接 KML 文件中多边形的颜色
【发布时间】:2013-09-27 18:01:04
【问题描述】:

我希望通过使用 JavaScript 的插件创建一个由覆盖在 Google 地球上的多边形组成的 Choropleth 地图。

多边形存在于服务器(不一定是我的)上的 KML 文件中,所有这些都具有唯一的 ID。 我希望能够动态更改多边形的颜色以显示不同的数据集。

这可行吗?

我查看了 KML 机制,但这只适用于同一服务器上的文件。

谢谢,

比尔

【问题讨论】:

    标签: javascript kml google-earth google-earth-plugin


    【解决方案1】:

    您可以通过Google Earth API 从任何网络可访问的 URL(同一服务器或其他)加载/解析远程 KML,然后遍历 KML 对象并以编程方式更改样式和多边形颜色。

    var href = 'http://code.google.com/'
               + 'apis/earth/documentation/samples/kml_example.kml';
    
    google.earth.fetchKml(ge, href, function(kmlObject) {
          if (kmlObject) {
             checkObject(kmlObject);
             // append KML objects to current view
             ge.getFeatures().appendChild(kmlObject);
          }    
    });
    
    function checkObject(kmlObject) {
        var type = kmlObject.getType();         
        if (type == 'KmlDocument' || type == 'KmlFolder') {
            var features = kmlObject.getFeatures();
            if (features.hasChildNodes()) {
                var children = features.getChildNodes();                    
                for (i=0; i < children.getLength(); i++) {
                    checkObject(children.item(i));                      
                }
            }
        } else if (type == 'KmlPlacemark') {
            // check/set style, change color, etc.
            // ...
        }
    }
    

    参考: https://developers.google.com/earth/documentation/kml#fetchkml_and_parsekml

    【讨论】:

      猜你喜欢
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多