Atlaslog
Atlaslog / Examples / OpenLayers / Regularshape
OpenLayers · Runnable Example

Regularshape

Runnable OpenLayers example: Regularshape. Open the live demo, read the complete source, and copy the file into your project. No build step or API key.

Open standalone → Chinese notes All OpenLayers examples
/demos/openlayers-regularshape.htmlLIVE
single HTML file · CDN dependencies onlyinterface may contain Chinese labels

Source

demos/openlayers-regularshape.html
<!--
  示例:规则图形符号
  要点:RegularShape 用 points / radius / radius2 / angle 组合出星形、三角形、十字
  依赖:OpenLayers 10.3.1 · jsDelivr CDN(底图 OSM,无 Key)
-->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta name="robots" content="noindex,follow">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>规则图形符号</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@10.3.1/ol.css">
<style>
  html,body{height:100%;margin:0}
  body{display:flex;flex-direction:column;overflow:hidden;
       font:13px/1.6 -apple-system,"Segoe UI","PingFang SC",sans-serif;color:#16181d}
  .bar{flex:0 0 auto;padding:8px 12px;background:#f6f7f9;border-bottom:1px solid #e3e6ea;
       display:flex;gap:14px;align-items:center;font-size:12px;color:#58606e;flex-wrap:wrap}
  #map{flex:1 1 auto;min-height:0;background:#eef0f3}
</style>
</head>
<body>
<div class="bar"><span>五角星为站点,三角形为机场,十字为医院,正方形为仓储,旋转参数随等级变化</span></div>
<div id="map"></div>
<script src="https://cdn.jsdelivr.net/npm/ol@10.3.1/dist/ol.js"></script>
<script>
(function () {
  'use strict';
  if (!window.ol) {
    document.getElementById('map').textContent = 'OpenLayers 加载失败,请检查 cdn.jsdelivr.net 是否可访问';
    return;
  }

  var fillBlue = new ol.style.Fill({ color: '#1f5fd0' });
  var fillRed = new ol.style.Fill({ color: '#d64545' });
  var fillGreen = new ol.style.Fill({ color: '#2f8f5b' });
  var fillGray = new ol.style.Fill({ color: '#5b6472' });
  var edge = new ol.style.Stroke({ color: '#ffffff', width: 2 });

  var SHAPES = {
    star: new ol.style.RegularShape({
      points: 5,
      radius: 11,
      radius2: 4.6,
      angle: 0,
      fill: fillBlue,
      stroke: edge
    }),
    triangle: new ol.style.RegularShape({
      points: 3,
      radius: 12,
      fill: fillRed,
      stroke: edge
    }),
    cross: new ol.style.RegularShape({
      points: 4,
      radius: 11,
      radius2: 3,
      angle: 0,
      fill: fillGreen,
      stroke: edge
    }),
    square: new ol.style.RegularShape({
      points: 4,
      radius: 9,
      angle: Math.PI / 4,
      fill: fillGray,
      stroke: edge
    }),
    diamond: new ol.style.RegularShape({
      points: 4,
      radius: 11,
      angle: 0,
      rotation: Math.PI / 4,
      fill: new ol.style.Fill({ color: '#c98a1b' }),
      stroke: edge
    })
  };

  var DATA = [
    ['星港站', 116.391, 39.907, 'star'],
    ['东郊机场', 116.600, 40.080, 'triangle'],
    ['中心医院', 116.352, 39.930, 'cross'],
    ['北仓物流园', 116.420, 40.010, 'square'],
    ['西苑枢纽', 116.290, 39.960, 'diamond']
  ];

  var features = DATA.map(function (d) {
    return new ol.Feature({
      geometry: new ol.geom.Point(ol.proj.fromLonLat([d[1], d[2]])),
      name: d[0],
      kind: d[3]
    });
  });

  var layer = new ol.layer.Vector({
    source: new ol.source.Vector({ features: features }),
    style: function (f) {
      return new ol.style.Style({
        image: SHAPES[f.get('kind')],
        text: new ol.style.Text({
          text: f.get('name'),
          offsetY: -20,
          font: '500 12px sans-serif',
          fill: new ol.style.Fill({ color: '#16181d' }),
          stroke: new ol.style.Stroke({ color: '#ffffff', width: 3 })
        })
      });
    }
  });

  new ol.Map({
    target: 'map',
    layers: [new ol.layer.Tile({ source: new ol.source.XYZ({url: 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer/tile/{z}/{y}/{x}', attributions: 'Tiles © Esri'}) }), layer],
    view: new ol.View({ center: ol.proj.fromLonLat([116.40, 39.97]), zoom: 12 }),
    controls: ol.control.defaults.defaults({ attribution: false, rotate: false })
  });
})();
</script>
</body>
</html>
Language: the code and this page are in English; the full tutorial and API notes for this example are in Chinese. The demo itself is the authoritative version — it runs exactly as shown.

More OpenLayers examples

OpenLayers · EXAMPLERender Geometry OpenLayers · EXAMPLEReprojection OpenLayers · EXAMPLEReprojection By Code