Add an icon to the map
Add an icon onto the map by utilizing an external URL and incorporate it into a symbol layer as specified in the style specification for layers.
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.STREETS,
center: [0, 0],
zoom: 2,
});
map.on("load", async function () {
const image = await map.loadImage(
"https://upload.wikimedia.org/wikipedia/commons/7/7c/201408_cat.png",
);
map.addImage("cat", image.data);
map.addSource("point", {
type: "geojson",
data: {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [0, 0],
},
},
],
},
});
map.addLayer({
id: "points",
type: "symbol",
source: "point",
layout: {
"icon-image": "cat",
"icon-size": 0.25,
},
});
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="robots" content="noindex">
<title>Add an icon to the map | NPM example</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%;}