只需解析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
});
});
喜伊戈爾 - Chernikov擔任,我想你的代碼,但我不明白的腳本工作。 我得到這個錯誤使用Chrome: 未捕獲TypeError:無法讀取屬性'closure_uid_175869212'undefined localhost /:38未捕獲TypeError:undefined不是函數 – Seph1603
你使用OpenLayers 3嗎?請參閱編輯我的答案 –
我不斷收到此錯誤:無法讀取屬性'closure_uid_257584869'的未定義 我不知道我在做什麼錯誤https://jsfiddle.net/seph1603/yu2hywmt – Seph1603