49 |
56 |
57 |
58 | import { mapFitFeatures } from 'geojson-map-fit-mercator';
59 | import maplibregl from 'maplibre-gl';
60 | import mapData from './map-data.json' with { type: 'json' }; // Load GeoJSON data
61 |
62 | const { bearing, center, zoom } = mapFitFeatures(
63 | mapData,
64 | [600, 300]
65 | );
66 |
67 | const map = new new maplibregl.Map({
68 | container: 'map', // container ID for a 600px by 400px element
69 | style: 'https://demotiles.maplibre.org/style.json',
70 | center: center, // starting position [lng, lat]
71 | zoom: zoom, // starting zoom
72 | bearing: bearing // starting bearing
73 | });
74 |
75 | ...
76 |
77 |
78 |
79 |
80 | import { mapFitFeatures } from 'geojson-map-fit-mercator';
81 | import mapboxgl from 'mapbox-gl';
82 | import mapData from './map-data.json' with { type: 'json' }; // Load GeoJSON data
83 |
84 | const { bearing, center, zoom } = mapFitFeatures(
85 | mapData,
86 | [600, 300]
87 | );
88 |
89 | const map = new new mapboxgl.Map({
90 | container: 'map', // container ID for a 600px by 400px element
91 | center: center, // starting position [lng, lat]
92 | zoom: zoom, // starting zoom
93 | bearing: bearing // starting bearing
94 | });
95 |
96 | ...
97 |
98 |
99 |
100 |
101 | #### Padding
102 |
103 | You can add padding around the features in the viewport by passing an object to the `padding` option of `mapFitFeatures`.
104 | This padding object is identical to the [Mapbox GL JS `paddingOptions` object](https://docs.mapbox.com/mapbox-gl-js/api/properties/#paddingoptions) and [MapLibre GL JS `paddingOptions` object](https://maplibre.org/maplibre-gl-js/docs/API/type-aliases/PaddingOptions/).
105 |
106 | ```javascript
107 | mapFitFeatures(
108 | mapData,
109 | [600, 300],
110 | {
111 | padding: {
112 | top: 50,
113 | bottom: 50,
114 | left: 100,
115 | right: 100
116 | }
117 | }
118 | );
119 | ```
120 |
121 |