@molecule/app-maps-leaflet 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +115 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/provider.d.ts +18 -0
- package/dist/provider.d.ts.map +1 -0
- package/dist/provider.js +240 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work.
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the
|
|
45
|
+
original version of the Work and any modifications or additions
|
|
46
|
+
to that Work, that is intentionally submitted to the Licensor for
|
|
47
|
+
inclusion in the Work by the copyright owner or by an individual or
|
|
48
|
+
Legal Entity authorized to submit on behalf of the copyright owner.
|
|
49
|
+
|
|
50
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
51
|
+
on behalf of whom a Contribution has been received by the Licensor and
|
|
52
|
+
subsequently incorporated within the Work.
|
|
53
|
+
|
|
54
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
55
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
56
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
57
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
58
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
59
|
+
Work and such Derivative Works in Source or Object form.
|
|
60
|
+
|
|
61
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
62
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
63
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
64
|
+
patent license to make, have made, use, offer to sell, sell, import,
|
|
65
|
+
and otherwise transfer the Work.
|
|
66
|
+
|
|
67
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
68
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
69
|
+
modifications, and in Source or Object form, provided that You
|
|
70
|
+
meet the following conditions:
|
|
71
|
+
|
|
72
|
+
(a) You must give any other recipients of the Work or
|
|
73
|
+
Derivative Works a copy of this License; and
|
|
74
|
+
|
|
75
|
+
(b) You must cause any modified files to carry prominent notices
|
|
76
|
+
stating that You changed the files; and
|
|
77
|
+
|
|
78
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
79
|
+
that You distribute, all copyright, patent, trademark, and
|
|
80
|
+
attribution notices from the Source form of the Work,
|
|
81
|
+
excluding those notices that do not pertain to any part of
|
|
82
|
+
the Derivative Works; and
|
|
83
|
+
|
|
84
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
85
|
+
distribution, then any Derivative Works that You distribute must
|
|
86
|
+
include a readable copy of the attribution notices contained
|
|
87
|
+
within such NOTICE file.
|
|
88
|
+
|
|
89
|
+
5. Submission of Contributions.
|
|
90
|
+
|
|
91
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
92
|
+
names, trademarks, service marks, or product names of the Licensor.
|
|
93
|
+
|
|
94
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
95
|
+
agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
|
|
96
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
|
|
97
|
+
|
|
98
|
+
8. Limitation of Liability. In no event and under no legal theory shall
|
|
99
|
+
any Contributor be liable to You for damages.
|
|
100
|
+
|
|
101
|
+
9. Accepting Warranty or Additional Liability.
|
|
102
|
+
|
|
103
|
+
Copyright 2026 Molecule Dev, Inc.
|
|
104
|
+
|
|
105
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
106
|
+
you may not use this file except in compliance with the License.
|
|
107
|
+
You may obtain a copy of the License at
|
|
108
|
+
|
|
109
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
110
|
+
|
|
111
|
+
Unless required by applicable law or agreed to in writing, software
|
|
112
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
113
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
114
|
+
See the License for the specific language governing permissions and
|
|
115
|
+
limitations under the License.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@molecule/app-maps-leaflet` — a Leaflet-backed provider for `@molecule/app-maps`.
|
|
3
|
+
* Renders real slippy maps with OpenStreetMap tiles (NO API key). Bond it once at
|
|
4
|
+
* startup and every `createMap` call shows a real map — the built-in default only
|
|
5
|
+
* paints a grey placeholder panel.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* // In your app entry, ONCE — REQUIRED: import 'leaflet/dist/leaflet.css'
|
|
10
|
+
* // (without that CSS the tiles + markers are mispositioned):
|
|
11
|
+
* import { setProvider, createMap } from '@molecule/app-maps'
|
|
12
|
+
* import { provider } from '@molecule/app-maps-leaflet'
|
|
13
|
+
* setProvider(provider)
|
|
14
|
+
*
|
|
15
|
+
* // then anywhere (the container element needs an explicit height):
|
|
16
|
+
* const el = document.getElementById('map') as HTMLElement
|
|
17
|
+
* const map = await createMap({ container: el, center: { lat: 51.5, lng: -0.12 }, zoom: 12 })
|
|
18
|
+
* map.addMarker({ id: 'a', position: { lat: 51.5, lng: -0.12 }, popup: 'Here' })
|
|
19
|
+
* // …later, on unmount: map.destroy()
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* TWO gotchas that decide whether the map is visible at all:
|
|
24
|
+
* - **Import `leaflet/dist/leaflet.css` once** (app entry). Without it, tiles and
|
|
25
|
+
* markers are mispositioned — the #1 "my Leaflet map looks broken" cause.
|
|
26
|
+
* - **The container needs an explicit height** (e.g. `style={{ height: 400 }}`); a
|
|
27
|
+
* zero-height container renders a blank map.
|
|
28
|
+
*
|
|
29
|
+
* Markers use a self-contained inline-SVG pin by default (no image-asset wiring —
|
|
30
|
+
* this sidesteps Leaflet's default-icon 404 under bundlers); pass `icon` for a
|
|
31
|
+
* custom URL. ALWAYS `destroy()` on unmount — Leaflet attaches DOM + resize
|
|
32
|
+
* listeners, and re-initialising over a live container throws "Map container is
|
|
33
|
+
* already initialized".
|
|
34
|
+
*
|
|
35
|
+
* `getSnapshot()` rejects (Leaflet has no built-in export) and `getCanvas()`
|
|
36
|
+
* returns a best-effort surface — both are WebGL-provider (Mapbox/MapLibre)
|
|
37
|
+
* concepts. Everything else (markers/polylines/polygons/circles/popups/viewport/
|
|
38
|
+
* fitBounds/project/events) renders for real.
|
|
39
|
+
*
|
|
40
|
+
* BROWSER-ONLY: draws to the DOM via Leaflet. Import + wire from app/client code.
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
export * from './provider.js';
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AAEH,cAAc,eAAe,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `@molecule/app-maps-leaflet` — a Leaflet-backed provider for `@molecule/app-maps`.
|
|
3
|
+
* Renders real slippy maps with OpenStreetMap tiles (NO API key). Bond it once at
|
|
4
|
+
* startup and every `createMap` call shows a real map — the built-in default only
|
|
5
|
+
* paints a grey placeholder panel.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* // In your app entry, ONCE — REQUIRED: import 'leaflet/dist/leaflet.css'
|
|
10
|
+
* // (without that CSS the tiles + markers are mispositioned):
|
|
11
|
+
* import { setProvider, createMap } from '@molecule/app-maps'
|
|
12
|
+
* import { provider } from '@molecule/app-maps-leaflet'
|
|
13
|
+
* setProvider(provider)
|
|
14
|
+
*
|
|
15
|
+
* // then anywhere (the container element needs an explicit height):
|
|
16
|
+
* const el = document.getElementById('map') as HTMLElement
|
|
17
|
+
* const map = await createMap({ container: el, center: { lat: 51.5, lng: -0.12 }, zoom: 12 })
|
|
18
|
+
* map.addMarker({ id: 'a', position: { lat: 51.5, lng: -0.12 }, popup: 'Here' })
|
|
19
|
+
* // …later, on unmount: map.destroy()
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @remarks
|
|
23
|
+
* TWO gotchas that decide whether the map is visible at all:
|
|
24
|
+
* - **Import `leaflet/dist/leaflet.css` once** (app entry). Without it, tiles and
|
|
25
|
+
* markers are mispositioned — the #1 "my Leaflet map looks broken" cause.
|
|
26
|
+
* - **The container needs an explicit height** (e.g. `style={{ height: 400 }}`); a
|
|
27
|
+
* zero-height container renders a blank map.
|
|
28
|
+
*
|
|
29
|
+
* Markers use a self-contained inline-SVG pin by default (no image-asset wiring —
|
|
30
|
+
* this sidesteps Leaflet's default-icon 404 under bundlers); pass `icon` for a
|
|
31
|
+
* custom URL. ALWAYS `destroy()` on unmount — Leaflet attaches DOM + resize
|
|
32
|
+
* listeners, and re-initialising over a live container throws "Map container is
|
|
33
|
+
* already initialized".
|
|
34
|
+
*
|
|
35
|
+
* `getSnapshot()` rejects (Leaflet has no built-in export) and `getCanvas()`
|
|
36
|
+
* returns a best-effort surface — both are WebGL-provider (Mapbox/MapLibre)
|
|
37
|
+
* concepts. Everything else (markers/polylines/polygons/circles/popups/viewport/
|
|
38
|
+
* fitBounds/project/events) renders for real.
|
|
39
|
+
*
|
|
40
|
+
* BROWSER-ONLY: draws to the DOM via Leaflet. Import + wire from app/client code.
|
|
41
|
+
*
|
|
42
|
+
* @module
|
|
43
|
+
*/
|
|
44
|
+
export * from './provider.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Leaflet implementation of the `@molecule/app-maps` `MapProvider`.
|
|
3
|
+
*
|
|
4
|
+
* Renders real slippy maps with OpenStreetMap raster tiles — no API key. See the
|
|
5
|
+
* module `@remarks` in `index.ts` for the required `leaflet/dist/leaflet.css`.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import type { MapProvider } from '@molecule/app-maps';
|
|
10
|
+
/**
|
|
11
|
+
* Creates a Leaflet-backed `MapProvider`.
|
|
12
|
+
*
|
|
13
|
+
* @returns A `MapProvider` that renders real OpenStreetMap-tiled maps.
|
|
14
|
+
*/
|
|
15
|
+
export declare const createProvider: () => MapProvider;
|
|
16
|
+
/** The default Leaflet map provider, ready to bond with `setProvider(provider)`. */
|
|
17
|
+
export declare const provider: MapProvider;
|
|
18
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,OAAO,KAAK,EAOV,WAAW,EAMZ,MAAM,oBAAoB,CAAA;AAmN3B;;;;GAIG;AACH,eAAO,MAAM,cAAc,QAAO,WAqBhC,CAAA;AAEF,oFAAoF;AACpF,eAAO,MAAM,QAAQ,EAAE,WAA8B,CAAA"}
|
package/dist/provider.js
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Leaflet implementation of the `@molecule/app-maps` `MapProvider`.
|
|
3
|
+
*
|
|
4
|
+
* Renders real slippy maps with OpenStreetMap raster tiles — no API key. See the
|
|
5
|
+
* module `@remarks` in `index.ts` for the required `leaflet/dist/leaflet.css`.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
import L from 'leaflet';
|
|
10
|
+
const OSM_TILES = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
|
|
11
|
+
const OSM_ATTRIBUTION = '© OpenStreetMap contributors';
|
|
12
|
+
/**
|
|
13
|
+
* Default marker icon as an inline-SVG `divIcon`. Leaflet's PNG default icons
|
|
14
|
+
* 404 under most bundlers (they resolve relative to the CSS), so a self-contained
|
|
15
|
+
* SVG pin makes markers render out-of-the-box with zero asset wiring.
|
|
16
|
+
*/
|
|
17
|
+
const defaultIcon = (color = '#2563eb') => L.divIcon({
|
|
18
|
+
className: 'mol-map-marker',
|
|
19
|
+
html: `<svg width="26" height="38" viewBox="0 0 26 38" xmlns="http://www.w3.org/2000/svg">` +
|
|
20
|
+
`<path d="M13 0C5.8 0 0 5.8 0 13c0 9.2 13 25 13 25s13-15.8 13-25C26 5.8 20.2 0 13 0z" fill="${color}"/>` +
|
|
21
|
+
`<circle cx="13" cy="13" r="5" fill="#fff"/></svg>`,
|
|
22
|
+
iconSize: [26, 38],
|
|
23
|
+
iconAnchor: [13, 38],
|
|
24
|
+
popupAnchor: [0, -34],
|
|
25
|
+
});
|
|
26
|
+
const toLatLng = (c) => [c.lat, c.lng];
|
|
27
|
+
const fromLatLng = (ll) => ({ lat: ll.lat, lng: ll.lng });
|
|
28
|
+
const toLeafletBounds = (b) => [
|
|
29
|
+
[b.sw.lat, b.sw.lng],
|
|
30
|
+
[b.ne.lat, b.ne.lng],
|
|
31
|
+
];
|
|
32
|
+
/** Builds the Leaflet marker for a molecule `MarkerConfig`. */
|
|
33
|
+
const buildMarker = (m) => {
|
|
34
|
+
const icon = m.icon
|
|
35
|
+
? L.icon({
|
|
36
|
+
iconUrl: m.icon,
|
|
37
|
+
iconSize: m.iconSize ?? [26, 38],
|
|
38
|
+
iconAnchor: m.iconAnchor ?? [13, 38],
|
|
39
|
+
})
|
|
40
|
+
: defaultIcon(m.color);
|
|
41
|
+
const marker = L.marker(toLatLng(m.position), {
|
|
42
|
+
icon,
|
|
43
|
+
draggable: m.draggable ?? false,
|
|
44
|
+
opacity: m.opacity ?? 1,
|
|
45
|
+
title: m.title,
|
|
46
|
+
});
|
|
47
|
+
if (m.popup)
|
|
48
|
+
marker.bindPopup(m.popup);
|
|
49
|
+
return marker;
|
|
50
|
+
};
|
|
51
|
+
/** Resolves the container element from a `MapConfig` (id string or element). */
|
|
52
|
+
const resolveContainer = (container) => {
|
|
53
|
+
if (typeof container === 'string') {
|
|
54
|
+
const el = document.getElementById(container);
|
|
55
|
+
if (!el)
|
|
56
|
+
throw new Error(`@molecule/app-maps-leaflet: container element "#${container}" not found`);
|
|
57
|
+
return el;
|
|
58
|
+
}
|
|
59
|
+
return container;
|
|
60
|
+
};
|
|
61
|
+
/** Wraps a live Leaflet map in the molecule `MapInstance` contract. */
|
|
62
|
+
const wrapInstance = (map) => {
|
|
63
|
+
const markers = new Map();
|
|
64
|
+
const polylines = new Map();
|
|
65
|
+
const polygons = new Map();
|
|
66
|
+
const circles = new Map();
|
|
67
|
+
return {
|
|
68
|
+
getViewport() {
|
|
69
|
+
return { center: fromLatLng(map.getCenter()), zoom: map.getZoom() };
|
|
70
|
+
},
|
|
71
|
+
setViewport(viewport) {
|
|
72
|
+
const center = viewport.center ? toLatLng(viewport.center) : map.getCenter();
|
|
73
|
+
map.setView(center, viewport.zoom ?? map.getZoom());
|
|
74
|
+
},
|
|
75
|
+
flyTo(location) {
|
|
76
|
+
const center = location.center ? toLatLng(location.center) : map.getCenter();
|
|
77
|
+
map.flyTo(center, location.zoom ?? map.getZoom());
|
|
78
|
+
},
|
|
79
|
+
fitBounds(bounds, options) {
|
|
80
|
+
map.fitBounds(toLeafletBounds(bounds), {
|
|
81
|
+
padding: options?.padding ? [options.padding, options.padding] : undefined,
|
|
82
|
+
animate: options?.animate,
|
|
83
|
+
});
|
|
84
|
+
},
|
|
85
|
+
getBounds() {
|
|
86
|
+
const b = map.getBounds();
|
|
87
|
+
return { ne: fromLatLng(b.getNorthEast()), sw: fromLatLng(b.getSouthWest()) };
|
|
88
|
+
},
|
|
89
|
+
addMarker(marker) {
|
|
90
|
+
const existing = markers.get(marker.id);
|
|
91
|
+
if (existing)
|
|
92
|
+
existing.marker.remove();
|
|
93
|
+
const built = buildMarker(marker);
|
|
94
|
+
built.addTo(map);
|
|
95
|
+
markers.set(marker.id, { marker: built, config: marker });
|
|
96
|
+
},
|
|
97
|
+
removeMarker(id) {
|
|
98
|
+
markers.get(id)?.marker.remove();
|
|
99
|
+
markers.delete(id);
|
|
100
|
+
},
|
|
101
|
+
updateMarker(id, updates) {
|
|
102
|
+
const existing = markers.get(id);
|
|
103
|
+
if (!existing)
|
|
104
|
+
return;
|
|
105
|
+
const merged = { ...existing.config, ...updates };
|
|
106
|
+
existing.marker.remove();
|
|
107
|
+
const built = buildMarker(merged);
|
|
108
|
+
built.addTo(map);
|
|
109
|
+
markers.set(id, { marker: built, config: merged });
|
|
110
|
+
},
|
|
111
|
+
getMarkers() {
|
|
112
|
+
return [...markers.values()].map((m) => m.config);
|
|
113
|
+
},
|
|
114
|
+
clearMarkers() {
|
|
115
|
+
for (const { marker } of markers.values())
|
|
116
|
+
marker.remove();
|
|
117
|
+
markers.clear();
|
|
118
|
+
},
|
|
119
|
+
addPolyline(polyline) {
|
|
120
|
+
polylines.get(polyline.id)?.remove();
|
|
121
|
+
const line = L.polyline(polyline.path.map(toLatLng), {
|
|
122
|
+
color: polyline.strokeColor,
|
|
123
|
+
weight: polyline.strokeWidth,
|
|
124
|
+
opacity: polyline.strokeOpacity,
|
|
125
|
+
dashArray: polyline.dashArray?.join(','),
|
|
126
|
+
}).addTo(map);
|
|
127
|
+
polylines.set(polyline.id, line);
|
|
128
|
+
},
|
|
129
|
+
removePolyline(id) {
|
|
130
|
+
polylines.get(id)?.remove();
|
|
131
|
+
polylines.delete(id);
|
|
132
|
+
},
|
|
133
|
+
addPolygon(polygon) {
|
|
134
|
+
polygons.get(polygon.id)?.remove();
|
|
135
|
+
const poly = L.polygon(polygon.path.map(toLatLng), {
|
|
136
|
+
fillColor: polygon.fillColor,
|
|
137
|
+
fillOpacity: polygon.fillOpacity,
|
|
138
|
+
}).addTo(map);
|
|
139
|
+
polygons.set(polygon.id, poly);
|
|
140
|
+
},
|
|
141
|
+
removePolygon(id) {
|
|
142
|
+
polygons.get(id)?.remove();
|
|
143
|
+
polygons.delete(id);
|
|
144
|
+
},
|
|
145
|
+
addCircle(circle) {
|
|
146
|
+
circles.get(circle.id)?.remove();
|
|
147
|
+
const c = L.circle(toLatLng(circle.center), { radius: circle.radius }).addTo(map);
|
|
148
|
+
circles.set(circle.id, c);
|
|
149
|
+
},
|
|
150
|
+
removeCircle(id) {
|
|
151
|
+
circles.get(id)?.remove();
|
|
152
|
+
circles.delete(id);
|
|
153
|
+
},
|
|
154
|
+
openPopup(popup) {
|
|
155
|
+
L.popup({ maxWidth: popup.maxWidth })
|
|
156
|
+
.setLatLng(toLatLng(popup.position))
|
|
157
|
+
.setContent(popup.content)
|
|
158
|
+
.openOn(map);
|
|
159
|
+
},
|
|
160
|
+
closePopups() {
|
|
161
|
+
map.closePopup();
|
|
162
|
+
},
|
|
163
|
+
on(event, handler) {
|
|
164
|
+
const wrapped = (e) => handler(e);
|
|
165
|
+
map.on(event, wrapped);
|
|
166
|
+
return () => map.off(event, wrapped);
|
|
167
|
+
},
|
|
168
|
+
off(event, handler) {
|
|
169
|
+
map.off(event, handler);
|
|
170
|
+
},
|
|
171
|
+
onMarkerClick(markerId, handler) {
|
|
172
|
+
const entry = markers.get(markerId);
|
|
173
|
+
if (!entry)
|
|
174
|
+
return () => { };
|
|
175
|
+
const wrapped = () => handler(entry.config);
|
|
176
|
+
entry.marker.on('click', wrapped);
|
|
177
|
+
return () => entry.marker.off('click', wrapped);
|
|
178
|
+
},
|
|
179
|
+
project(coordinates) {
|
|
180
|
+
const p = map.latLngToContainerPoint(toLatLng(coordinates));
|
|
181
|
+
return { x: p.x, y: p.y };
|
|
182
|
+
},
|
|
183
|
+
unproject(point) {
|
|
184
|
+
return fromLatLng(map.containerPointToLatLng(L.point(point.x, point.y)));
|
|
185
|
+
},
|
|
186
|
+
resize() {
|
|
187
|
+
map.invalidateSize();
|
|
188
|
+
},
|
|
189
|
+
getContainer() {
|
|
190
|
+
return map.getContainer();
|
|
191
|
+
},
|
|
192
|
+
getCanvas() {
|
|
193
|
+
// Leaflet renders via SVG/DOM tile layers, not a single WebGL canvas. Return
|
|
194
|
+
// the map's canvas renderer surface if one exists, else a detached canvas —
|
|
195
|
+
// getCanvas() is a WebGL-provider (Mapbox/MapLibre) concept.
|
|
196
|
+
const found = map.getContainer().querySelector('canvas');
|
|
197
|
+
return found ?? document.createElement('canvas');
|
|
198
|
+
},
|
|
199
|
+
getSnapshot() {
|
|
200
|
+
return Promise.reject(new Error('@molecule/app-maps-leaflet: getSnapshot() is not supported (Leaflet has no built-in ' +
|
|
201
|
+
'export). Use a WebGL provider (Mapbox/MapLibre) or the leaflet-image plugin.'));
|
|
202
|
+
},
|
|
203
|
+
getInstance() {
|
|
204
|
+
return map;
|
|
205
|
+
},
|
|
206
|
+
destroy() {
|
|
207
|
+
map.remove();
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
};
|
|
211
|
+
/**
|
|
212
|
+
* Creates a Leaflet-backed `MapProvider`.
|
|
213
|
+
*
|
|
214
|
+
* @returns A `MapProvider` that renders real OpenStreetMap-tiled maps.
|
|
215
|
+
*/
|
|
216
|
+
export const createProvider = () => ({
|
|
217
|
+
getName: () => 'leaflet',
|
|
218
|
+
isLoaded: () => typeof L !== 'undefined' && typeof L.map === 'function',
|
|
219
|
+
getStyles: () => [{ id: 'osm', name: 'OpenStreetMap', url: OSM_TILES }],
|
|
220
|
+
createMap(config) {
|
|
221
|
+
const container = resolveContainer(config.container);
|
|
222
|
+
const map = L.map(container, {
|
|
223
|
+
center: config.center ? toLatLng(config.center) : [0, 0],
|
|
224
|
+
zoom: config.zoom ?? 2,
|
|
225
|
+
minZoom: config.minZoom,
|
|
226
|
+
maxZoom: config.maxZoom,
|
|
227
|
+
scrollWheelZoom: config.scrollZoom ?? true,
|
|
228
|
+
doubleClickZoom: config.doubleClickZoom ?? true,
|
|
229
|
+
touchZoom: config.touchZoomRotate ?? true,
|
|
230
|
+
dragging: config.dragPan ?? true,
|
|
231
|
+
keyboard: config.keyboard ?? true,
|
|
232
|
+
});
|
|
233
|
+
L.tileLayer(OSM_TILES, { attribution: OSM_ATTRIBUTION, maxZoom: 19 }).addTo(map);
|
|
234
|
+
if (config.maxBounds)
|
|
235
|
+
map.setMaxBounds(toLeafletBounds(config.maxBounds));
|
|
236
|
+
return wrapInstance(map);
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
/** The default Leaflet map provider, ready to bond with `setProvider(provider)`. */
|
|
240
|
+
export const provider = createProvider();
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@molecule/app-maps-leaflet",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Leaflet (OpenStreetMap) provider for @molecule/app-maps",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc",
|
|
10
|
+
"test": "vitest run",
|
|
11
|
+
"test:watch": "vitest"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"keywords": [
|
|
23
|
+
"molecule",
|
|
24
|
+
"maps",
|
|
25
|
+
"leaflet",
|
|
26
|
+
"openstreetmap"
|
|
27
|
+
],
|
|
28
|
+
"license": "Apache-2.0",
|
|
29
|
+
"repository": {
|
|
30
|
+
"type": "git",
|
|
31
|
+
"directory": "packages/app/bonds/maps/leaflet"
|
|
32
|
+
},
|
|
33
|
+
"peerDependencies": {
|
|
34
|
+
"@molecule/app-maps": "^1.0.0"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@molecule/app-maps": "1.0.0",
|
|
38
|
+
"@types/leaflet": "1.9.21",
|
|
39
|
+
"@types/node": "26.1.2",
|
|
40
|
+
"typescript": "6.0.3",
|
|
41
|
+
"vitest": "4.1.10"
|
|
42
|
+
},
|
|
43
|
+
"dependencies": {
|
|
44
|
+
"leaflet": "1.9.4"
|
|
45
|
+
}
|
|
46
|
+
}
|