作为一种选择,您可以创建具有与 OL3 样式相同的属性的 SVG 元素。这是圆形样式的示例(您也需要其他类型的类似方法):
getIconLegend = function(style) {
style = style.getImage();
var radius = style.getRadius();
var strokeWidth = style.getStroke().getWidth();
var dx = radius + strokeWidth;
var svgElem = $('<svg />')
.attr({
width: dx * 2,
height: dx * 2
});
$('<circle />')
.attr({
cx: dx,
cy: dx,
r: radius,
stroke: style.getStroke().getColor(),
'stroke-width': strokeWidth,
fill: style.getFill().getColor()
})
.appendTo(svgElem);
// Convert DOM object to string to overcome from some SVG manipulation related oddities
return $('<div>').append(svgElem).html();
}
由于使用 jQuery 的 SVG 操作与 HTML 元素有点不同,因此我将对象转换为字符串作为回报。可以从jquery 的 append not working with svg element 中找到详细信息?
稍后,您可以将图例图标粘贴到 HTML 中
$('#legendText').prepend($(getIconLegend(yourFeature.getStyle())));