openlayers编辑区域

2023-03-14,,

<!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="lib/OpenLayers/ol.css" type="text/css"> <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="lib/OpenLayers/ol.js"></script>
<script src="olStyle/Light.js"></script>
<style>
html,
body {
width: 100%;
height: 100%;
margin: 0;
} .map {
width: 100%;
height: 100%;
background: #f6f6f4;
}
</style>
</head> <body>
<div id="map" class="map" data-id="11"></div>
<script type="text/javascript"> $(function () { InitMap(); AddPolygon(); //编辑
EditArea(); }) var map; var layer = new ol.layer.Vector({
source: new ol.source.Vector(),
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 0.5)'
}),
stroke: new ol.style.Stroke({
color: 'red',
width: 2
}),
image: new ol.style.Circle({
//半径大小
radius: 7,
//填充
fill: new ol.style.Fill({
//填充颜色
color: '#e81818'
})
})
})
}); //地图初始化
function InitMap() {
map = new ol.Map({
layers: [new ol.layer.Vector({
source: new ol.source.Vector()
}),
style: function (feature, resolution) {
switch (feature.get('layer')) {
case 'poi':
poiStyle.getText().setText(feature.get('name'));
return poiStyle;
case 'boundary': return boundaryStyle;
case 'lawn': return lawnStyle;
case 'road':
roadStyle.getText().setText(feature.get('name'));
return (resolution < 2) ? roadStyle : null;
case 'building':
return buildingStyle(feature, resolution);
case 'other':
otherStyle.getText().setText(feature.get('name'));
return otherStyle;
default: return null;
}
}
}), layer],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([120.277, 36.330]),
minZoom: 1,
zoom: 16
})
});
} /*显示并编辑区域**********************************************************************************/
function AddPolygon() {
layer.getSource().clear(); // var points = [[
// ol.proj.fromLonLat([120.27681827545163, 36.33236136226455]),
// ol.proj.fromLonLat([120.28038024902342, 36.33141061727086]),
// ol.proj.fromLonLat([120.27681827545163, 36.33001040806349]),
// ol.proj.fromLonLat([120.27681827545163, 36.33236136226455])
// ]] // var plygon = new ol.geom.Polygon(points)
// //多边形要素类
// var feature = new ol.Feature({
// geometry: plygon
// }); //var points = ol.proj.fromLonLat([120.27611017227173,36.331306898933306]);
var points = [13389075.33897296,4346304.97458048] var plygon = new ol.geom.Circle(points,294)
var feature = new ol.Feature({
geometry: plygon
}); layer.getSource().addFeature(feature); } function EditArea() { modify = new ol.interaction.Modify({
source: layer.getSource(),
});
//modify.setActive(true);
modify.on("modifyend", function () {
// 编辑结束时获取坐标点
// var features = layer.getSource().getFeatures(); // var geometry = features[0].getGeometry(); // var points;
// var lnglat = []; // points = geometry.getCoordinates()[0]; // for (var i = 0; i < points.length; i++) {
// lnglat.push(ol.proj.transform(points[i], 'EPSG:3857', 'EPSG:4326'));
// } // console.log(lnglat);
})
map.addInteraction(modify);
} </script>
</body> </html>

  

openlayers编辑区域的相关教程结束。

《openlayers编辑区域.doc》

下载本文的Word格式文档,以方便收藏与打印。