openlayers绘制多边形
时间: 2023-08-13 10:06:19 浏览: 121
要使用OpenLayers绘制多边形,可以按照以下步骤进行操作:
1. 创建一个OpenLayers地图对象:
```
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: new ol.View({
center: ol.proj.fromLonLat([37.41, 8.82]),
zoom: 4
})
});
```
2. 创建一个绘制图层和一个绘制交互对象:
```
var source = new ol.source.Vector({wrapX: false});
var vector = new ol.layer.Vector({
source: source
});
map.addLayer(vector);
var draw = new ol.interaction.Draw({
source: source,
type: 'Polygon'
});
map.addInteraction(draw);
```
3. 监听绘制结束事件并获取多边形的坐标数组:
```
var coordinates = [];
draw.on('drawend', function(event) {
var feature = event.feature;
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates()[0];
coordinates = coord;
});
```
4. 将多边形坐标数组用于其他操作,例如显示多边形信息:
```
var info = document.getElementById('info');
info.innerHTML = '';
for (var i = 0; i < coordinates.length; i++) {
info.innerHTML += 'Point ' + (i+1) + ': ' + coordinates[i] + '<br>';
}
```
以上是OpenLayers绘制多边形的基本步骤,可以根据需要进行自定义和扩展。
阅读全文