Distances, bearings and polygons on a round earth.
Geolib is a small JavaScript library for working with coordinates. It has
no dependencies and comes with TypeScript types. Your bundle only includes
the functions you import: about 1 kB for getDistance,
under 5 kB for all 41.
npm install geolib
Getting started
Install
npm install geolib
Or use yarn add geolib or pnpm add geolib.
Import
Geolib ships as an ES module and as CommonJS. Import the functions you need:
import { getDistance, getCenter } from 'geolib';
const { getDistance, getCenter } = require('geolib');
Bundlers leave out everything you don’t import. If you prefer a namespace,
import * as geolib from 'geolib' works as well.
Without a build step
Load the script from a CDN. All functions are available on window.geolib:
<script src="https://cdn.jsdelivr.net/npm/geolib@4"></script>
Or import it as an ES module:
import { getDistance } from 'https://cdn.jsdelivr.net/npm/geolib@4/+esm';
TypeScript
Geolib is written in TypeScript and includes its type definitions. They need TypeScript 4.7 or newer. You can import the types, too:
import type { GeolibInputCoordinates } from 'geolib';
Points
Every function that takes a point accepts an object with a
latitude or lat property and a
longitude, lng or lon property,
or a GeoJSON array in [longitude, latitude] order. Values can
be numbers, numeric strings or sexagesimal strings. Your objects can have
any other properties as well.
Each example on this page is editable and runs again as you type. The result of the last expression is shown below the code.
Upgrading from version 3
Most code keeps working. The changes that might affect you:
-
Deep imports like
geolib/es/getDistanceare gone. Import fromgeolibinstead; unused functions are still left out of your bundle. -
There is no default export. Use named imports or
import * as geolib from 'geolib'. - The browser build moved to
dist/geolib.iife.jsand no longer supports AMD. -
computeDestinationPointnow uses the same earth radius as every other function, and some functions return slightly different results after bug fixes.