code
Snippet
language
CDN
package_2
NPM
content_copy
Copy
open_in_new
Open
var features = [];
for ( var i = 0 ; i < 50 ; i ++ ) {
features. push ( new ol. Feature ({
geometry: new ol.geom. Point (ol.proj. fromLonLat ([ 2.2 + Math. random () * 0.3 , 48.8 + Math. random () * 0.1 ])),
category: [ 'restaurant' , 'hotel' , 'museum' ][Math. floor (Math. random () * 3 )],
rating: Math. floor (Math. random () * 5 ) + 1
}));
}
var colors = { restaurant: '#e63946' , hotel: '#2196F3' , museum: '#9C27B0' };
var layer = new ol.layer. Vector ({
source: new ol.source. Vector ({ features: features }),
style : function ( feature ) {
var cat = feature. get ( 'category' );
var r = feature. get ( 'rating' );
return new ol.style. Style ({
image: new ol.style. Circle ({
radius: 4 + r * 2 ,
fill: new ol.style. Fill ({ color: colors[cat] }),
stroke: new ol.style. Stroke ({ color: '#fff' , width: 1.5 })
})
});
}
});
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.35 , 48.85 ]), zoom: 13 })
}); content_copy
Copy
open_in_new
Open
<! doctype html >
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width, initial-scale=1.0" >
< title >Data-Driven Styling with OpenLayers</ title >
< link rel = "stylesheet" href = "https://unpkg.com/ol@10.11.0/ol.css" >
< script src = "https://unpkg.com/ol@10.11.0/dist/ol.js" ></ script >
< style >
body { margin : 0 ; padding : 0 ; }
#map { width : 100 % ; height : 100 vh ; }
</ style >
</ head >
< body >
< div id = "map" ></ div >
< script >
var features = [];
for ( var i = 0 ; i < 50 ; i ++ ) {
features. push ( new ol. Feature ({
geometry: new ol.geom. Point (ol.proj. fromLonLat ([ 2.2 + Math. random () * 0.3 , 48.8 + Math. random () * 0.1 ])),
category: [ 'restaurant' , 'hotel' , 'museum' ][Math. floor (Math. random () * 3 )],
rating: Math. floor (Math. random () * 5 ) + 1
}));
}
var colors = { restaurant: '#e63946' , hotel: '#2196F3' , museum: '#9C27B0' };
var layer = new ol.layer. Vector ({
source: new ol.source. Vector ({ features: features }),
style : function ( feature ) {
var cat = feature. get ( 'category' );
var r = feature. get ( 'rating' );
return new ol.style. Style ({
image: new ol.style. Circle ({
radius: 4 + r * 2 ,
fill: new ol.style. Fill ({ color: colors[cat] }),
stroke: new ol.style. Stroke ({ color: '#fff' , width: 1.5 })
})
});
}
});
var map = new ol. Map ({
target: 'map' ,
layers: [
new ol.layer. Tile ({ source: new ol.source. XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new ol. View ({ center: ol.proj. fromLonLat ([ 2.35 , 48.85 ]), zoom: 13 })
});
</ script >
</ body >
</ html > content_copy
Copy
open_in_new
Open
import Map from 'ol/Map' ;
import View from 'ol/View' ;
import Feature from 'ol/Feature' ;
import TileLayer from 'ol/layer/Tile' ;
import VectorLayer from 'ol/layer/Vector' ;
import XYZ from 'ol/source/XYZ' ;
import VectorSource from 'ol/source/Vector' ;
import Style from 'ol/style/Style' ;
import Fill from 'ol/style/Fill' ;
import Stroke from 'ol/style/Stroke' ;
import CircleStyle from 'ol/style/Circle' ;
import Point from 'ol/geom/Point' ;
import { fromLonLat } from 'ol/proj' ;
import 'ol/ol.css' ;
var features = [];
for ( var i = 0 ; i < 50 ; i ++ ) {
features. push ( new Feature ({
geometry: new Point ( fromLonLat ([ 2.2 + Math. random () * 0.3 , 48.8 + Math. random () * 0.1 ])),
category: [ 'restaurant' , 'hotel' , 'museum' ][Math. floor (Math. random () * 3 )],
rating: Math. floor (Math. random () * 5 ) + 1
}));
}
var colors = { restaurant: '#e63946' , hotel: '#2196F3' , museum: '#9C27B0' };
var layer = new VectorLayer ({
source: new VectorSource ({ features: features }),
style : function ( feature ) {
var cat = feature. get ( 'category' );
var r = feature. get ( 'rating' );
return new Style ({
image: new CircleStyle ({
radius: 4 + r * 2 ,
fill: new Fill ({ color: colors[cat] }),
stroke: new Stroke ({ color: '#fff' , width: 1.5 })
})
});
}
});
var map = new Map ({
target: 'map' ,
layers: [
new TileLayer ({ source: new XYZ ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY' , attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new View ({ center: fromLonLat ([ 2.35 , 48.85 ]), zoom: 13 })
});
var features = [];
for (var i = 0; i < 50; i++) {
features.push(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([2.2 + Math.random() * 0.3, 48.8 + Math.random() * 0.1])),
category: ['restaurant', 'hotel', 'museum'][Math.floor(Math.random() * 3)],
rating: Math.floor(Math.random() * 5) + 1
}));
}
var colors = { restaurant: '#e63946', hotel: '#2196F3', museum: '#9C27B0' };
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: function(feature) {
var cat = feature.get('category');
var r = feature.get('rating');
return new ol.style.Style({
image: new ol.style.Circle({
radius: 4 + r * 2,
fill: new ol.style.Fill({ color: colors[cat] }),
stroke: new ol.style.Stroke({ color: '#fff', width: 1.5 })
})
});
}
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new ol.View({ center: ol.proj.fromLonLat([2.35, 48.85]), zoom: 13 })
}); <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data-Driven Styling with OpenLayers</title>
<link rel="stylesheet" href="https://unpkg.com/ol@10.11.0/ol.css">
<script src="https://unpkg.com/ol@10.11.0/dist/ol.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var features = [];
for (var i = 0; i < 50; i++) {
features.push(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([2.2 + Math.random() * 0.3, 48.8 + Math.random() * 0.1])),
category: ['restaurant', 'hotel', 'museum'][Math.floor(Math.random() * 3)],
rating: Math.floor(Math.random() * 5) + 1
}));
}
var colors = { restaurant: '#e63946', hotel: '#2196F3', museum: '#9C27B0' };
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: function(feature) {
var cat = feature.get('category');
var r = feature.get('rating');
return new ol.style.Style({
image: new ol.style.Circle({
radius: 4 + r * 2,
fill: new ol.style.Fill({ color: colors[cat] }),
stroke: new ol.style.Stroke({ color: '#fff', width: 1.5 })
})
});
}
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new ol.View({ center: ol.proj.fromLonLat([2.35, 48.85]), zoom: 13 })
});
</script>
</body>
</html> import Map from 'ol/Map';
import View from 'ol/View';
import Feature from 'ol/Feature';
import TileLayer from 'ol/layer/Tile';
import VectorLayer from 'ol/layer/Vector';
import XYZ from 'ol/source/XYZ';
import VectorSource from 'ol/source/Vector';
import Style from 'ol/style/Style';
import Fill from 'ol/style/Fill';
import Stroke from 'ol/style/Stroke';
import CircleStyle from 'ol/style/Circle';
import Point from 'ol/geom/Point';
import { fromLonLat } from 'ol/proj';
import 'ol/ol.css';
var features = [];
for (var i = 0; i < 50; i++) {
features.push(new Feature({
geometry: new Point(fromLonLat([2.2 + Math.random() * 0.3, 48.8 + Math.random() * 0.1])),
category: ['restaurant', 'hotel', 'museum'][Math.floor(Math.random() * 3)],
rating: Math.floor(Math.random() * 5) + 1
}));
}
var colors = { restaurant: '#e63946', hotel: '#2196F3', museum: '#9C27B0' };
var layer = new VectorLayer({
source: new VectorSource({ features: features }),
style: function(feature) {
var cat = feature.get('category');
var r = feature.get('rating');
return new Style({
image: new CircleStyle({
radius: 4 + r * 2,
fill: new Fill({ color: colors[cat] }),
stroke: new Stroke({ color: '#fff', width: 1.5 })
})
});
}
});
var map = new Map({
target: 'map',
layers: [
new TileLayer({ source: new XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=YOUR_MAPTILER_KEY', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new View({ center: fromLonLat([2.35, 48.85]), zoom: 13 })
});
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Data-Driven Styling with OpenLayers</title>
<link rel="stylesheet" href="https://unpkg.com/ol@10.11.0/ol.css">
<script src="https://unpkg.com/ol@10.11.0/dist/ol.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { width: 100%; height: 100vh; }
</style>
</head>
<body>
<div id="map"></div>
<script>
var features = [];
for (var i = 0; i < 50; i++) {
features.push(new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([2.2 + Math.random() * 0.3, 48.8 + Math.random() * 0.1])),
category: ['restaurant', 'hotel', 'museum'][Math.floor(Math.random() * 3)],
rating: Math.floor(Math.random() * 5) + 1
}));
}
var colors = { restaurant: '#e63946', hotel: '#2196F3', museum: '#9C27B0' };
var layer = new ol.layer.Vector({
source: new ol.source.Vector({ features: features }),
style: function(feature) {
var cat = feature.get('category');
var r = feature.get('rating');
return new ol.style.Style({
image: new ol.style.Circle({
radius: 4 + r * 2,
fill: new ol.style.Fill({ color: colors[cat] }),
stroke: new ol.style.Stroke({ color: '#fff', width: 1.5 })
})
});
}
});
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({ source: new ol.source.XYZ({ url: 'https://api.maptiler.com/maps/streets-v4/256/{z}/{x}/{y}.png?key=LKv9WPh4aJggV1CHU4Y8', attributions: '© <a href="https://www.maptiler.com/copyright/">MapTiler</a>' }) }),
layer
],
view: new ol.View({ center: ol.proj.fromLonLat([2.35, 48.85]), zoom: 13 })
});
</script>
</body>
</html>