简体   繁体   English

如何在OpenLayers 3中插入半圈样式标记

[英]How to insert half circle style marker in OpenLayers 3

Is it possible to insert half circle shape like below? 是否可以插入如下所示的半圆形?

在此处输入图片说明

I'd like to use it as a vector point marker. 我想将其用作矢量点标记。

RegularShape goes close to my intention but it seems to work only for "regular shapes". RegularShape接近我的意图,但它似乎仅适用于“常规形状”。

I've found also an example using Icon which looks very useful and I can achieve a similar result with polygons. 我还发现了一个使用Icon的示例,它看起来非常有用,并且使用多边形也可以实现类似的结果。 Is there any smarter way? 有没有更聪明的方法?

What about using Point instead of Polygon and masking the other half? 使用Point代替Polygon并遮盖另一半该怎么办? Thanks. 谢谢。

Like in the example you found, you can create a canvas and use it as img of an icon style: 就像在您发现的示例中一样,您可以创建一个画布并将其用作icon样式的img

var canvas = document.createElement('canvas');
canvas.width = size;
canvas.height = size;
var context = canvas.getContext('2d');
var centerX = canvas.width / 2;
var centerY = canvas.height / 4;
var radius = canvas.width / 4;
var startingAngle = Math.PI / 2;
var endingAngle = -Math.PI / 2;
var counterclockwise = false;
context.arc(centerX, centerY, radius, startingAngle,
    endingAngle, counterclockwise);
context.fillStyle = '#bada55';
context.fill();
style = new ol.style.Style({
  image: new ol.style.Icon({
    img: canvas,
    imgSize: [size, size]
  })
});

I created a JSFiddle which shows the above code in action. 我创建了一个JSFiddle ,它显示了上面的代码。

The circle creation code is based on an answer from How to draw bottom half of a circle in canvas . 圆的创建代码基于如何在画布中绘制圆的下半部分的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM