矩形
矩形示例。 简单 geometry rectangle box bbox 多边形
一个简单的地图,底图为 OSM,并包含一个矩形多边形要素(例如边界框)。
main.js
js
import Feature from 'ol/Feature.js';
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {fromExtent} from 'ol/geom/Polygon.js';
import TileLayer from 'ol/layer/Tile.js';
import VectorLayer from 'ol/layer/Vector.js';
import OSM from 'ol/source/OSM.js';
import VectorSource from 'ol/source/Vector.js';
const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
new VectorLayer({
source: new VectorSource({
features: [
new Feature(
// 这里需要一个 `Geometry`(几何对象),例如 `Polygon`(多边形),它提供了一些便捷函数,可以从 bbox 坐标创建矩形
fromExtent([-1000000, 5000000, 3000000, 7000000]), // 最小 X、最小 Y、最大 X、最大 Y
),
],
}),
}),
],
target: 'map',
view: new View({
center: [1000000, 6000000],
zoom: 4,
}),
});index.html
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>矩形</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script type="module" src="main.js"></script>
</body>
</html>package.json
json
{
"name": "rectangle",
"dependencies": {
"ol": "10.11.0"
},
"devDependencies": {
"vite": "^8.1.5"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}