只需解析您的 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
});
});