OpenLayers 教程
最开始接触地图的时候,对圆的理解不正确,以为也是面状的;实际上 Openlayers 中圆指的就是圆点和半径,而面状的圆其实就是多边形,比如十七边形,只是看起来像圆。
下边介绍创建圆的方法以及根据圆获取圆形多边形的方法。
Openlayers 圆的操作
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="http://openlayers.vip/examples/css/ol.css" type="text/css">
<style>
.map {
height: 400px;
width: 100%;
float: left;
}
</style>
<script src="http://openlayers.vip/examples/resources/ol.js"></script>
<script src="./tiandituLayers.js"></script>
<title>OpenLayers example</title>
</head>
<body>
<h2>Feature transfer</h2>
<div id="map" class="map"></div>
<script type="text/javascript">
var map = new ol.Map({
target: 'map',
layers: [
getIMG_CLayer(),
getIBO_CLayer(),
getCIA_CLayer(),
],
view: new ol.View({
projection: "EPSG:4326",
center: [116, 39],
zoom: 4,
maxZoom: 18,
minZoom: 1,
})
});
var xy = [116.391232637988, 39.907157016256974];
let myCircle = new ol.geom.Circle(xy, 0.01);
let feature = new ol.Feature({
geometry: myCircle,
name: 'My Circle',
});
var layer = initVectorLayer();
function initVectorLayer() {
let source = new ol.source.Vector();
let customVectorLayer = new ol.layer.Vector({
source: source,
zIndex: 2,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'red',
width: 5,
lineDash: [3, 5]
}),
fill: new ol.style.Fill({
color: 'rgba(0, 0, 255, 0.3)',
}),
}),
});
map.addLayer(customVectorLayer);
customVectorLayer.getSource().addFeatures([feature]);
var extent = customVectorLayer.getSource().getExtent();
map.getView().fit(extent, {
duration: 1,
callback: null,
});
return customVectorLayer;
}
function toPolygon() {
var polygon_circle = new ol.Feature({
geometry: ol.geom.Polygon.fromCircle(feature.getGeometry(), 16, 0),
name: 'polygon_circle'
})
polygon_circle.setStyle(new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'blue',
width: 5,
lineDash: [3, 5]
}),
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 1)',
}),
}))
layer.getSource().addFeatures([polygon_circle]);
}
</script>
<button id="toPolygon" onclick="toPolygon()">转为多边形圆</button>
</body>
</html>
在线示例
地图添加各种数据格式点线面:Openlayers circle
|