Created
June 26, 2018 01:44
-
-
Save stevage/96066b4b8914ee40309260340db509eb to your computer and use it in GitHub Desktop.
mapbox on.load vs queryRenderedFeatures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset='utf-8' /> | |
<title>Display a map</title> | |
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' /> | |
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.46.0/mapbox-gl.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.46.0/mapbox-gl.css' rel='stylesheet' /> | |
<style> | |
body { margin:0; padding:0; } | |
.rounded-label { | |
background: white; | |
padding: 0.25em 0.5em; | |
font-size: 16pt; | |
border-radius: 1em; | |
border:1px solid lightgrey; | |
} | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<div id='map'></div> | |
<script> | |
function countCities(event) { | |
var labelLayers = ['place-city-lg-n', 'place-city-lg-s', 'place-city-md-n', 'place-city-md-s', 'place-city-sm']; | |
var places = map.queryRenderedFeatures({ layers: labelLayers}); | |
console.log('[' + event + '] ', places.length, ' city labels found'); | |
} | |
mapboxgl.accessToken = 'pk.eyJ1Ijoic3RldmFnZSIsImEiOiJGcW03aExzIn0.QUkUmTGIO3gGt83HiRIjQw'; | |
var map = new mapboxgl.Map({ | |
container: 'map', | |
style: 'mapbox://styles/mapbox/streets-v9', | |
center: [-74.50, 40], | |
zoom: 9 | |
}); | |
map.on('load', () => countCities('load')); | |
map.on('render', () => countCities('render')); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment