0

我用 PHP 和 Mysql 创建了这个 XML 文件:

XML 文件

如何使用 OpenLayers 绘制所有多边形?我做了一些研究并找到了 WKT 的示例,但在这些示例中,他们只使用了一个多边形:

var feature = format.readFeature(
'POLYGON((10.689697265625 -25.0927734375, 34.595947265625 ' +
'-20.1708984375, 38.814697265625 -35.6396484375, 13.502197265625 ' +
'-39.1552734375, 10.689697265625 -25.0927734375))');

我想绘制所有多边形以创建基于“区域”数据的专题图。

4

1 回答 1

0

只需解析您的 XML 并遍历标记标签。像这样的东西:

var xml = new OpenLayers.Format.XML(),
    wkt = new OpenLayers.Format.WKT(),
    vectorLayer = new OpenLayers.Layer.Vector('features'),
    doc, markers, i, feature;
OpenLayers.Request.GET({
    url: "features.xml",
    success: function(request) {
        doc = xml.read(request.responseText);
        markers = doc.documentElement.getElementsByTagName('marker');
        for (i = 0; i < markers.length; i++) {
            feature = wkt.read(markers[i].attributes.geometry.nodeValue);
            vectorLayer.addFeatures([feature]);
        }
    }
});

编辑:如果您使用的是 OpenLayers 3,请尝试以下操作:

var wkt = new ol.format.WKT(),
    vectorLayer,
    source,
    features = [],
    feature,
    markers;
//make sure that jQuery is included
$.ajax('features.xml').then(function(response) {
    var markers = response.getElementsByTagName('marker');
    for (var i = 0; i < markers.length; i++) {
        feature = wkt.readFeature(markers[i].attributes.geometry.nodeValue);
        features.push(feature);
    }
    source = new ol.source.Vector({
        features: features
    });
    vectorLayer = new ol.layer.Vector({
        source: source
    });
});
于 2015-04-08T04:35:18.333 回答