npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

geojson-map-fit-mercator

v1.1.0

Published

Finds the optimal bearing, zoom and center point for fitting a set of GeoJSON features in a Mapbox GL or LibreMap Mercator map.

Downloads

282

Readme

GeoJSON Map Fit Mercator

npm version CI Build Coverage Status FOSSA Status

GeoJSON Map Fit Mercator finds the optimal bearing, zoom and center point for fitting a set of GeoJSON features in a Mapbox GL or MapLibre GL viewport. The optimal viewport is calculated by determining the optimal bearing and zoom level to present a minimum bounding rectangle (MBR) all the given GeoJSON features. This can allow you to render more detailed, better fitting maps than the default bounding box behaviour which only describes a x/y aligned minimum bounding rectangle.

Checkout the demo to see the library in action.

Installation

NPM

npm install geojson-map-fit-mercator

Yarn

yarn add geojson-map-fit-mercator

Usage

EcmaScript Module (ESM)

import { mapFitFeatures } from 'geojson-map-fit-mercator';

CommonJS Module

const { mapFitFeatures } = require('geojson-map-fit-mercator');

Map Initialisation Example

mapFitFeatures returns an object describing the optimal center point (center), bearing (bearing) and zoom level (zoom) to display the given GeoJSON features in a given resolution map.

import { mapFitFeatures } from 'geojson-map-fit-mercator';
import maplibregl from 'maplibre-gl';
import mapData from './map-data.json' with { type: 'json' }; // Load GeoJSON data

const { bearing, center, zoom } = mapFitFeatures(mapData, [600, 400]);

const map = new new maplibregl.Map({
  container: 'map', // container ID for a 600px by 400px element
  style: 'https://demotiles.maplibre.org/style.json',
  center: center, // starting position [lng, lat]
  zoom: zoom, // starting zoom
  bearing: bearing // starting bearing
});

...

Additional options for mapFitFeatures such as map padding, zoom and bearing preferences and tile-size are described in the API section below.

API

mapFitFeatures(geojson: GeoJSON, dimensions: [number, number], options: mapFitOptions): { bearing: number, center: LonLat, zoom: number }

Finds the optimal bearing, zoom and center point for fitting all features in a map viewport.

Parameters

  • geojson: GeoJSON - A GeoJSON object containing features to fit in the map viewport.
  • dimensions: [number, number] - The dimensions of the map viewport in pixels as a [x, y] array.
  • options: mapFitOptions - Options for the map fitting algorithm and tile calculations.

Returns

  • center: LonLat - The optimal center point for the map viewport expressed as [lon, lat] array.
  • bearing: number - The optimal bearing for the map viewport.
  • zoom: number - The optimal zoom level for the map viewport. A number between 0 and options.maxZoom. If the options.floatZoom is set to false it will return only a whole number.

mapFitOptions

Options for the map fitting algorithm and tile calculations.

Properties

  • padding: mapFitPadding - The padding to apply to the map viewport. The feature fitting will scale the map down to ensure this many pixels pad the features on each side of the map. Default: { top: 0, bottom: 0, left: 0, right: 0 }.
  • maxZoom: number - The maximum zoom level to allow. Default: 23.
  • floatZoom: boolean - If true the zoom level will be a floating point number. If set to false only whole number zoom levels will be returned. Default: true.
  • preferredBearing: number - Determines which orientation of the bounding rectangle the algorithm will attempt to orient upwards. Eg. 0 will keep north pointing in an upwards angle, while 180 will mean south is pointing at an upwards angle. Default: 0.
  • tileSize: number - The size of the map tiles in pixels. By default for Mapbox GL JS and MapLibre GL JS this is 512. Default: 512.