Example assigning a custom color to an icon
Example assigning a custom color to an icon. The icon styles in this example use images with a white fill. For some features, custom colors set using the color
property. Note that icon files need to obey the same origin policy or send proper CORS headers for this to work. When relying on CORS headers, the ol/style/Icon
must be configured with crossOrigin: 'anonymous'
.
import 'ol/ol.css';
import Feature from 'ol/Feature';
import Map from 'ol/Map';
import Point from 'ol/geom/Point';
import TileJSON from 'ol/source/TileJSON';
import VectorSource from 'ol/source/Vector';
import View from 'ol/View';
import {Icon, Style} from 'ol/style';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {fromLonLat} from 'ol/proj';
const rome = new Feature({
geometry: new Point(fromLonLat([12.5, 41.9])),
});
const london = new Feature({
geometry: new Point(fromLonLat([-0.12755, 51.507222])),
});
const madrid = new Feature({
geometry: new Point(fromLonLat([-3.683333, 40.4])),
});
const paris = new Feature({
geometry: new Point(fromLonLat([2.353, 48.8566])),
});
const berlin = new Feature({
geometry: new Point(fromLonLat([13.3884, 52.5169])),
});
rome.setStyle(
new Style({
image: new Icon({
color: '#BADA55',
crossOrigin: 'anonymous',
// For Internet Explorer 11
imgSize: [20, 20],
src: 'data/square.svg',
}),
})
);
london.setStyle(
new Style({
image: new Icon({
color: 'rgba(255, 0, 0, .5)',
crossOrigin: 'anonymous',
src: 'data/bigdot.png',
scale: 0.2,
}),
})
);
madrid.setStyle(
new Style({
image: new Icon({
crossOrigin: 'anonymous',
src: 'data/bigdot.png',
scale: 0.2,
}),
})
);
paris.setStyle(
new Style({
image: new Icon({
color: '#8959A8',
crossOrigin: 'anonymous',
// For Internet Explorer 11
imgSize: [20, 20],
src: 'data/dot.svg',
}),
})
);
berlin.setStyle(
new Style({
image: new Icon({
crossOrigin: 'anonymous',
// For Internet Explorer 11
imgSize: [20, 20],
src: 'data/dot.svg',
}),
})
);
const vectorSource = new VectorSource({
features: [rome, london, madrid, paris, berlin],
});
const vectorLayer = new VectorLayer({
source: vectorSource,
});
const rasterLayer = new TileLayer({
source: new TileJSON({
url: 'https://a.tiles.mapbox.com/v3/aj.1x1-degrees.json?secure=1',
crossOrigin: '',
}),
});
const map = new Map({
layers: [rasterLayer, vectorLayer],
target: document.getElementById('map'),
view: new View({
center: fromLonLat([2.896372, 44.6024]),
zoom: 3,
}),
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Icon Colors</title>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/[email protected]/dist/elm-pep.js"></script>
<!-- The lines below are only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v3/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,TextDecoder"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/core-js/3.18.3/minified.js"></script>
<style>
.map {
width: 100%;
height:400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script src="main.js"></script>
</body>
</html>
{
"name": "icon-color",
"dependencies": {
"ol": "6.14.1"
},
"devDependencies": {
"parcel": "^2.0.0"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --public-url . index.html"
}
}