1.向map中添加vector圖層方式:
首先要想vector屬于layer是以查找api總的layer,可以找到vector,檢視vector構造方法,
vector需要兩個參數,name和options(可以檢視OpenLayers.Layer.Vector中的屬性)直接截圖
也就是在options中我們可以配置這麼多屬性,我們隻配置isBaseLayer
構造的代碼如下
var vectors = new OpenLayers.Layer.Vector("vector", {isBaseLayer: true});
将vectore圖層添加到map中:
map.addLayers([vectors]);
之後我們向vector添加features屬性可以通過
進行添加。
其次打開addFeatures方法,看看他是如何構造的
參數有features和option,看到features是一個array類型,繼續點開Openlayers.Feature.Vector看怎麼構造的,方法同上
這裡的geometry參數我們通過wkt來進行添加
var feature = new OpenLayers.Feature.Vector(
OpenLayers.Geometry.fromWKT(
"POLYGON((28.828125 0.3515625, 132.1875 -13.0078125, -1.40625 59.4140625, 28.828125 0.3515625))"
)
);
feature構造完畢,将feature通過addFeature添加到vector中
vectors.addFeatures([feature]);
此時vector構造完成