繪製多線幾何
建立一個向量源
var vectorSource = new ol.source.Vector({});
啟動地圖物件並將向量圖層新增到地圖,將 Source 新增為 vectorSource
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
}),
new ol.layer.Vector({
source: vectorSource
})
],
target: 'map',
view: new ol.View({
center: [45, 5],
zoom:5
})
});
將投影從源投影系統轉換為目標專案系統
var points=[];
for (i = 0; i < 10; i++) {
var xx = Math.random() * (xmax - xmin) + xmin;
var yy = Math.random() * (ymax - ymin) + ymin;
points.push(ol.proj.transform([xx,yy],'EPSG:4326', 'EPSG:3857'));
}
將點傳遞給 ol.geom.MultiLineString([])建構函式
var thing = new ol.geom.MultiLineString([points1]);
建立一個特徵並新增幾何圖形
var featurething = new ol.Feature({
name: "Thing",
geometry: thing,
style : new ol.style.Style({
stroke : new ol.style.Stroke({
color : 'red'
})
})
});
最後將其新增到原始碼
vectorSource.addFeature( featurething );
注意:放置適當的源和目標投影系統非常重要