Render street names.
Example showing the use of a text style with placement: 'line'
to render text along a path.
<!DOCTYPE html>
<html>
<head>
<title>Street Labels</title>
<link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
</head>
<body>
<div id="map" class="map"></div>
<script>
import Map from 'ol/Map.js';
import View from 'ol/View.js';
import {getCenter} from 'ol/extent.js';
import GeoJSON from 'ol/format/GeoJSON.js';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
import BingMaps from 'ol/source/BingMaps.js';
import VectorSource from 'ol/source/Vector.js';
import {Fill, Style, Text} from 'ol/style.js';
var style = new Style({
text: new Text({
font: 'bold 11px "Open Sans", "Arial Unicode MS", "sans-serif"',
placement: 'line',
fill: new Fill({
color: 'white'
})
})
});
var viewExtent = [1817379, 6139595, 1827851, 6143616];
var map = new Map({
layers: [new TileLayer({
source: new BingMaps({
key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here',
imagerySet: 'Aerial'
})
}), new VectorLayer({
declutter: true,
source: new VectorSource({
format: new GeoJSON(),
url: 'data/geojson/vienna-streets.geojson'
}),
style: function(feature) {
style.getText().setText(feature.get('name'));
return style;
}
})],
target: 'map',
view: new View({
extent: viewExtent,
center: getCenter(viewExtent),
zoom: 17,
minZoom: 14
})
});
</script>
</body>
</html>