Getting feature information from vector tiles.
Move your pointer over rendered features to display feature properties.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Vector Tile Info</title>
<!-- 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>
<style>
.map {
width: 100%;
height:400px;
}
#map {
position: relative;
}
#info {
z-index: 1;
opacity: 0;
position: absolute;
bottom: 0;
left: 0;
margin: 0;
background: rgba(0,60,136,0.7);
color: white;
border: 0;
transition: opacity 100ms ease-in;
}
</style>
</head>
<body>
<div id="map" class="map">
<pre id="info"/>
</div>
<script src="index.js"></script>
</body>
</html>
import 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import MVT from 'ol/format/MVT';
import VectorTileLayer from 'ol/layer/VectorTile';
import VectorTileSource from 'ol/source/VectorTile';
var map = new Map({
target: 'map',
view: new View({
center: [0, 0],
zoom: 2
}),
layers: [new VectorTileLayer({
source: new VectorTileSource({
format: new MVT(),
url: 'https://basemaps.arcgis.com/v1/arcgis/rest/services/World_Basemap/VectorTileServer/tile/{z}/{y}/{x}.pbf'
})
})]
});
map.on('pointermove', showInfo);
var info = document.getElementById('info');
function showInfo(event) {
var features = map.getFeaturesAtPixel(event.pixel);
if (features.length == 0) {
info.innerText = '';
info.style.opacity = 0;
return;
}
var properties = features[0].getProperties();
info.innerText = JSON.stringify(properties, null, 2);
info.style.opacity = 1;
}
{
"name": "vector-tile-info",
"dependencies": {
"ol": "6.1.1"
},
"devDependencies": {
"parcel": "1.11.0"
},
"scripts": {
"start": "parcel index.html",
"build": "parcel build --experimental-scope-hoisting --public-url . index.html"
}
}