JSFiddle - React, Tailwind, and code Playground
by Tristen Brown
HTML
<script src="https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.tiles.mapbox.com/mapbox-gl-js/v1.1.0/mapbox-gl.css">
<div id="map"></div>
CSS
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
.marker {
background: #E9F3FF;
color: #3C77C0;
border: 2px solid #70A5E7;
border-radius: 3px;
display: inline-block;
padding: 3px 6px;
}
JavaScript
mapboxgl.accessToken = 'pk.eyJ1IjoidHJpc3RlbiIsImEiOiJjajZoOXU4Z2kwNnppMnlxd2d6bTFvZ2xtIn0.PP7AoUakMfeqdXFHdsY0oA';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/tristen/cjwurvssn93s01cny9t91anji',
center: [139.766137, 35.681416],
zoom: 16
});
map.on('render', () => {
map.queryRenderedFeatures({
layers: ['transit-entry-exit-label']
}).forEach(feature => {
const { name } = feature.properties;
feature.marker = document.createElement('div');
const position = map.project(feature.geometry.coordinates);
feature.marker.style.transform = `translate(-50%, -50%) translate(${position.x}px, ${position.y}px)`;
feature.marker.className = 'marker';
feature.marker.textContent = name;
map.getCanvasContainer().appendChild(feature.marker);
});
});