@geolonia/maps-react 0.5.0 → 0.6.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/README.md CHANGED
@@ -71,7 +71,7 @@ Root component. Creates a Geolonia map instance and provides it to children via
71
71
  | `minZoom` / `maxZoom` | `number` | Zoom range |
72
72
  | `gestureHandling` | `boolean` | Gesture handling on scrollable pages |
73
73
  | `loader` | `boolean` | Loading animation |
74
- | `geojson` | `string \| FeatureCollection` | GeoJSON (or its URL) drawn with simplestyle. `<Map>` always passes a `center` (default `[0, 0]`), so the view is not fitted to the data: set `center` and `zoom` yourself |
74
+ | `geojson` | `string \| FeatureCollection` | GeoJSON (or its URL) drawn with simplestyle. Without `center`, the view is fitted to the data (`zoom` is only the starting zoom before the fit). With `center`, it is used as is |
75
75
  | `cluster` | `boolean` | Cluster the point features of `geojson` (default `true`). Ignored when `heatmap` or `circle` is set: those never cluster |
76
76
  | `clusterColor` | `string` | Color of the cluster markers |
77
77
  | `choropleth` | `string \| ChoroplethOptions` | Color the polygons of `geojson` by a property value. A property name, or `{ property, colors?, range? }` |
@@ -86,7 +86,7 @@ Root component. Creates a Geolonia map instance and provides it to children via
86
86
 
87
87
  `ControlPosition` = `'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'`
88
88
 
89
- `geojson`, `cluster`, `clusterColor`, `choropleth`, `heatmap` and `circle` are read once when the map is created. Changing them afterwards has no effect, so remount `<Map>` with a `key` to change them. To update GeoJSON data on a live map, use `<Source>` and `<Layer>` below.
89
+ `geojson`, `cluster`, `clusterColor`, `choropleth`, `heatmap` and `circle` are read once when the map is created. Changing them afterwards has no effect, so remount `<Map>` with a `key` to change them. To change them on a live map, use `<SimpleStyle>` below.
90
90
 
91
91
  ```tsx
92
92
  <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12} geojson={meshes} choropleth="population" />
@@ -129,6 +129,29 @@ Displays a standalone popup at the given coordinates.
129
129
  | `onClose` | `() => void` | Callback when the user closes the popup (close button, click on the map, or `closeOnMove`). Not called when the popup or the whole `<Map>` unmounts |
130
130
  | `children` | `ReactNode` | Popup content |
131
131
 
132
+ ### Controls
133
+
134
+ The controls below each have a component that adds the control to the map and removes it when it unmounts. Except for `<GeocoderControl>` (see [below](#geocodercontrol)), all of them take `position` (`'top-left'`, `'top-right'`, `'bottom-left'` or `'bottom-right'`) and the options of the MapLibre control (`<GlobeControl>` has no options besides `position`). Changing a prop adds the control again; object-valued props (such as `positionOptions`) are compared by reference, so define them outside the render or memoize them.
135
+
136
+ | Component | Default `position` | MapLibre control |
137
+ |---|---|---|
138
+ | `<NavigationControl>` | `'top-right'` | `NavigationControl` (zoom and compass) |
139
+ | `<GeolocateControl>` | `'top-right'` | `GeolocateControl` |
140
+ | `<FullscreenControl>` | `'top-right'` | `FullscreenControl` |
141
+ | `<GlobeControl>` | `'top-right'` | `GlobeControl` (switches the projection between mercator and globe) |
142
+ | `<GeocoderControl>` | `'top-left'` | A search box for Japanese addresses, not a MapLibre control. See [`<GeocoderControl>`](#geocodercontrol) |
143
+ | `<ScaleControl>` | `'bottom-left'` | `ScaleControl` |
144
+ | `<AttributionControl>` | `'bottom-right'` | `AttributionControl` |
145
+
146
+ ```tsx
147
+ <Map apiKey="YOUR-API-KEY">
148
+ <NavigationControl position="bottom-right" />
149
+ <GlobeControl position="bottom-left" />
150
+ </Map>
151
+ ```
152
+
153
+ For any other MapLibre control, use `useControl(() => new SomeControl(), { position })`. To put your own React elements in a control, use `<Control position onAdd onRemove>`: its `children` are rendered inside the control's container.
154
+
132
155
  ### `<Source>` / `<Layer>`
133
156
 
134
157
  Declaratively add MapLibre sources and layers. Nesting `<Layer>` inside `<Source>` automatically inherits the source ID.
@@ -143,7 +166,69 @@ Declaratively add MapLibre sources and layers. Nesting `<Layer>` inside `<Source
143
166
  </Source>
144
167
  ```
145
168
 
146
- `<Layer>` props match `LayerSpecification` from MapLibre GL, plus `beforeId` for insertion order.
169
+ `<Layer>` props match `LayerSpecification` from MapLibre GL, plus `beforeId` for insertion order. Inside `<Source>`, `source` can be omitted. Outside `<Source>`, pass `source` explicitly; a `<Layer>` without a source (except `type="background"`) throws an error that names the layer. When the map's `style` changes, `<Source>` and `<Layer>` are added back to the new style from their current props. A `<Layer>` outside `<Source>` is added back once its `source` exists in the new style.
170
+
171
+ ### `<SimpleStyle>`
172
+
173
+ Draws GeoJSON on the map the way `<Map geojson>` does, but as a child of `<Map>`, so it follows later changes without recreating the map.
174
+
175
+ ```tsx
176
+ <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12}>
177
+ <SimpleStyle geojson={spots} heatmap="weight" fitBounds />
178
+ </Map>
179
+ ```
180
+
181
+ | Prop | Type | Description |
182
+ |---|---|---|
183
+ | `geojson` | `string \| FeatureCollection` | GeoJSON, or its URL. Changing it updates the data on the live map (the view is not moved) |
184
+ | `id` | `string` | Prefix of the source and layer IDs. Default `'geolonia-simple-style'`, the one `<Map geojson>` uses: give each `<SimpleStyle>` on a map, and any `<Map geojson>`, its own |
185
+ | `cluster`, `clusterColor`, `choropleth`, `heatmap`, `circle` | same as `<Map>` | Changing one rebuilds the layers from the current data. Object values are compared by content, so a new literal does not rebuild |
186
+ | `fitBounds` | `boolean` | Fit the view to the data when the layers are added (also when rebuilt). Default `false` |
187
+
188
+ The layers are removed when the component unmounts, and added again after the map's `style` changes.
189
+
190
+ ### `<GeocoderControl>`
191
+
192
+ A search box on the map for Japanese addresses. Submitting an address moves the map to it and puts a marker there. Addresses are read with [`@geolonia/normalize-japanese-addresses`](https://github.com/geolonia/normalize-japanese-addresses), a dependency of this package. It carries no address data: each search fetches what it needs from the address data API.
193
+
194
+ ```tsx
195
+ <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={10}>
196
+ <GeocoderControl
197
+ onResult={(result) => console.log(result.address, result.center)}
198
+ onError={(error) => console.warn(error.code)}
199
+ />
200
+ </Map>
201
+ ```
202
+
203
+ | Prop | Type | Description |
204
+ |---|---|---|
205
+ | `position` | `ControlPosition` | Default `'top-left'`. Read when the control is added: to change it later, change the component's `key` |
206
+ | `placeholder` | `string` | Placeholder and label of the input. Default `'住所を入力'` |
207
+ | `marker` | `boolean` | Put a marker at the result. Default `true` |
208
+ | `fitBounds` | `boolean` | Move the map to the bounds of the result. Default `true` |
209
+ | `onResult` | `(result: GeocodeResult, address: string) => void` | Called when an address is found. `address` is what was typed |
210
+ | `onError` | `(error: GeocodeError) => void` | Called when a search fails. `error.code` is `ZERO_RESULTS` (no position found), `INVALID_REQUEST` (empty input) or `ERROR` (the address data could not be loaded) |
211
+
212
+ The input has `aria-invalid` after a failed search, until it is edited. When searches overlap, only the latest one changes the map. The control and the marker are removed when the component unmounts.
213
+
214
+ To build your own search UI, call `geocode(address)`. It returns a `GeocodeResult` and rejects with a `GeocodeError`.
215
+
216
+ ```ts
217
+ import { geocode } from '@geolonia/maps-react';
218
+
219
+ const { center, bounds, level, address, partial } = await geocode('東京都台東区上野公園7-7');
220
+ map.fitBounds(bounds);
221
+ ```
222
+
223
+ | Field | Description |
224
+ |---|---|
225
+ | `center` | `[lng, lat]` |
226
+ | `bounds` | `[[west, south], [east, north]]`, to pass to `fitBounds()`. Its size follows `level` |
227
+ | `level` | Accuracy of `center`: `1` prefecture, `2` city, `3` town or chome, `8` house number or lot |
228
+ | `address` | The address down to what `center` stands for. For a town representative point, the street number is left out |
229
+ | `partial` | `true` when `center` is coarser than the input, or text that is not an address (a building name, for example) was left over |
230
+
231
+ Reverse geocoding is not supported.
147
232
 
148
233
  ### `useMap()`
149
234
 
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import * as react from 'react';
2
2
  import { ReactNode, CSSProperties } from 'react';
3
3
  import { Map as Map$1, AttributionControlOptions, FullscreenControlOptions, GeolocateControlOptions, NavigationControlOptions, ScaleControlOptions, IControl, LayerSpecification, PointLike, PositionAnchor, Alignment, SourceSpecification, CanvasSourceSpecification } from 'maplibre-gl';
4
- import { GeoloniaMapOptions, GeoloniaMap } from '@geolonia/maps-core';
5
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
+ import { GeoloniaMapOptions, GeoloniaMap } from '@geolonia/maps-core';
6
6
 
7
7
  type ControlPosition = "top-left" | "top-right" | "bottom-left" | "bottom-right";
8
8
  type ControlProps = {
@@ -13,31 +13,102 @@ type ControlProps = {
13
13
  };
14
14
  declare function Control({ position, onAdd, onRemove, children, }: ControlProps): react.ReactPortal | null;
15
15
 
16
- declare function useControl<T extends IControl>(factory: () => T, options?: {
16
+ type AttributionControlProps = AttributionControlOptions & {
17
17
  position?: ControlPosition;
18
- }): void;
19
- type NavigationControlProps = NavigationControlOptions & {
18
+ };
19
+ declare function AttributionControl({ position, compact, customAttribution, }?: AttributionControlProps): null;
20
+
21
+ type FullscreenControlProps = FullscreenControlOptions & {
20
22
  position?: ControlPosition;
21
23
  };
22
- declare function NavigationControl({ position, showCompass, showZoom, visualizePitch, }?: NavigationControlProps): null;
24
+ declare function FullscreenControl({ position, container, }?: FullscreenControlProps): null;
25
+
26
+ /** `geocode()` が reject するときの `code`。 */
27
+ type GeocodeErrorCode =
28
+ /** 入力が空です。 */
29
+ "INVALID_REQUEST"
30
+ /** 住所を特定できませんでした。 */
31
+ | "ZERO_RESULTS"
32
+ /** 住所データの取得に失敗しました。 */
33
+ | "ERROR";
34
+ /** `geocode()` が失敗したときに reject される Error です。 */
35
+ declare class GeocodeError extends Error {
36
+ readonly code: GeocodeErrorCode;
37
+ constructor(message: string, code: GeocodeErrorCode);
38
+ }
39
+ type GeocodeResult = {
40
+ /** 位置。[経度, 緯度] の順です。 */
41
+ center: [number, number];
42
+ /** 結果を表示するのに適した範囲。[[西, 南], [東, 北]] の順で、`fitBounds()` にそのまま渡せます。 */
43
+ bounds: [[number, number], [number, number]];
44
+ /**
45
+ * `center` の正確さ。1 都道府県、2 市区町村、3 丁目・町字、
46
+ * 8 住居表示住所・地番の代表点です。
47
+ */
48
+ level: number;
49
+ /** 位置が表す場所までの住所。たとえば位置が町字の代表点なら、番地は含みません。 */
50
+ address: string;
51
+ /**
52
+ * 入力の住所全体には一致しなかった場合に `true` です。位置が入力より粗い代表点
53
+ * だった場合や、住所として読めない文字列(建物名など)が残った場合です。
54
+ */
55
+ partial: boolean;
56
+ };
57
+ /**
58
+ * 日本の住所を検索して、位置と表示に適した範囲を返します。
59
+ *
60
+ * 住所の解釈には `@geolonia/normalize-japanese-addresses` を使います。住所データは
61
+ * 検索のたびに、住所データ API から必要な分だけ取得します。
62
+ *
63
+ * 失敗すると、`code` を持つ {@link GeocodeError} で reject します。
64
+ */
65
+ declare function geocode(address: string): Promise<GeocodeResult>;
66
+
67
+ type GeocoderControlProps = {
68
+ position?: ControlPosition;
69
+ placeholder?: string;
70
+ /** 結果の位置にマーカーを置く。既定は true。 */
71
+ marker?: boolean;
72
+ /** 結果の範囲へ地図を動かす。既定は true。 */
73
+ fitBounds?: boolean;
74
+ /** 住所が見つかったときに呼ばれる。`address` は入力された文字列。 */
75
+ onResult?: (result: GeocodeResult, address: string) => void;
76
+ /** 検索に失敗したときに呼ばれる。`error.code` で原因を区別できる。 */
77
+ onError?: (error: GeocodeError) => void;
78
+ };
79
+ declare function GeocoderControl({ position, placeholder, marker, fitBounds, onResult, onError, }?: GeocoderControlProps): react_jsx_runtime.JSX.Element;
80
+
23
81
  type GeolocateControlProps = GeolocateControlOptions & {
24
82
  position?: ControlPosition;
25
83
  };
26
84
  declare function GeolocateControl({ position, positionOptions, fitBoundsOptions, trackUserLocation, showAccuracyCircle, showUserLocation, }?: GeolocateControlProps): null;
27
- type FullscreenControlProps = FullscreenControlOptions & {
85
+
86
+ type GlobeControlProps = {
28
87
  position?: ControlPosition;
29
88
  };
30
- declare function FullscreenControl({ position, container, }?: FullscreenControlProps): null;
89
+ /** A button that switches the map projection between mercator and globe. */
90
+ declare function GlobeControl({ position, }?: GlobeControlProps): null;
91
+
92
+ type NavigationControlProps = NavigationControlOptions & {
93
+ position?: ControlPosition;
94
+ };
95
+ declare function NavigationControl({ position, showCompass, showZoom, visualizePitch, }?: NavigationControlProps): null;
96
+
31
97
  type ScaleControlProps = ScaleControlOptions & {
32
98
  position?: ControlPosition;
33
99
  };
34
100
  declare function ScaleControl({ position, maxWidth, unit, }?: ScaleControlProps): null;
35
- type AttributionControlProps = AttributionControlOptions & {
101
+
102
+ declare function useControl<T extends IControl>(factory: () => T, options?: {
36
103
  position?: ControlPosition;
37
- };
38
- declare function AttributionControl({ position, compact, customAttribution, }?: AttributionControlProps): null;
104
+ }): void;
39
105
 
40
- type LayerProps = LayerSpecification & {
106
+ type OptionalSource<T> = T extends {
107
+ source: unknown;
108
+ } ? Omit<T, "source"> & {
109
+ source?: string;
110
+ } : T;
111
+ type LayerProps = OptionalSource<LayerSpecification> & {
41
112
  beforeId?: string;
42
113
  };
43
114
  declare function Layer({ beforeId, ...layerSpec }: LayerProps): null;
@@ -150,6 +221,20 @@ type PopupProps = {
150
221
  };
151
222
  declare function Popup(props: PopupProps): react.ReactPortal;
152
223
 
224
+ type SimpleStyleOptions = Pick<GeoloniaMapOptions, "cluster" | "clusterColor" | "choropleth" | "heatmap" | "circle">;
225
+ type SimpleStyleProps = SimpleStyleOptions & {
226
+ /** GeoJSON to draw, or the URL of one. Changing it updates the data on the live map. */
227
+ geojson: NonNullable<GeoloniaMapOptions["geojson"]>;
228
+ /**
229
+ * Prefix of the source and layer IDs. Give each `<SimpleStyle>` on a map its
230
+ * own. Default `"geolonia-simple-style"`, the one `<Map geojson>` uses.
231
+ */
232
+ id?: string;
233
+ /** Fit the view to the data when it is added to the map. Default `false`. */
234
+ fitBounds?: boolean;
235
+ };
236
+ declare function SimpleStyle({ geojson, id, cluster, clusterColor, choropleth, heatmap, circle, fitBounds, }: SimpleStyleProps): null;
237
+
153
238
  type SourceProps = (SourceSpecification | CanvasSourceSpecification) & {
154
239
  id: string;
155
240
  children?: ReactNode;
@@ -173,4 +258,5 @@ declare function useMaps(): MapCollection;
173
258
  declare const SourceContext: react.Context<string | null>;
174
259
  declare function useSourceId(): string | null;
175
260
 
176
- export { AttributionControl, type AttributionControlProps, Control, type ControlPosition, type ControlProps, FullscreenControl, type FullscreenControlProps, GeolocateControl, type GeolocateControlProps, Layer, type LayerProps, Map, type MapCollection, MapContext, type MapEventProps, type MapProps, MapProvider, MapProviderContext, type MapProviderProps, type MapRegistry, Marker, type MarkerProps, NavigationControl, type NavigationControlProps, Popup, type PopupProps, ScaleControl, type ScaleControlProps, Source, SourceContext, type SourceProps, useControl, useMap, useMaps, useSourceId };
261
+ export { AttributionControl, Control, FullscreenControl, GeocodeError, GeocoderControl, GeolocateControl, GlobeControl, Layer, Map, MapContext, MapProvider, MapProviderContext, Marker, NavigationControl, Popup, ScaleControl, SimpleStyle, Source, SourceContext, geocode, useControl, useMap, useMaps, useSourceId };
262
+ export type { AttributionControlProps, ControlPosition, ControlProps, FullscreenControlProps, GeocodeErrorCode, GeocodeResult, GeocoderControlProps, GeolocateControlProps, GlobeControlProps, LayerProps, MapCollection, MapEventProps, MapProps, MapProviderProps, MapRegistry, MarkerProps, NavigationControlProps, PopupProps, ScaleControlProps, SimpleStyleProps, SourceProps };
package/dist/index.js CHANGED
@@ -64,20 +64,11 @@ function Control({
64
64
  return container ? createPortal(children, container) : null;
65
65
  }
66
66
 
67
- // src/components/controls.tsx
67
+ // src/components/controls/attribution-control.tsx
68
68
  import * as maplibregl from "maplibre-gl";
69
69
  import { useEffect as useEffect2 } from "react";
70
- function useControl(factory, options) {
71
- const map = useMap();
72
- const position = options?.position;
73
- useEffect2(() => {
74
- const ctrl = factory();
75
- map.addControl(ctrl, position);
76
- return () => {
77
- safeRemoveControl(map, ctrl);
78
- };
79
- }, [map, position]);
80
- }
70
+
71
+ // src/components/controls/defined.ts
81
72
  function defined(obj) {
82
73
  const out = {};
83
74
  for (const k of Object.keys(obj)) {
@@ -85,24 +76,332 @@ function defined(obj) {
85
76
  }
86
77
  return out;
87
78
  }
88
- function NavigationControl2({
89
- position = "top-right",
90
- showCompass,
91
- showZoom,
92
- visualizePitch
79
+
80
+ // src/components/controls/attribution-control.tsx
81
+ function AttributionControl2({
82
+ position = "bottom-right",
83
+ compact,
84
+ customAttribution
93
85
  } = {}) {
94
86
  const map = useMap();
95
87
  useEffect2(() => {
96
- const ctrl = new maplibregl.NavigationControl(
97
- defined({ showCompass, showZoom, visualizePitch })
88
+ const ctrl = new maplibregl.AttributionControl(
89
+ defined({ compact, customAttribution })
98
90
  );
99
91
  map.addControl(ctrl, position);
100
92
  return () => {
101
93
  safeRemoveControl(map, ctrl);
102
94
  };
103
- }, [map, position, showCompass, showZoom, visualizePitch]);
95
+ }, [map, position, compact, customAttribution]);
104
96
  return null;
105
97
  }
98
+
99
+ // src/components/controls/fullscreen-control.tsx
100
+ import * as maplibregl2 from "maplibre-gl";
101
+ import { useEffect as useEffect3 } from "react";
102
+ function FullscreenControl2({
103
+ position = "top-right",
104
+ container
105
+ } = {}) {
106
+ const map = useMap();
107
+ useEffect3(() => {
108
+ const ctrl = new maplibregl2.FullscreenControl(defined({ container }));
109
+ map.addControl(ctrl, position);
110
+ return () => {
111
+ safeRemoveControl(map, ctrl);
112
+ };
113
+ }, [map, position, container]);
114
+ return null;
115
+ }
116
+
117
+ // src/components/controls/geocoder-control.tsx
118
+ import { useEffect as useEffect5, useRef as useRef2, useState as useState2 } from "react";
119
+
120
+ // src/geocode.ts
121
+ var GeocodeError = class extends Error {
122
+ constructor(message, code) {
123
+ super(message);
124
+ this.name = "GeocodeError";
125
+ this.code = code;
126
+ }
127
+ };
128
+ var HALF_SPAN = {
129
+ 1: 180 / 2 ** 9,
130
+ 2: 180 / 2 ** 12,
131
+ 3: 180 / 2 ** 15,
132
+ 8: 180 / 2 ** 18
133
+ };
134
+ var TIMEOUT_MS = 1e4;
135
+ function isValidPoint(p) {
136
+ return Number.isFinite(p.lat) && Number.isFinite(p.lng) && Math.abs(p.lat) <= 90 && Math.abs(p.lng) <= 180;
137
+ }
138
+ function cityPoint(r) {
139
+ const lngLat = r.metadata?.city?.point;
140
+ if (!r.city || !lngLat) return void 0;
141
+ return { lat: lngLat[1], lng: lngLat[0], level: 2 };
142
+ }
143
+ function toResult(r, point) {
144
+ const parts = [
145
+ [r.addr ?? "", 8],
146
+ [r.town ?? "", 3],
147
+ [r.city ?? "", 2],
148
+ [r.pref ?? "", 1]
149
+ ];
150
+ const parsed = parts.filter(([name]) => name);
151
+ const included = parsed.filter(([, level]) => level <= point.level);
152
+ const half = HALF_SPAN[point.level] ?? HALF_SPAN[1];
153
+ const parsedLevel = parsed[0]?.[1] ?? 0;
154
+ return {
155
+ center: [point.lng, point.lat],
156
+ bounds: [
157
+ [point.lng - half, point.lat - half],
158
+ [point.lng + half, point.lat + half]
159
+ ],
160
+ level: point.level,
161
+ address: included.map(([name]) => name).reverse().join(""),
162
+ partial: point.level < parsedLevel || (r.other ?? "").trim() !== ""
163
+ };
164
+ }
165
+ async function geocode(address) {
166
+ const input = typeof address === "string" ? address.trim() : "";
167
+ if (!input) {
168
+ throw new GeocodeError(
169
+ "geocode: an address is required.",
170
+ "INVALID_REQUEST"
171
+ );
172
+ }
173
+ let r;
174
+ let timer;
175
+ try {
176
+ const load = async () => {
177
+ const { normalize } = await import("@geolonia/normalize-japanese-addresses");
178
+ return normalize(input);
179
+ };
180
+ const timeout = new Promise((_, reject) => {
181
+ timer = setTimeout(
182
+ () => reject(new Error(`no answer in ${TIMEOUT_MS / 1e3}s`)),
183
+ TIMEOUT_MS
184
+ );
185
+ });
186
+ r = await Promise.race([load(), timeout]);
187
+ } catch (e) {
188
+ throw new GeocodeError(
189
+ `geocode: failed to load address data (${e instanceof Error ? e.message : String(e)}).`,
190
+ "ERROR"
191
+ );
192
+ } finally {
193
+ clearTimeout(timer);
194
+ }
195
+ const point = r.point ?? cityPoint(r);
196
+ if (!point) {
197
+ throw new GeocodeError(
198
+ `geocode: no result for "${input}".`,
199
+ "ZERO_RESULTS"
200
+ );
201
+ }
202
+ if (!isValidPoint(point)) {
203
+ throw new GeocodeError(
204
+ `geocode: the address data has invalid coordinates for "${input}".`,
205
+ "ERROR"
206
+ );
207
+ }
208
+ return toResult(r, point);
209
+ }
210
+
211
+ // src/components/marker.tsx
212
+ import { GeoloniaMarker } from "@geolonia/maps-core";
213
+ import * as maplibregl3 from "maplibre-gl";
214
+ import { useEffect as useEffect4, useRef } from "react";
215
+ import { createPortal as createPortal2 } from "react-dom";
216
+ function Marker(props) {
217
+ const {
218
+ lat,
219
+ lng,
220
+ color,
221
+ openPopup,
222
+ draggable,
223
+ rotation,
224
+ offset,
225
+ anchor,
226
+ rotationAlignment,
227
+ pitchAlignment,
228
+ className,
229
+ children
230
+ } = props;
231
+ const map = useMap();
232
+ const markerRef = useRef(null);
233
+ const popupEl = useRef(document.createElement("div"));
234
+ const handlersRef = useRef({});
235
+ handlersRef.current = {
236
+ onClick: props.onClick,
237
+ onDragStart: props.onDragStart,
238
+ onDrag: props.onDrag,
239
+ onDragEnd: props.onDragEnd
240
+ };
241
+ useEffect4(() => {
242
+ const marker = new GeoloniaMarker({
243
+ color,
244
+ draggable,
245
+ rotation,
246
+ offset,
247
+ anchor,
248
+ rotationAlignment,
249
+ pitchAlignment,
250
+ className
251
+ }).setLngLat([lng, lat]).addTo(map);
252
+ if (children) {
253
+ marker.setPopup(
254
+ new maplibregl3.Popup({ offset: [0, -30] }).setDOMContent(
255
+ popupEl.current
256
+ )
257
+ );
258
+ if (openPopup) marker.togglePopup();
259
+ }
260
+ const onClickWrapper = (e) => {
261
+ handlersRef.current.onClick?.(e);
262
+ };
263
+ const onDragStartWrapper = (e) => {
264
+ handlersRef.current.onDragStart?.(e);
265
+ };
266
+ const onDragWrapper = (e) => {
267
+ handlersRef.current.onDrag?.(e);
268
+ };
269
+ const onDragEndWrapper = (e) => {
270
+ handlersRef.current.onDragEnd?.(e);
271
+ };
272
+ const element = marker.getElement();
273
+ element.addEventListener("click", onClickWrapper);
274
+ marker.on("dragstart", onDragStartWrapper);
275
+ marker.on("drag", onDragWrapper);
276
+ marker.on("dragend", onDragEndWrapper);
277
+ markerRef.current = marker;
278
+ return () => {
279
+ element.removeEventListener("click", onClickWrapper);
280
+ marker.off("dragstart", onDragStartWrapper);
281
+ marker.off("drag", onDragWrapper);
282
+ marker.off("dragend", onDragEndWrapper);
283
+ marker.remove();
284
+ };
285
+ }, [map, color, anchor, className]);
286
+ useEffect4(() => {
287
+ markerRef.current?.setLngLat([lng, lat]);
288
+ }, [lat, lng]);
289
+ useEffect4(() => {
290
+ if (draggable === void 0) return;
291
+ markerRef.current?.setDraggable(draggable);
292
+ }, [draggable]);
293
+ useEffect4(() => {
294
+ if (rotation === void 0) return;
295
+ markerRef.current?.setRotation(rotation);
296
+ }, [rotation]);
297
+ useEffect4(() => {
298
+ if (offset === void 0) return;
299
+ markerRef.current?.setOffset(offset);
300
+ }, [offset]);
301
+ useEffect4(() => {
302
+ if (rotationAlignment === void 0) return;
303
+ markerRef.current?.setRotationAlignment(rotationAlignment);
304
+ }, [rotationAlignment]);
305
+ useEffect4(() => {
306
+ if (pitchAlignment === void 0) return;
307
+ markerRef.current?.setPitchAlignment(pitchAlignment);
308
+ }, [pitchAlignment]);
309
+ useEffect4(() => {
310
+ const marker = markerRef.current;
311
+ const popup = marker?.getPopup();
312
+ if (!popup) return;
313
+ const isOpen = popup.isOpen();
314
+ if (!!openPopup !== isOpen) marker?.togglePopup();
315
+ }, [openPopup]);
316
+ return children ? createPortal2(children, popupEl.current) : null;
317
+ }
318
+
319
+ // src/components/controls/geocoder-control.tsx
320
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
321
+ var DEFAULT_LABEL = "\u4F4F\u6240\u3092\u5165\u529B";
322
+ function createContainer() {
323
+ const div = document.createElement("div");
324
+ div.className = "maplibregl-ctrl maplibregl-ctrl-group maps-react-geocoder";
325
+ return div;
326
+ }
327
+ function GeocoderControl({
328
+ position = "top-left",
329
+ placeholder = DEFAULT_LABEL,
330
+ marker = true,
331
+ fitBounds = true,
332
+ onResult,
333
+ onError
334
+ } = {}) {
335
+ const map = useMap();
336
+ const [input, setInput] = useState2("");
337
+ const [found, setFound] = useState2(null);
338
+ const [failed, setFailed] = useState2(false);
339
+ const seqRef = useRef2(0);
340
+ const aliveRef = useRef2(true);
341
+ useEffect5(() => {
342
+ aliveRef.current = true;
343
+ return () => {
344
+ aliveRef.current = false;
345
+ };
346
+ }, []);
347
+ const latest = useRef2({ fitBounds, onResult, onError });
348
+ useEffect5(() => {
349
+ latest.current = { fitBounds, onResult, onError };
350
+ });
351
+ async function search(e) {
352
+ e.preventDefault();
353
+ const address = input;
354
+ const seq = ++seqRef.current;
355
+ let result;
356
+ try {
357
+ result = await geocode(address);
358
+ } catch (err) {
359
+ if (!aliveRef.current || seq !== seqRef.current) return;
360
+ setFailed(true);
361
+ latest.current.onError?.(
362
+ err instanceof GeocodeError ? err : new GeocodeError(String(err), "ERROR")
363
+ );
364
+ return;
365
+ }
366
+ if (!aliveRef.current || seq !== seqRef.current) return;
367
+ setFailed(false);
368
+ setFound(result);
369
+ if (latest.current.fitBounds) map.fitBounds(result.bounds);
370
+ latest.current.onResult?.(result, address);
371
+ }
372
+ return /* @__PURE__ */ jsxs(Fragment, { children: [
373
+ /* @__PURE__ */ jsx(Control, { position, onAdd: createContainer, children: /* @__PURE__ */ jsxs(
374
+ "form",
375
+ {
376
+ onSubmit: search,
377
+ style: { display: "flex", gap: 4, padding: 4, background: "#fff" },
378
+ children: [
379
+ /* @__PURE__ */ jsx(
380
+ "input",
381
+ {
382
+ type: "search",
383
+ value: input,
384
+ placeholder,
385
+ "aria-label": placeholder || DEFAULT_LABEL,
386
+ "aria-invalid": failed,
387
+ onChange: (e) => {
388
+ setInput(e.target.value);
389
+ setFailed(false);
390
+ },
391
+ style: { border: "1px solid #ccc", padding: "4px 8px" }
392
+ }
393
+ ),
394
+ /* @__PURE__ */ jsx("button", { type: "submit", children: "\u691C\u7D22" })
395
+ ]
396
+ }
397
+ ) }),
398
+ marker && found && /* @__PURE__ */ jsx(Marker, { lng: found.center[0], lat: found.center[1] })
399
+ ] });
400
+ }
401
+
402
+ // src/components/controls/geolocate-control.tsx
403
+ import * as maplibregl4 from "maplibre-gl";
404
+ import { useEffect as useEffect6 } from "react";
106
405
  function GeolocateControl2({
107
406
  position = "top-right",
108
407
  positionOptions,
@@ -112,8 +411,8 @@ function GeolocateControl2({
112
411
  showUserLocation
113
412
  } = {}) {
114
413
  const map = useMap();
115
- useEffect2(() => {
116
- const ctrl = new maplibregl.GeolocateControl(
414
+ useEffect6(() => {
415
+ const ctrl = new maplibregl4.GeolocateControl(
117
416
  defined({
118
417
  positionOptions,
119
418
  fitBoundsOptions,
@@ -137,55 +436,75 @@ function GeolocateControl2({
137
436
  ]);
138
437
  return null;
139
438
  }
140
- function FullscreenControl2({
141
- position = "top-right",
142
- container
143
- } = {}) {
439
+
440
+ // src/components/controls/globe-control.tsx
441
+ import * as maplibregl5 from "maplibre-gl";
442
+
443
+ // src/components/controls/use-control.ts
444
+ import { useEffect as useEffect7 } from "react";
445
+ function useControl(factory, options) {
144
446
  const map = useMap();
145
- useEffect2(() => {
146
- const ctrl = new maplibregl.FullscreenControl(defined({ container }));
447
+ const position = options?.position;
448
+ useEffect7(() => {
449
+ const ctrl = factory();
147
450
  map.addControl(ctrl, position);
148
451
  return () => {
149
452
  safeRemoveControl(map, ctrl);
150
453
  };
151
- }, [map, position, container]);
454
+ }, [map, position]);
455
+ }
456
+
457
+ // src/components/controls/globe-control.tsx
458
+ function GlobeControl2({
459
+ position = "top-right"
460
+ } = {}) {
461
+ useControl(() => new maplibregl5.GlobeControl(), { position });
152
462
  return null;
153
463
  }
154
- function ScaleControl2({
155
- position = "bottom-left",
156
- maxWidth,
157
- unit
464
+
465
+ // src/components/controls/navigation-control.tsx
466
+ import * as maplibregl6 from "maplibre-gl";
467
+ import { useEffect as useEffect8 } from "react";
468
+ function NavigationControl2({
469
+ position = "top-right",
470
+ showCompass,
471
+ showZoom,
472
+ visualizePitch
158
473
  } = {}) {
159
474
  const map = useMap();
160
- useEffect2(() => {
161
- const ctrl = new maplibregl.ScaleControl(defined({ maxWidth, unit }));
475
+ useEffect8(() => {
476
+ const ctrl = new maplibregl6.NavigationControl(
477
+ defined({ showCompass, showZoom, visualizePitch })
478
+ );
162
479
  map.addControl(ctrl, position);
163
480
  return () => {
164
481
  safeRemoveControl(map, ctrl);
165
482
  };
166
- }, [map, position, maxWidth, unit]);
483
+ }, [map, position, showCompass, showZoom, visualizePitch]);
167
484
  return null;
168
485
  }
169
- function AttributionControl2({
170
- position = "bottom-right",
171
- compact,
172
- customAttribution
486
+
487
+ // src/components/controls/scale-control.tsx
488
+ import * as maplibregl7 from "maplibre-gl";
489
+ import { useEffect as useEffect9 } from "react";
490
+ function ScaleControl2({
491
+ position = "bottom-left",
492
+ maxWidth,
493
+ unit
173
494
  } = {}) {
174
495
  const map = useMap();
175
- useEffect2(() => {
176
- const ctrl = new maplibregl.AttributionControl(
177
- defined({ compact, customAttribution })
178
- );
496
+ useEffect9(() => {
497
+ const ctrl = new maplibregl7.ScaleControl(defined({ maxWidth, unit }));
179
498
  map.addControl(ctrl, position);
180
499
  return () => {
181
500
  safeRemoveControl(map, ctrl);
182
501
  };
183
- }, [map, position, compact, customAttribution]);
502
+ }, [map, position, maxWidth, unit]);
184
503
  return null;
185
504
  }
186
505
 
187
506
  // src/components/layer.tsx
188
- import { useEffect as useEffect3, useRef } from "react";
507
+ import { useEffect as useEffect10, useRef as useRef3 } from "react";
189
508
 
190
509
  // src/source-context.ts
191
510
  import { createContext as createContext2, useContext as useContext2 } from "react";
@@ -230,8 +549,19 @@ function Layer({ beforeId, ...layerSpec }) {
230
549
  const map = useMap();
231
550
  const ctxSourceId = useSourceId();
232
551
  const spec = ctxSourceId && !("source" in layerSpec && layerSpec.source) ? { ...layerSpec, source: ctxSourceId } : layerSpec;
233
- const prevRef = useRef({});
234
- useEffect3(() => {
552
+ if (spec.type !== "background" && !("source" in spec && spec.source)) {
553
+ throw new Error(
554
+ `<Layer id="${layerSpec.id}"> needs a source: put it inside <Source>, or pass the source prop.`
555
+ );
556
+ }
557
+ const specRef = useRef3(spec);
558
+ specRef.current = spec;
559
+ const beforeIdRef = useRef3(beforeId);
560
+ beforeIdRef.current = beforeId;
561
+ const insideSourceRef = useRef3(false);
562
+ insideSourceRef.current = Boolean(ctxSourceId);
563
+ const prevRef = useRef3({});
564
+ useEffect10(() => {
235
565
  map.addLayer(spec, beforeId);
236
566
  prevRef.current = {
237
567
  paint: layerSpec.paint ?? void 0,
@@ -250,7 +580,37 @@ function Layer({ beforeId, ...layerSpec }) {
250
580
  }
251
581
  };
252
582
  }, [map, layerSpec.id]);
253
- useEffect3(() => {
583
+ useEffect10(() => {
584
+ const id = layerSpec.id;
585
+ function restore() {
586
+ if (insideSourceRef.current || !map.isStyleLoaded()) return;
587
+ const current = specRef.current;
588
+ try {
589
+ if (map.getLayer(id)) return;
590
+ if (current.source && !map.getSource(current.source)) return;
591
+ const before = beforeIdRef.current;
592
+ map.addLayer(
593
+ current,
594
+ before && map.getLayer(before) ? before : void 0
595
+ );
596
+ prevRef.current = {
597
+ paint: current.paint ?? void 0,
598
+ layout: current.layout ?? void 0,
599
+ filter: current.filter,
600
+ minzoom: current.minzoom,
601
+ maxzoom: current.maxzoom,
602
+ beforeId: beforeIdRef.current
603
+ };
604
+ } catch {
605
+ }
606
+ }
607
+ const events = ["styledata", "style.load", "idle"];
608
+ for (const event of events) map.on(event, restore);
609
+ return () => {
610
+ for (const event of events) map.off(event, restore);
611
+ };
612
+ }, [map, layerSpec.id]);
613
+ useEffect10(() => {
254
614
  try {
255
615
  if (!map.getLayer(layerSpec.id)) return;
256
616
  const prev = prevRef.current;
@@ -316,12 +676,12 @@ import { MapMouseEvent } from "maplibre-gl";
316
676
  import {
317
677
  forwardRef,
318
678
  useContext as useContext3,
319
- useEffect as useEffect4,
679
+ useEffect as useEffect11,
320
680
  useImperativeHandle,
321
- useRef as useRef2,
322
- useState as useState2
681
+ useRef as useRef4,
682
+ useState as useState3
323
683
  } from "react";
324
- import { jsx } from "react/jsx-runtime";
684
+ import { jsx as jsx2 } from "react/jsx-runtime";
325
685
  var DEFAULT_CENTER = [0, 0];
326
686
  var DEFAULT_ZOOM = 0;
327
687
  var HANDLER_NAMES = {
@@ -392,23 +752,23 @@ var Map = forwardRef((props, ref) => {
392
752
  children,
393
753
  ...rest
394
754
  } = props;
395
- const containerRef = useRef2(null);
396
- const [map, setMap] = useState2(null);
755
+ const containerRef = useRef4(null);
756
+ const [map, setMap] = useState3(null);
397
757
  const registry = useContext3(MapProviderContext);
398
- const registryRef = useRef2(registry);
758
+ const registryRef = useRef4(registry);
399
759
  registryRef.current = registry;
400
- const handlersRef = useRef2({});
760
+ const handlersRef = useRef4({});
401
761
  for (const key of Object.keys(HANDLER_NAMES)) {
402
762
  handlersRef.current[key] = props[key];
403
763
  }
404
- const interactiveLayerIdsRef = useRef2(
764
+ const interactiveLayerIdsRef = useRef4(
405
765
  interactiveLayerIds
406
766
  );
407
767
  interactiveLayerIdsRef.current = interactiveLayerIds;
408
768
  const layerIdsKey = interactiveLayerIds?.join("|") ?? "";
409
- const hoverStateRef = useRef2({ over: false });
769
+ const hoverStateRef = useRef4({ over: false });
410
770
  useImperativeHandle(ref, () => map, [map]);
411
- useEffect4(() => {
771
+ useEffect11(() => {
412
772
  if (!map || !id) return;
413
773
  const r = registryRef.current;
414
774
  if (!r) return;
@@ -417,7 +777,7 @@ var Map = forwardRef((props, ref) => {
417
777
  registryRef.current?.unregister(id);
418
778
  };
419
779
  }, [map, id]);
420
- useEffect4(() => {
780
+ useEffect11(() => {
421
781
  if (!containerRef.current) return;
422
782
  const mapOptions = { ...rest };
423
783
  for (const key of Object.keys(HANDLER_NAMES)) {
@@ -426,8 +786,9 @@ var Map = forwardRef((props, ref) => {
426
786
  if (mapOptions.stage === void 0) {
427
787
  delete mapOptions.stage;
428
788
  }
789
+ const fitsToGeojson = Boolean(mapOptions.geojson) && !mapOptions.center;
429
790
  const instance = new GeoloniaMap({
430
- center: DEFAULT_CENTER,
791
+ ...fitsToGeojson ? {} : { center: DEFAULT_CENTER },
431
792
  zoom: DEFAULT_ZOOM,
432
793
  stage: DEFAULT_STAGE,
433
794
  ...mapOptions,
@@ -505,12 +866,12 @@ var Map = forwardRef((props, ref) => {
505
866
  setMap(null);
506
867
  };
507
868
  }, []);
508
- useEffect4(() => {
869
+ useEffect11(() => {
509
870
  if (!hoverStateRef.current.over) return;
510
871
  hoverStateRef.current.over = false;
511
872
  handlersRef.current.onMouseLeave?.({ features: [] });
512
873
  }, [layerIdsKey]);
513
- useEffect4(() => {
874
+ useEffect11(() => {
514
875
  if (!map || cursor === void 0) return;
515
876
  const canvas = map.getCanvas();
516
877
  const prev = canvas.style.cursor;
@@ -530,8 +891,8 @@ var Map = forwardRef((props, ref) => {
530
891
  const minPitch = rest.minPitch;
531
892
  const maxPitch = rest.maxPitch;
532
893
  const style = rest.style;
533
- const prevStyleRef = useRef2(style);
534
- useEffect4(() => {
894
+ const prevStyleRef = useRef4(style);
895
+ useEffect11(() => {
535
896
  if (!map) return;
536
897
  const m = map;
537
898
  const opts = {};
@@ -548,46 +909,46 @@ var Map = forwardRef((props, ref) => {
548
909
  if (transitionType === "jump") m.jumpTo(opts);
549
910
  else m.flyTo(opts);
550
911
  }, [map, lng, lat, zoom, bearing, pitch, transitionType]);
551
- useEffect4(() => {
912
+ useEffect11(() => {
552
913
  if (!map) return;
553
914
  map.setMinZoom(
554
915
  minZoom ?? null
555
916
  );
556
917
  }, [map, minZoom]);
557
- useEffect4(() => {
918
+ useEffect11(() => {
558
919
  if (!map) return;
559
920
  map.setMaxZoom(
560
921
  maxZoom ?? null
561
922
  );
562
923
  }, [map, maxZoom]);
563
- useEffect4(() => {
924
+ useEffect11(() => {
564
925
  if (!map) return;
565
926
  map.setMinPitch(
566
927
  minPitch ?? null
567
928
  );
568
929
  }, [map, minPitch]);
569
- useEffect4(() => {
930
+ useEffect11(() => {
570
931
  if (!map) return;
571
932
  map.setMaxPitch(
572
933
  maxPitch ?? null
573
934
  );
574
935
  }, [map, maxPitch]);
575
- useEffect4(() => {
936
+ useEffect11(() => {
576
937
  if (!map) return;
577
938
  if (!style) return;
578
939
  if (prevStyleRef.current === style) return;
579
940
  prevStyleRef.current = style;
580
941
  map.setStyle(style);
581
942
  }, [map, style]);
582
- return /* @__PURE__ */ jsx("div", { ref: containerRef, className, style: containerStyle, children: map && /* @__PURE__ */ jsx(MapContext.Provider, { value: map, children }) });
943
+ return /* @__PURE__ */ jsx2("div", { ref: containerRef, className, style: containerStyle, children: map && /* @__PURE__ */ jsx2(MapContext.Provider, { value: map, children }) });
583
944
  });
584
945
  Map.displayName = "Map";
585
946
 
586
947
  // src/components/map-provider.tsx
587
- import { useCallback, useMemo, useState as useState3 } from "react";
588
- import { jsx as jsx2 } from "react/jsx-runtime";
948
+ import { useCallback, useMemo, useState as useState4 } from "react";
949
+ import { jsx as jsx3 } from "react/jsx-runtime";
589
950
  function MapProvider({ children }) {
590
- const [maps, setMaps] = useState3({});
951
+ const [maps, setMaps] = useState4({});
591
952
  const register = useCallback((id, map) => {
592
953
  setMaps((prev) => {
593
954
  if (prev[id] === map) return prev;
@@ -606,120 +967,12 @@ function MapProvider({ children }) {
606
967
  () => ({ maps, register, unregister }),
607
968
  [maps, register, unregister]
608
969
  );
609
- return /* @__PURE__ */ jsx2(MapProviderContext.Provider, { value, children });
610
- }
611
-
612
- // src/components/marker.tsx
613
- import { GeoloniaMarker } from "@geolonia/maps-core";
614
- import * as maplibregl2 from "maplibre-gl";
615
- import { useEffect as useEffect5, useRef as useRef3 } from "react";
616
- import { createPortal as createPortal2 } from "react-dom";
617
- function Marker(props) {
618
- const {
619
- lat,
620
- lng,
621
- color,
622
- openPopup,
623
- draggable,
624
- rotation,
625
- offset,
626
- anchor,
627
- rotationAlignment,
628
- pitchAlignment,
629
- className,
630
- children
631
- } = props;
632
- const map = useMap();
633
- const markerRef = useRef3(null);
634
- const popupEl = useRef3(document.createElement("div"));
635
- const handlersRef = useRef3({});
636
- handlersRef.current = {
637
- onClick: props.onClick,
638
- onDragStart: props.onDragStart,
639
- onDrag: props.onDrag,
640
- onDragEnd: props.onDragEnd
641
- };
642
- useEffect5(() => {
643
- const marker = new GeoloniaMarker({
644
- color,
645
- draggable,
646
- rotation,
647
- offset,
648
- anchor,
649
- rotationAlignment,
650
- pitchAlignment,
651
- className
652
- }).setLngLat([lng, lat]).addTo(map);
653
- if (children) {
654
- marker.setPopup(
655
- new maplibregl2.Popup({ offset: [0, -30] }).setDOMContent(
656
- popupEl.current
657
- )
658
- );
659
- if (openPopup) marker.togglePopup();
660
- }
661
- const onClickWrapper = (e) => {
662
- handlersRef.current.onClick?.(e);
663
- };
664
- const onDragStartWrapper = (e) => {
665
- handlersRef.current.onDragStart?.(e);
666
- };
667
- const onDragWrapper = (e) => {
668
- handlersRef.current.onDrag?.(e);
669
- };
670
- const onDragEndWrapper = (e) => {
671
- handlersRef.current.onDragEnd?.(e);
672
- };
673
- const element = marker.getElement();
674
- element.addEventListener("click", onClickWrapper);
675
- marker.on("dragstart", onDragStartWrapper);
676
- marker.on("drag", onDragWrapper);
677
- marker.on("dragend", onDragEndWrapper);
678
- markerRef.current = marker;
679
- return () => {
680
- element.removeEventListener("click", onClickWrapper);
681
- marker.off("dragstart", onDragStartWrapper);
682
- marker.off("drag", onDragWrapper);
683
- marker.off("dragend", onDragEndWrapper);
684
- marker.remove();
685
- };
686
- }, [map, color, anchor, className]);
687
- useEffect5(() => {
688
- markerRef.current?.setLngLat([lng, lat]);
689
- }, [lat, lng]);
690
- useEffect5(() => {
691
- if (draggable === void 0) return;
692
- markerRef.current?.setDraggable(draggable);
693
- }, [draggable]);
694
- useEffect5(() => {
695
- if (rotation === void 0) return;
696
- markerRef.current?.setRotation(rotation);
697
- }, [rotation]);
698
- useEffect5(() => {
699
- if (offset === void 0) return;
700
- markerRef.current?.setOffset(offset);
701
- }, [offset]);
702
- useEffect5(() => {
703
- if (rotationAlignment === void 0) return;
704
- markerRef.current?.setRotationAlignment(rotationAlignment);
705
- }, [rotationAlignment]);
706
- useEffect5(() => {
707
- if (pitchAlignment === void 0) return;
708
- markerRef.current?.setPitchAlignment(pitchAlignment);
709
- }, [pitchAlignment]);
710
- useEffect5(() => {
711
- const marker = markerRef.current;
712
- const popup = marker?.getPopup();
713
- if (!popup) return;
714
- const isOpen = popup.isOpen();
715
- if (!!openPopup !== isOpen) marker?.togglePopup();
716
- }, [openPopup]);
717
- return children ? createPortal2(children, popupEl.current) : null;
970
+ return /* @__PURE__ */ jsx3(MapProviderContext.Provider, { value, children });
718
971
  }
719
972
 
720
973
  // src/components/popup.tsx
721
- import * as maplibregl3 from "maplibre-gl";
722
- import { useEffect as useEffect6, useRef as useRef4 } from "react";
974
+ import * as maplibregl8 from "maplibre-gl";
975
+ import { useEffect as useEffect12, useRef as useRef5 } from "react";
723
976
  import { createPortal as createPortal3 } from "react-dom";
724
977
  function defined2(obj) {
725
978
  const out = {};
@@ -743,17 +996,17 @@ function Popup3(props) {
743
996
  onClose
744
997
  } = props;
745
998
  const map = useMap();
746
- const containerEl = useRef4(document.createElement("div"));
747
- const popupRef = useRef4(null);
748
- const onCloseRef = useRef4(onClose);
999
+ const containerEl = useRef5(document.createElement("div"));
1000
+ const popupRef = useRef5(null);
1001
+ const onCloseRef = useRef5(onClose);
749
1002
  onCloseRef.current = onClose;
750
- useEffect6(() => {
1003
+ useEffect12(() => {
751
1004
  let mapRemoving = false;
752
1005
  const handleMapRemove = () => {
753
1006
  mapRemoving = true;
754
1007
  };
755
1008
  map.on("remove", handleMapRemove);
756
- const popup = new maplibregl3.Popup(
1009
+ const popup = new maplibregl8.Popup(
757
1010
  defined2({
758
1011
  anchor,
759
1012
  offset,
@@ -775,17 +1028,17 @@ function Popup3(props) {
775
1028
  if (popup.isOpen()) popup.remove();
776
1029
  };
777
1030
  }, [map, anchor, closeButton, closeOnClick, closeOnMove]);
778
- useEffect6(() => {
1031
+ useEffect12(() => {
779
1032
  popupRef.current?.setLngLat([lng, lat]);
780
1033
  }, [lat, lng]);
781
- useEffect6(() => {
1034
+ useEffect12(() => {
782
1035
  popupRef.current?.setOffset(offset);
783
1036
  }, [offset]);
784
- useEffect6(() => {
1037
+ useEffect12(() => {
785
1038
  popupRef.current?.setMaxWidth(maxWidth ?? "240px");
786
1039
  }, [maxWidth]);
787
- const prevClassNameRef = useRef4(className);
788
- useEffect6(() => {
1040
+ const prevClassNameRef = useRef5(className);
1041
+ useEffect12(() => {
789
1042
  const popup = popupRef.current;
790
1043
  if (!popup) return;
791
1044
  const prev = prevClassNameRef.current;
@@ -797,9 +1050,88 @@ function Popup3(props) {
797
1050
  return createPortal3(children, containerEl.current);
798
1051
  }
799
1052
 
1053
+ // src/components/simple-style.tsx
1054
+ import {
1055
+ SimpleStyle as CoreSimpleStyle
1056
+ } from "@geolonia/maps-core";
1057
+ import { useEffect as useEffect13, useRef as useRef6 } from "react";
1058
+ var DEFAULT_ID = "geolonia-simple-style";
1059
+ function SimpleStyle({
1060
+ geojson,
1061
+ id,
1062
+ cluster,
1063
+ clusterColor,
1064
+ choropleth,
1065
+ heatmap,
1066
+ circle,
1067
+ fitBounds
1068
+ }) {
1069
+ const map = useMap();
1070
+ const geojsonRef = useRef6(geojson);
1071
+ geojsonRef.current = geojson;
1072
+ const fitBoundsRef = useRef6(fitBounds);
1073
+ fitBoundsRef.current = fitBounds;
1074
+ const instanceRef = useRef6(null);
1075
+ const appliedRef = useRef6(geojson);
1076
+ const optionsKey = JSON.stringify([
1077
+ id,
1078
+ cluster,
1079
+ clusterColor,
1080
+ choropleth,
1081
+ heatmap,
1082
+ circle
1083
+ ]);
1084
+ useEffect13(() => {
1085
+ const options = {};
1086
+ const given = { id, cluster, clusterColor, choropleth, heatmap, circle };
1087
+ for (const [key, value] of Object.entries(given)) {
1088
+ if (value !== void 0) options[key] = value;
1089
+ }
1090
+ let cancelled = false;
1091
+ let instance = null;
1092
+ function build() {
1093
+ instance = new CoreSimpleStyle(geojsonRef.current, options);
1094
+ appliedRef.current = geojsonRef.current;
1095
+ instance.addTo(map);
1096
+ instanceRef.current = instance;
1097
+ if (fitBoundsRef.current) instance.fitBounds();
1098
+ }
1099
+ function sync() {
1100
+ if (cancelled || !map.isStyleLoaded()) return;
1101
+ if (!instance) {
1102
+ build();
1103
+ } else if (!map.getSource(options.id ?? DEFAULT_ID)) {
1104
+ instance.remove();
1105
+ build();
1106
+ }
1107
+ }
1108
+ const events = ["styledata", "style.load", "idle"];
1109
+ for (const event of events) map.on(event, sync);
1110
+ sync();
1111
+ return () => {
1112
+ cancelled = true;
1113
+ for (const event of events) map.off(event, sync);
1114
+ instanceRef.current = null;
1115
+ try {
1116
+ instance?.remove();
1117
+ } catch {
1118
+ }
1119
+ };
1120
+ }, [map, optionsKey]);
1121
+ useEffect13(() => {
1122
+ const instance = instanceRef.current;
1123
+ if (!instance || appliedRef.current === geojson) return;
1124
+ if (!map.getSource(id ?? DEFAULT_ID)) return;
1125
+ appliedRef.current = geojson;
1126
+ if (typeof geojson === "string") instance.setGeoJSON(geojson);
1127
+ else instance.updateData(geojson);
1128
+ }, [map, id, geojson]);
1129
+ return null;
1130
+ }
1131
+
800
1132
  // src/components/source.tsx
801
- import { useEffect as useEffect7, useRef as useRef5, useState as useState4 } from "react";
802
- import { jsx as jsx3 } from "react/jsx-runtime";
1133
+ import { Fragment as Fragment2, useEffect as useEffect14, useRef as useRef7, useState as useState5 } from "react";
1134
+ import { jsx as jsx4 } from "react/jsx-runtime";
803
1135
  var SUPPORTED_UPDATE_PROPS = {
804
1136
  geojson: ["data"],
805
1137
  image: ["url", "coordinates"],
@@ -812,16 +1144,40 @@ var SUPPORTED_UPDATE_PROPS = {
812
1144
  };
813
1145
  function Source({ id, children, ...spec }) {
814
1146
  const map = useMap();
815
- const [ready, setReady] = useState4(false);
816
- const prevSpecRef = useRef5(null);
817
- useEffect7(() => {
1147
+ const [ready, setReady] = useState5(false);
1148
+ const [generation, setGeneration] = useState5(0);
1149
+ const prevSpecRef = useRef7(null);
1150
+ const specRef = useRef7(spec);
1151
+ specRef.current = spec;
1152
+ useEffect14(() => {
818
1153
  let cancelled = false;
1154
+ let added = false;
1155
+ let ownSource;
819
1156
  function add() {
820
1157
  if (cancelled || map.getSource(id)) return;
821
1158
  map.addSource(id, spec);
1159
+ added = true;
1160
+ ownSource = map.getSource(id);
822
1161
  prevSpecRef.current = { ...spec };
823
1162
  if (!cancelled) setReady(true);
824
1163
  }
1164
+ function restore() {
1165
+ if (cancelled || !added || !map.isStyleLoaded()) return;
1166
+ const current = map.getSource(id);
1167
+ if (current && current === ownSource) return;
1168
+ if (!current) {
1169
+ try {
1170
+ map.addSource(id, specRef.current);
1171
+ } catch {
1172
+ return;
1173
+ }
1174
+ }
1175
+ ownSource = map.getSource(id);
1176
+ prevSpecRef.current = { ...specRef.current };
1177
+ setGeneration((g) => g + 1);
1178
+ }
1179
+ const restoreEvents = ["styledata", "style.load", "idle"];
1180
+ for (const event of restoreEvents) map.on(event, restore);
825
1181
  if (map.isStyleLoaded()) {
826
1182
  add();
827
1183
  } else {
@@ -830,6 +1186,7 @@ function Source({ id, children, ...spec }) {
830
1186
  return () => {
831
1187
  cancelled = true;
832
1188
  map.off("styledata", add);
1189
+ for (const event of restoreEvents) map.off(event, restore);
833
1190
  setReady(false);
834
1191
  try {
835
1192
  if (map.getSource(id)) {
@@ -842,7 +1199,7 @@ function Source({ id, children, ...spec }) {
842
1199
  }
843
1200
  };
844
1201
  }, [map, id]);
845
- useEffect7(() => {
1202
+ useEffect14(() => {
846
1203
  if (!ready) return;
847
1204
  const source = map.getSource(id);
848
1205
  if (!source) return;
@@ -915,13 +1272,16 @@ function Source({ id, children, ...spec }) {
915
1272
  }
916
1273
  prevSpecRef.current = { ...current };
917
1274
  });
918
- return /* @__PURE__ */ jsx3(SourceContext.Provider, { value: id, children: ready && children });
1275
+ return /* @__PURE__ */ jsx4(SourceContext.Provider, { value: id, children: /* @__PURE__ */ jsx4(Fragment2, { children: ready && children }, generation) });
919
1276
  }
920
1277
  export {
921
1278
  AttributionControl2 as AttributionControl,
922
1279
  Control,
923
1280
  FullscreenControl2 as FullscreenControl,
1281
+ GeocodeError,
1282
+ GeocoderControl,
924
1283
  GeolocateControl2 as GeolocateControl,
1284
+ GlobeControl2 as GlobeControl,
925
1285
  Layer,
926
1286
  Map,
927
1287
  MapContext,
@@ -931,8 +1291,10 @@ export {
931
1291
  NavigationControl2 as NavigationControl,
932
1292
  Popup3 as Popup,
933
1293
  ScaleControl2 as ScaleControl,
1294
+ SimpleStyle,
934
1295
  Source,
935
1296
  SourceContext,
1297
+ geocode,
936
1298
  useControl,
937
1299
  useMap,
938
1300
  useMaps,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/maps-react",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,7 +14,8 @@
14
14
  "dist"
15
15
  ],
16
16
  "dependencies": {
17
- "@geolonia/maps-core": "^0.8.0"
17
+ "@geolonia/maps-core": "^0.8.0",
18
+ "@geolonia/normalize-japanese-addresses": "^3.1.3"
18
19
  },
19
20
  "peerDependencies": {
20
21
  "maplibre-gl": "^6.0.0",
@@ -22,20 +23,20 @@
22
23
  "react-dom": ">=18"
23
24
  },
24
25
  "devDependencies": {
25
- "@biomejs/biome": "^2.5.15",
26
- "@playwright/test": "^1.63.0",
26
+ "@biomejs/biome": "^2.6.6",
27
+ "@playwright/test": "^1.65.0",
27
28
  "@testing-library/react": "^16.3.3",
28
29
  "@types/react": "^18",
29
30
  "@types/react-dom": "^18",
30
- "@vitejs/plugin-react": "^6.1.2",
31
- "@vitest/coverage-v8": "^5.0.5",
32
- "jsdom": "^30.1.3",
33
- "maplibre-gl": "6.11.2",
31
+ "@vitejs/plugin-react": "^6.2.1",
32
+ "@vitest/coverage-v8": "^5.1.0",
33
+ "jsdom": "^31.0.0",
34
+ "maplibre-gl": "6.16.0",
34
35
  "react": "^18",
35
36
  "react-dom": "^18",
36
- "tsup": "^8",
37
+ "tsup": "^9",
37
38
  "typescript": "^5.8",
38
- "vite": "^8.3.4",
39
+ "vite": "^8.3.6",
39
40
  "vitest": "^5.0.2"
40
41
  },
41
42
  "scripts": {