Geolocation and Maps in JavaScript (Node.js): Libraries and Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Geolocation and Maps in JavaScript (Node.js)

Working with geographic data and map display is one of the key tasks of modern web applications. The Node.js ecosystem has many libraries for processing coordinates, building routes, performing reverse geocoding, and rendering interactive maps. In this article, we will review the main tools and provide practical code examples for the server side (Node.js).



1. Main Libraries for Working with Geodata



1.1. Leaflet (client-side) and node-leaflet

Leaflet is the most popular JavaScript library for interactive maps. It works on the browser side, but for Node.js there is the leaflet package (via npm install leaflet), which allows rendering maps on the server using leaflet-headless or using data in GeoJSON format.

Example of generating a map as an image on the server (using leaflet-static):

const L = require('leaflet');const { createCanvas } = require('canvas');

const map = L.map(createCanvas(800, 600), { center: [55.751244, 37.618423], // Moscow zoom: 10});

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: '© OpenStreetMap contributors'}).addTo(map);

// Export to PNG (simplified)const buffer = map.getCanvas().toBuffer();require('fs').writeFileSync('map.png', buffer);


1.2. OpenStreetMap (OSM) and Overpass API

OpenStreetMap provides free tiles and an API for searching for objects. In Node.js, the osmosis package or direct HTTP requests to https://nominatim.openstreetmap.org are often used to work with OSM.

Example of reverse geocoding (getting an address from coordinates):

const fetch = require('node-fetch');

async function reverseGeocode(lat, lon) { const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lon}`; const response = await fetch(url, { headers: { 'User-Agent': 'PythonLibExample/1.0' } }); const data = await response.json(); return data.display_name;}

reverseGeocode(55.751244, 37.618423).then(addr => console.log(addr));// Result: "Moscow, Russia, Red Square"


1.3. Turf.js — Geospatial Analysis

Turf.js is a powerful library for performing geometric operations: buffering, polygon merging, distance calculation, area calculation, etc. It works both in the browser and in Node.js.

Example of calculating the distance between two points (in kilometers):

const turf = require('@turf/turf');

const point1 = turf.point([37.618423, 55.751244]); // [lon, lat]const point2 = turf.point([30.315868, 59.939095]); // Saint Petersburg

const distance = turf.distance(point1, point2, { units: 'kilometers' });console.log(`Distance: ${distance.toFixed(2)} km`); // ≈ 634.5 km


1.4. GeoJSON and shapefile

GeoJSON is a format for storing geographic structures (points, lines, polygons). In Node.js, the built-in JSON.parse is sufficient for working with it, and the shapefile package is used for reading shapefiles.

const shapefile = require('shapefile');

shapefile.open('path/to/file.shp') .then(source => source.read() .then(function log(result) { if (result.done) return; console.log(result.value); // GeoJSON Feature return source.read().then(log); })) .catch(error => console.error(error));


2. Advanced Tools



2.1. Mapbox GL JS (Node.js)

Mapbox provides a commercial platform with highly detailed maps. For server-side rendering, use @mapbox/mapbox-gl-native. An API key is required.



2.2. Nominatim (Geocoding)

Recommendations