javascript - Openlayers 3 - 创建自定义符号,如 openlayers 2 -> OpenLayers.Renderer.symbol.anySymbol

标签 javascript maps gis openlayers openlayers-3

Openlayers v2 中,我可以添加如下符号:

OpenLayers.Renderer.symbol.pointToIcon = [100, 70, 20, 50, 42, 70, 20, 90];

我如何使用 Openlayers 3 做到这一点?

openlayers 3 我可以使用这个功能:

var starSymbol = new ol.style.RegularShape({
    points: 4,
    opacity: .5,
    radius: 10,
    radius2: 10 * .5,
    angle: 130,
    fill: new ol.style.Fill({
        color: "blue"
    }),
    stroke: new ol.style.Stroke({
        color: "red",
        width: 1
    })
});

其他的图我都可以,但是下图我不会。

enter image description here

此外,我不会像这样使用“图标”:

new ol.style.Style({
    image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
        anchor: [0.5, 46],
        anchorXUnits: 'fraction',
        anchorYUnits: 'pixels',
        opacity: 0.75,
        src: 'data/icon.png'
    }))
})

因为图标的颜色是固定的,我不能使用我需要的颜色。

如果你能帮助我,我真的很感激。

最佳答案

您可以将自定义符号绘制到 Canvas 上,并将其用作图标图像。 OpenLayers 为此提供了一个非常好的 API。您可能想看一下示例:http://openlayers.org/en/v3.12.1/examples/earthquake-custom-symbol.html .

对于您的特定交易品种,代码可能如下所示:

var canvas = document.createElement('canvas');
var render = ol.render.toContext(canvas.getContext('2d'),
    {size: [50, 45]});
render.setFillStrokeStyle(
    new ol.style.Fill({ color: 'red' }),
    new ol.style.Stroke({ color: 'black' }));
render.drawPolygonGeometry(new ol.geom.Polygon(
    [[[50, 35], [10, 25], [21,35], [10, 45], [50, 35]]]));
var style = new ol.style.Style({
  image: new ol.style.Icon({
    img: canvas,
    imgSize: [canvas.width, canvas.height],
  })
});

关于javascript - Openlayers 3 - 创建自定义符号,如 openlayers 2 -> OpenLayers.Renderer.symbol.anySymbol,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34378461/

相关文章:

javascript - 使用 Google Maps API 对 LatLong 进行地理编码返回未定义

javascript - 使用动态数据向 highcharts 图表添加不同的符号

android - 使用 MyLocationOverlay 绘制精度圆

c# - Windows 应用商店应用程序的免费映射组件?

database - 计算邮政编码之间的距离...和用户。

javascript - 我想在 JavaScript 中更改日期对象的时区

javascript - 页面重叠在页脚 div 上

java - getLastKnownLocation() 总是返回 null,所有提供者都是 DummyLocationProvider

clojure - 为什么匿名函数不能与 clojure 中的 map 一起使用?

C# WPF 分辨率独立性?