【问题标题】:How can I use a SVG image as layer on OpenLayers 4如何在 OpenLayers 4 上使用 SVG 图像作为图层
【发布时间】:2018-01-12 17:00:41
【问题描述】:

我尝试基于 ol.source.ImageStatic 为 OpenLayers 3 https://stackoverflow.com/a/36183738/2797243 采用这种方法 到 OpenLayers 4,没有结果: https://codepen.io/itsonit/pen/jYKMMz?editors=0010

您可以在此处找到 OL4 的当前 ol.source.ImageStatic 示例:https://openlayers.org/en/latest/examples/static-image.html

如何将 SVG 源加载到 OpenLayers 4 地图?

【问题讨论】:

    标签: javascript svg openlayers


    【解决方案1】:

    功能似乎取决于 SVG 数据。一个工作示例:

    var svgComment = '<svg width="160" height="160" version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 160 160" viewPort="0 0 160 160" class="svgClass">'
        + '<circle cx="30" cy="30" r="10" stroke="rgb(0, 191, 255)" stroke-width="1" fill="none" opacity="0.8">'
        + '<animate attributeType="CSS" attributeName="stroke-width" from="1" to="30" dur="0.5s" begin="0s" repeatCount="indefinite" />'
        + '<animate attributeType="CSS" attributeName="opacity" from="0.8" to="0.2" dur="0.5s" begin="0s" repeatCount="indefinite" />'
        + '</circle>'
        + '<circle cx="30" cy="30" r="10" fill="rgba(0,0,0,0.8)">'
        + '</circle>'
        + '</svg>';
    
    
    //Test SVG
    //var img = document.createElement('img');
    //img.src=src;
    //document.body.append(img);
    
     var commentStyle =  new ol.style.Style({
        image: new ol.style.Icon({
          src: 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svgComment)
        })
      });
    
    var vectorSource = new ol.source.Vector({
      features: [
          new ol.Feature({
            geometry: new ol.geom.Point([0, 0])
          }) 
      ]
    });
    
    var vectorLayer = new ol.layer.Vector({
      name: 'Comments',
      style: commentStyle,
      source: vectorSource
    });
    
    //display the map
    var rasterLayer = new ol.layer.Tile({
      source: new ol.source.TileJSON({
        url: 'https://api.tiles.mapbox.com/v3/mapbox.geography-class.json?secure',
        crossOrigin: ''
      })
    });
    
    var map = new ol.Map({
      layers: [rasterLayer, vectorLayer],
      target: document.getElementById('map'),
      view: new ol.View({
        center: [0, 0],
        zoom: 3
      })
    });
    <script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
    <div id="map" class="map"></div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多