Javascript OpenLayers3在圆上添加文本

Javascript OpenLayers3在圆上添加文本,javascript,html,css,openlayers-3,Javascript,Html,Css,Openlayers 3,在搜索了很多之后,我终于找到了一段代码,允许我在地图上画一个圆圈 HTML: JavaScript: $(document).ready(function(){ var map = new ol.Map({ target: 'mapHolder', interactions: ol.interaction.defaults({mouseWheelZoom:false}), layers: [ new ol.l

在搜索了很多之后,我终于找到了一段代码,允许我在地图上画一个圆圈

HTML:

JavaScript:

$(document).ready(function(){
  var map = new ol.Map({
          target: 'mapHolder',
          interactions: ol.interaction.defaults({mouseWheelZoom:false}),
          layers: [
            new ol.layer.Tile({
              source: new ol.source.OSM()
            })
          ],
          view: new ol.View({
            center: ol.proj.transform([parseFloat(8.680239), parseFloat(50.114034)], 'EPSG:4326','EPSG:3857'),
        zoom: 13
          })
        });



  var vectorSource = new ol.source.Vector();
  var circleLayer = new ol.layer.Vector({
    source: vectorSource
  });
  map.addLayer(circleLayer);

  var coordinate = ol.proj.transform([parseFloat(8.680239), parseFloat(50.114034)], 'EPSG:4326','EPSG:3857');
  vectorSource.addFeature(new ol.Feature(new ol.geom.Circle(coordinate, 2000)));

});
这是小提琴:

1) 如何在圆圈上放置标题为“近似面积”的文字;并且能够定义颜色和字体


2) 此外,我还想更改圆边框的颜色和厚度。

您可以使用矢量层上的样式来获得它

宣布你的风格

     var myStlye = new ol.style.Style ({
      fill: new ol.style.Fill({
         color: 'rgba(255,100,50,0.5)'
       }),
       stroke: new ol.style.Stroke({
         color: 'blue',
         width: 3
       }),
       text: new ol.style.Text({
         textAlign: "Start",
         textBaseline: "Middle",
         font: 'Normal 12px Arial',
         text: 'Approximate Area',
         fill: new ol.style.Fill({
           color: '#ffa500'
         }),
         stroke: new ol.style.Stroke({
           color: '#000000',
           width: 3
         }),
         offsetX: -45,
         offsetY: 0,
         rotation: 0
       })
    });
然后将其附加到图层上

 var circleLayer = new ol.layer.Vector({
        style:myStlye,
        source: vectorSource
      });

下面是一个示例,您可以使用矢量层上的样式来查看它的运行情况

宣布你的风格

     var myStlye = new ol.style.Style ({
      fill: new ol.style.Fill({
         color: 'rgba(255,100,50,0.5)'
       }),
       stroke: new ol.style.Stroke({
         color: 'blue',
         width: 3
       }),
       text: new ol.style.Text({
         textAlign: "Start",
         textBaseline: "Middle",
         font: 'Normal 12px Arial',
         text: 'Approximate Area',
         fill: new ol.style.Fill({
           color: '#ffa500'
         }),
         stroke: new ol.style.Stroke({
           color: '#000000',
           width: 3
         }),
         offsetX: -45,
         offsetY: 0,
         rotation: 0
       })
    });
然后将其附加到图层上

 var circleLayer = new ol.layer.Vector({
        style:myStlye,
        source: vectorSource
      });
下面是一个例子,看看它的实际效果