我是openlayers的新手3。我以图像的形式显示点,但我想动态更改图像的颜色。在openlayers3中可能会或其他设施(例如openlayers中的画布(3?
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'data/icon.png'
}))
});
以图标颜色为起点,在其中使用透明的png带有 ol.Color
选项具有多种颜色点,我尝试更改这样的颜色而没有成功...
var london = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-0.12755, 51.507222]))
});
london.setStyle(new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
color: '#ff0000',
crossOrigin: 'anonymous',
src: 'https://openlayers.org/en/v4.1.1/examples/data/dot.png'
}))
}));
var vectorSource = new ol.source.Vector({
features: [london]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
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: ol.proj.fromLonLat([2.896372, 44.60240]),
zoom: 3
})
});
map.on('singleclick', function (event) { // Use 'pointermove' to capture mouse movement over the feature
map.forEachFeatureAtPixel(event.pixel, function (feature, layer) {
/* Get the current image instance and its color */
var image = feature.getStyle().getImage();
console.log(image.getColor());
/* Color gets stored in image.i, but there's no setter */
console.log(image.i);
/* Trying to force the color change: */
image.i = [0, 255, 0, 1];
/* Dispatch the changed() event on layer, to force styles reload */
layer.changed();
console.log(image.getColor()); // Logs new color, but it doesn't work...
});
});
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.1.1/build/ol.js"></script>
<div id="map" class="map"></div>
查看如何检索这样的图标颜色:
var image = feature.getStyle().getImage(),
color = image.getColor(); // returns [R, G, B, alpha]
但是没有setColor()
方法,因此无法在运行时更改颜色...
编辑:再次查看您的问题和代码,我注意到您永远不会为图标设置颜色,所以也许您真的不想真正更改应事件的颜色,但只是 set 颜色。如果那是您想要的,那很容易:
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
src: 'data/icon.png',
color: '#ff0000' // allows HEX as String or RGB(a) as Array
}))
});
请参阅:http://openlayers.org/en/latest/apidoc/ol.style.icon.icon.html