openlayer 画矩形
时间: 2024-06-07 22:04:12 浏览: 201
OPENGL绘制矩形
5星 · 资源好评率100%
OpenLayers是一个开源的JavaScript库,用于在Web页面上显示交互式地图。要使用OpenLayers画矩形,你需要先引入OpenLayers库,并创建一个地图容器。接下来,你可以通过创建一个矩形几何对象,将其添加到地图上。以下是一个简单的示例代码,用于在OpenLayers中绘制一个矩形:
```
<!DOCTYPE html>
<html>
<head>
<title>OpenLayers Draw Rectangle Example</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.5/css/ol.css" type="text/css">
<script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<script>
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
})
});
var rectangle = new ol.geom.Polygon.fromExtent([-5000000, -5000000, 5000000, 5000000]);
var feature = new ol.Feature(rectangle);
var vectorSource = new ol.source.Vector({
features: [feature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
map.addLayer(vectorLayer);
</script>
</body>
</html>
```
阅读全文