Leaflet GeometryUtil

0.10.3 · active · verified Sun Apr 19

Leaflet GeometryUtil is a utility library that extends Leaflet's core functionality with a wide array of geometric operations. It provides functions for calculating distances, bearings between points, interpolating points along polylines, locating points on lines, finding closest points or layers to a given geographic coordinate, rotating points around a center, and managing layers within a specified radius. The library seamlessly integrates with Leaflet's `L.LatLng` and layer objects, providing a cohesive API for complex geospatial computations directly within Leaflet applications. The current stable version is 0.10.3, released in December 2023, indicating active maintenance with several minor and patch releases per year to add features and address bugs. A key differentiator is its deep integration with the Leaflet ecosystem, offering a comprehensive set of geometric tools that feel native to Leaflet, rather than a standalone geometry library. It has included robust TypeScript definitions since version 0.10.0, significantly enhancing the developer experience for TypeScript projects by providing strong type checking and intellisense for all its utilities.

Common errors

Warnings

Install

Imports

Quickstart

Initializes a Leaflet map, adds a polyline, then demonstrates finding the closest point on that polyline to a given `L.LatLng` and interpolating a point at 50% along the line's length using `GeometryUtil.closest` and `GeometryUtil.interpolateOnLine`.

import L from 'leaflet';
import GeometryUtil from 'leaflet-geometryutil';
import 'leaflet/dist/leaflet.css';

// Ensure you have a div with id="map" in your HTML
// <div id="map" style="height: 400px; width: 600px;"></div>

const map = L.map('map').setView([51.505, -0.09], 13);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '© OpenStreetMap contributors'
}).addTo(map);

// Define a polyline
const polylinePoints = [
  [51.505, -0.09],
  [51.51, -0.08],
  [51.50, -0.05],
  [51.515, -0.07]
];
const polyline = L.polyline(polylinePoints, {color: 'blue'}).addTo(map);

// Example 1: Find the closest point on the polyline to a given point
const testPoint = L.latLng(51.51, -0.10);
L.marker(testPoint).addTo(map).bindPopup('Test Point').openPopup();

const closestPointResult = GeometryUtil.closest(map, polyline, testPoint);
if (closestPointResult) {
  L.marker(closestPointResult.latlng, {
    icon: L.divIcon({ className: 'my-div-icon', html: 'Closest' })
  }).addTo(map);
  console.log('Closest point on polyline:', closestPointResult.latlng.toString());
} else {
  console.log('No closest point found (polyline might be empty).');
}

// Example 2: Interpolate a point on the line at 50% distance
const interpolatedPointResult = GeometryUtil.interpolateOnLine(map, polyline, 0.5);
if (interpolatedPointResult) {
  L.circleMarker(interpolatedPointResult.latlng, {
    radius: 5,
    color: 'red'
  }).addTo(map).bindPopup('Interpolated Point (50%)');
  console.log('Interpolated point on polyline:', interpolatedPointResult.latlng.toString());
}

view raw JSON →