使用OpenLayers使用WKT绘制多边形 - XML文件

时间:2015-04-07 19:37:41

标签: mysql xml dictionary openlayers wkt

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

XML File

如何使用它来使用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))');

我想绘制所有多边形以创建基于" Area"的专题地图。数据

1 个答案:

答案 0 :(得分: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
    });
});