dotted‑world‑map

How do I make a dotted world map in Svelte?

Draw the map in the component's <script> with @setkernel/dotted-map-next, and render the SVG string that getSVG() returns with {@html svg}. In SvelteKit the component is rendered on the server, so the map is in the HTML of the page.

npm install @setkernel/dotted-map-next

The component

{@html} inserts the string as HTML. That is safe here: the library escapes every text it writes and throws on a colour or a number it cannot draw.

src/lib/WorldMap.svelte
<script>
  import { DottedMap } from '@setkernel/dotted-map-next';

  const map = new DottedMap({ height: 40 });
  map.addPin({ lat: -1.2921, lng: 36.8219 });
  const svg = map.getSVG({ title: 'Nairobi on a world map' });
</script>

{@html svg}
Nairobi on a world map
Drawn by src/lib/WorldMap.svelte when this page was built.

In SvelteKit

Put the file in src/lib/ and use it from a route. With export const prerender = true in the route's +page.js, the map is drawn once, at build time.

src/routes/+page.svelte
<script>
  import WorldMap from '../lib/WorldMap.svelte';
</script>

<main>
  <WorldMap />
</main>