Example demonstrating how to get feature properties from vector tiles

This commit is contained in:
Tim Schaub
2017-11-06 09:35:44 -07:00
parent 4ea72c2483
commit 8a6fe81f09
3 changed files with 61 additions and 0 deletions

View File

@@ -0,0 +1,16 @@
#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;
}

View File

@@ -0,0 +1,11 @@
---
layout: example.html
title: Vector Tile Info
shortdesc: Getting feature information from vector tiles.
docs: >
<p>Move your pointer over rendered features to display feature properties.</p>
tags: "vector tiles"
---
<div id="map" class="map">
<pre id="info"/>
</div>

View File

@@ -0,0 +1,34 @@
goog.require('ol.Map');
goog.require('ol.View');
goog.require('ol.format.MVT');
goog.require('ol.layer.VectorTile');
goog.require('ol.source.VectorTile');
var map = new ol.Map({
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
}),
layers: [new ol.layer.VectorTile({
source: new ol.source.VectorTile({
format: new ol.format.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) {
info.innerText = '';
info.style.opacity = 0;
return;
}
var properties = features[0].getProperties();
info.innerText = JSON.stringify(properties, null, 2);
info.style.opacity = 1;
}