How to add a custom icon (PNG) to a point layer
This example shows how to make a map with pins to display a point layer from a MapTiler Tileset using a custom PNG icon.
Download the underground sample icon, The icon is from the Maps Icons Collection.
NPM module setup
npm install --save @maptiler/sdk
import { Map, MapStyle, config } from '@maptiler/sdk';
import '@maptiler/sdk/dist/maptiler-sdk.css';
config.apiKey = 'YOUR_MAPTILER_API_KEY_HERE';
const map = new Map({
container: 'map',
style: MapStyle.BASE,
center: [-73.9066, 40.7314], // starting position [lng, lat]
zoom: 10.21, // starting zoom
});
map.on('load', async function() {
const image = await map.loadImage('./underground.png');
map.addImage('pinMetro', image.data);
map.addSource('points', {
type: 'vector',
url: "https://api.maptiler.com/tiles/9537e49b-39c3-43ce-a6b9-9cf3d26b6d93/tiles.json"
});
map.addLayer({
'id': 'points',
'type': 'symbol',
'source': 'points',
'source-layer': 'stations',
'layout': {
'icon-image': 'pinMetro',
'icon-size': ['*', ['get', 'scalerank'] ,0.01]
},
'paint': {}
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>Add custom icon (PNG) to a point layer</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div id="map"></div>
<script type="module" src="./main.js"></script>
</body>
</html>
body {margin: 0; padding: 0;}
#map {position: absolute; top: 0; bottom: 0; width: 100%;}
Replace YOUR_MAPTILER_API_KEY_HERE with your own API key. Make sure to secure the key before you publish it.
Replace YOUR_MAPTILER_TILESET_ID_HERE with your actual MapTiler Tileset ID.
Replace YOUR_TILESET_LAYER_HERE with your actual Layer ID. How to get the Layer ID.