Skip to content

Commit 33f8f6c

Browse files
authored
Add preloaded bookmarks support for BookmarkControl and ControlGrid (#76)
- Add bookmarkOptions to ControlGridOptions so bookmark settings (including preloaded bookmarks) can be passed through addControlGrid() - Add preloaded bookmarks (SF, NYC, London, Tokyo, Paris) to both the bookmark-control and add-control-grid examples
1 parent e7ccc65 commit 33f8f6c

4 files changed

Lines changed: 130 additions & 11 deletions

File tree

examples/add-control-grid/main.ts

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,63 @@ const layerControl = new LayerControl({
4242
map.addControl(layerControl, 'top-right');
4343

4444
// Add a ControlGrid with all default controls in one call
45-
const controlGrid = addControlGrid(map, { basemapStyleUrl: BASEMAP_STYLE });
45+
const controlGrid = addControlGrid(map, {
46+
basemapStyleUrl: BASEMAP_STYLE,
47+
bookmarkOptions: {
48+
bookmarks: [
49+
{
50+
id: "san-francisco",
51+
name: "San Francisco",
52+
lng: -122.4194,
53+
lat: 37.7749,
54+
zoom: 12,
55+
pitch: 0,
56+
bearing: 0,
57+
createdAt: Date.now(),
58+
},
59+
{
60+
id: "new-york",
61+
name: "New York City",
62+
lng: -74.006,
63+
lat: 40.7128,
64+
zoom: 12,
65+
pitch: 0,
66+
bearing: 0,
67+
createdAt: Date.now(),
68+
},
69+
{
70+
id: "london",
71+
name: "London",
72+
lng: -0.1276,
73+
lat: 51.5074,
74+
zoom: 11,
75+
pitch: 0,
76+
bearing: 0,
77+
createdAt: Date.now(),
78+
},
79+
{
80+
id: "tokyo",
81+
name: "Tokyo",
82+
lng: 139.6917,
83+
lat: 35.6895,
84+
zoom: 11,
85+
pitch: 0,
86+
bearing: 0,
87+
createdAt: Date.now(),
88+
},
89+
{
90+
id: "paris",
91+
name: "Paris",
92+
lng: 2.3522,
93+
lat: 48.8566,
94+
zoom: 12,
95+
pitch: 0,
96+
bearing: 0,
97+
createdAt: Date.now(),
98+
},
99+
],
100+
},
101+
});
46102

47103
// Register data-layer adapters so COG, Zarr, PMTiles layers appear in the LayerControl
48104
for (const adapter of controlGrid.getAdapters()) {

examples/bookmark-control/main.ts

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,66 @@ const map = new maplibregl.Map({
99
});
1010

1111
map.on("load", () => {
12-
// Add the BookmarkControl with localStorage persistence
12+
// Add the BookmarkControl with localStorage persistence and preloaded bookmarks
1313
const bookmarkControl = new BookmarkControl({
1414
position: "top-right",
1515
collapsed: true,
1616
storageKey: "maplibre-bookmarks-demo",
1717
maxBookmarks: 20,
1818
flyToDuration: 2000,
1919
generateThumbnails: false,
20+
bookmarks: [
21+
{
22+
id: "san-francisco",
23+
name: "San Francisco",
24+
lng: -122.4194,
25+
lat: 37.7749,
26+
zoom: 12,
27+
pitch: 0,
28+
bearing: 0,
29+
createdAt: Date.now(),
30+
},
31+
{
32+
id: "new-york",
33+
name: "New York City",
34+
lng: -74.006,
35+
lat: 40.7128,
36+
zoom: 12,
37+
pitch: 0,
38+
bearing: 0,
39+
createdAt: Date.now(),
40+
},
41+
{
42+
id: "london",
43+
name: "London",
44+
lng: -0.1276,
45+
lat: 51.5074,
46+
zoom: 11,
47+
pitch: 0,
48+
bearing: 0,
49+
createdAt: Date.now(),
50+
},
51+
{
52+
id: "tokyo",
53+
name: "Tokyo",
54+
lng: 139.6917,
55+
lat: 35.6895,
56+
zoom: 11,
57+
pitch: 0,
58+
bearing: 0,
59+
createdAt: Date.now(),
60+
},
61+
{
62+
id: "paris",
63+
name: "Paris",
64+
lng: 2.3522,
65+
lat: 48.8566,
66+
zoom: 12,
67+
pitch: 0,
68+
bearing: 0,
69+
createdAt: Date.now(),
70+
},
71+
],
2072
});
2173

2274
map.addControl(bookmarkControl, "top-right");
@@ -38,11 +90,4 @@ map.on("load", () => {
3890
console.log("Bookmarks imported:", event.state.bookmarks.length, "total");
3991
});
4092

41-
// Add some sample bookmarks if none exist
42-
if (bookmarkControl.getBookmarks().length === 0) {
43-
// Navigate to SF and add bookmark
44-
map.once("moveend", () => {
45-
bookmarkControl.addBookmark("San Francisco");
46-
});
47-
}
4893
});

src/lib/core/ControlGrid.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ type OptionalControlGridFields =
6464
| "defaultControls"
6565
| "basemapStyleUrl"
6666
| "excludeLayers"
67-
| "streetViewOptions";
67+
| "streetViewOptions"
68+
| "bookmarkOptions";
6869

6970
/** ControlGrid options with required fields except for optional ones */
7071
type ResolvedControlGridOptions = Required<
@@ -96,6 +97,7 @@ const DEFAULT_OPTIONS: ResolvedControlGridOptions = {
9697
basemapStyleUrl: undefined,
9798
excludeLayers: undefined,
9899
streetViewOptions: undefined,
100+
bookmarkOptions: undefined,
99101
};
100102

101103
/** Wrench icon SVG for collapsed state – stroke style matching MapLibre globe icon. */
@@ -369,7 +371,10 @@ export class ControlGrid implements IControl {
369371
case "measure":
370372
return new MeasureControl({ collapsed: true });
371373
case "bookmark":
372-
return new BookmarkControl({ collapsed: true });
374+
return new BookmarkControl({
375+
collapsed: true,
376+
...this._options.bookmarkOptions,
377+
});
373378
case "print":
374379
return new PrintControl({ collapsed: true });
375380
case "zarrLayer":

src/lib/core/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,19 @@ export interface ControlGridOptions {
13981398
* from Vite env vars (`VITE_GOOGLE_MAPS_API_KEY`, `VITE_MAPILLARY_ACCESS_TOKEN`).
13991399
*/
14001400
streetViewOptions?: Partial<StreetViewControlOptions>;
1401+
/**
1402+
* Optional Bookmark control options (e.g. preloaded bookmarks).
1403+
*
1404+
* @example
1405+
* ```typescript
1406+
* bookmarkOptions: {
1407+
* bookmarks: [
1408+
* { id: 'nyc', name: 'New York', lng: -74.006, lat: 40.7128, zoom: 12, pitch: 0, bearing: 0, createdAt: Date.now() },
1409+
* ],
1410+
* }
1411+
* ```
1412+
*/
1413+
bookmarkOptions?: Partial<BookmarkControlOptions>;
14011414
}
14021415

14031416
/**

0 commit comments

Comments
 (0)