dotted‑world‑map

How do I put a pin on a world map at a latitude and longitude?

Call map.addPin({ lat, lng }) before map.getSVG(): the pin snaps to the nearest point of the grid and is drawn twice the size of a dot, in the map's colour. Give it svgOptions for its own colour and size, and data for your own fields, which addPin() returns with the pin's position.

npm install @setkernel/dotted-map-next

The default pin

lat and lng are in degrees, north and east positive. A pin with no svgOptions is drawn at twice the map's radius: 0.6 on dots of 0.3.

pin.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40 });
map.addPin({ lat: -33.8688, lng: 151.2093 });
const svg = map.getSVG({ title: 'Sydney on a world map' });
Sydney on a world map
Drawn by pin.mjs when this page was built.

Colours, sizes and data

svgOptions.color takes any CSS colour, and svgOptions.radius is in grid units, as the map's radius is. data is not drawn: it stays with the pin. addPin() returns the pin as a point, with the x, y, lat and lng it snapped to and your data, and getPoints() lists it with the dots, for a list or a tooltip beside the map.

offices.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const offices = [
  { name: 'New York', lat: 40.7128, lng: -74.006 },
  { name: 'London', lat: 51.5072, lng: -0.1276 },
  { name: 'Singapore', lat: 1.3521, lng: 103.8198 },
];

const map = new DottedMap({ height: 40 });
for (const { name, lat, lng } of offices) {
  map.addPin({
    lat,
    lng,
    data: { name },
    svgOptions: { color: '#e8590c', radius: 0.7 },
  });
}
const svg = map.getSVG({ color: '#8c959f', title: 'Our offices' });
Our offices
Drawn by offices.mjs when this page was built.

In TypeScript, new DottedMap<{ name: string }>(…) types each pin's data.

Pins outside the land

A pin at sea is drawn at its place on the grid, where there are no dots. With avoidOuterPins: true, addPin() returns undefined for a pin outside the map's countries, and draws nothing.

at-sea.mjs
import { DottedMap } from '@setkernel/dotted-map-next';

const map = new DottedMap({ height: 40, avoidOuterPins: true });
map.addPin({ lat: 48.8566, lng: 2.3522 }); // Paris: drawn
map.addPin({ lat: 0, lng: -30 }); // the Atlantic: undefined, not drawn
const svg = map.getSVG({ title: 'Paris on a world map' });
Paris on a world map
Drawn by at-sea.mjs when this page was built.