0
votes

I have this XML file created with PHP and Mysql:

XML File

How can I use this to draw all the polygons using OpenLayers? I did some research and found examples with WKT, but in those examples they used just one polygon:

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))');

I want to draw all the polygons to create a thematic map based on the "Area" data.

1

1 Answers

0
votes

Just parse your XML and iterate over the marker tags. Something like this:

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]);
        }
    }
});

Edit: If you are using OpenLayers 3, try the following:

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
    });
});