OpenLayers 笔记

创建多边形

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4.     <meta charset=“UTF-8”>
  5.     <meta name=“viewport” content=“width=device-width, initial-scale=1.0”>
  6.     <meta http-equiv=“X-UA-Compatible” content=“ie=edge”>
  7.     <title>Custom Polygon Styles</title>
  8.     <link href=“ol_v5.0.0/css/ol.css” rel=“stylesheet” type=“text/css” />
  9.     <script src=“ol_v5.0.0/build/ol.js” type=“text/javascript”></script>
  10. </head>
  11. <body>
  12.     <div id=“map”></div>
  13.     <script>
  14.         var styles = [
  15.             new ol.style.Style({
  16.                 stroke: new ol.style.Stroke({       // 线样式
  17.                     color: ‘blue’,
  18.                     width: 3
  19.                 }),
  20.                 fill: new ol.style.Fill({           // 填充样式
  21.                     color: ‘rgba(0, 0, 255, 0.1)’
  22.                 })
  23.             }),
  24.             new ol.style.Style({
  25.                 image: new ol.style.Circle({   // 点样式
  26.                     radius: 5,          // 圆形符号的半径
  27.                     fill: new ol.style.Fill({   // 圆形符号的填充样式
  28.                         color: ‘orange’
  29.                     })
  30.                 }),
  31.                 // 将样式设置为多边形外环独有的样式
  32.                 geometry: function(feature){
  33.                     // 返回多变形第一个外环的坐标(数组形式)
  34.                     var coordinates = feature.getGeometry().getCoordinates()[0];
  35.                     // 将返回的多边形外环的坐标以MultiPoint的形式重新创建为多点图形
  36.                     return new ol.geom.MultiPoint(coordinates);
  37.                 }
  38.             })
  39.         ];
  40.         // GeoJson格式的数据
  41.         var geojsonObject = {
  42.             ‘type’: ‘FeatureCollection’,
  43.             ‘crs’: {
  44.             ‘type’: ‘name’,
  45.             ‘properties’: {
  46.                 ‘name’: ‘EPSG:3857’
  47.             }
  48.             },
  49.             ‘features’: [{
  50.             ‘type’: ‘Feature’,
  51.             ‘geometry’: {
  52.                 ‘type’: ‘Polygon’,
  53.                 ‘coordinates’: [[[-5e6, 6e6], [-5e6, 8e6], [-3e6, 8e6],
  54.                 [-3e6, 6e6], [-5e6, 6e6]]]
  55.             }
  56.             }, {
  57.             ‘type’: ‘Feature’,
  58.             ‘geometry’: {
  59.                 ‘type’: ‘Polygon’,
  60.                 ‘coordinates’: [[[-2e6, 6e6], [-2e6, 8e6], [0, 8e6],
  61.                 [0, 6e6], [-2e6, 6e6]]]
  62.             }
  63.             }, {
  64.             ‘type’: ‘Feature’,
  65.             ‘geometry’: {
  66.                 ‘type’: ‘Polygon’,
  67.                 ‘coordinates’: [[[1e6, 6e6], [1e6, 8e6], [3e6, 8e6],
  68.                 [3e6, 6e6], [1e6, 6e6]]]
  69.             }
  70.             }, {
  71.             ‘type’: ‘Feature’,
  72.             ‘geometry’: {
  73.                 ‘type’: ‘Polygon’,
  74.                 ‘coordinates’: [[[-2e6, -1e6], [-1e6, 1e6],
  75.                 [0, -1e6], [-2e6, -1e6]]]
  76.             }
  77.             }]
  78.         };
  79.         // 读取GeoJson数据并将其初始化为矢量图层源
  80.         var source = new ol.source.Vector({
  81.             features: (new ol.format.GeoJSON()).readFeatures(geojsonObject)
  82.         });
  83.         // 承载GeoJson数据的矢量图层
  84.         var layer = new ol.layer.Vector({
  85.             source: source,
  86.             style: styles           // 图层可以接受一个样式数组作为渲染的样式
  87.         });
  88.         var map = new ol.Map({
  89.             target: ‘map’,
  90.             layers: [
  91.                 layer
  92.             ],
  93.             view: new ol.View({
  94.                 center: [0, 3000000],
  95.                 zoom: 2
  96.             })
  97.         });
  98.     </script>
  99. </body>
  100. </html>

 

禁用鼠标双击缩放事件

 

  1. const dblClickInteraction = map  
  2.         .getInteractions()  
  3.         .getArray()  
  4.         .find(interaction => {  
  5.           return interaction instanceof ol.interaction.DoubleClickZoom;  
  6.         });  
  7. map.removeInteraction(dblClickInteraction);  

本文链接地址: OpenLayers 笔记