Add custom icons with Markers
Use Marker to add custom icons to your map.
See the example: https://docs.mapbox.com//mapbox-gl-js/example/custom-marker-icons/
by Swapnil Navalakha
HTML
<script src="https://api.mapbox.com/mapbox-gl-js/v1.10.1/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v1.10.1/mapbox-gl.css">
<div id="map"></div>
CSS
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
.marker {
display: block;
border: none;
border-radius: 50%;
cursor: pointer;
padding: 0;
}
.cls-1 {
fill: #fff;
}
.cls-1, .cls-2 {
stroke: #707070;
}
.cls-2, .cls-4 {
fill: none;
}
.cls-3 {
stroke: none;
}
JavaScript
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'pk.eyJ1Ijoic2phbW9ubmEiLCJhIjoiY2o4YXcxczVlMDhyMDJxbnlhMTcycWpjZCJ9.3HTi8RSFUAM9dv7FtK3eag';
const imageURL = `https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/-122.337798,37.810550,9.67,0.00,0.00/200x250@2x?access_token=${mapboxgl.accessToken}`
var geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'properties': {
'message': 'Foo',
'iconSize': [60, 60]
},
'geometry': {
'type': 'Point',
'coordinates': [-66.324462890625, -16.024695711685304]
}
},
{
'type': 'Feature',
'properties': {
'message': 'Bar',
'iconSize': [50, 50]
},
'geometry': {
'type': 'Point',
'coordinates': [-61.2158203125, -15.97189158092897]
}
},
{
'type': 'Feature',
'properties': {
'message': 'Baz',
'iconSize': [40, 40]
},
'geometry': {
'type': 'Point',
'coordinates': [-63.29223632812499, -18.28151823530889]
}
}
]
};
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [-65.017, -16.457],
zoom: 5
});
// add markers to map
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="100"...