@geolonia/maps-react 0.4.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,6 +71,12 @@ 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. 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
+ | `cluster` | `boolean` | Cluster the point features of `geojson` (default `true`). Ignored when `heatmap` or `circle` is set: those never cluster |
76
+ | `clusterColor` | `string` | Color of the cluster markers |
77
+ | `choropleth` | `string \| ChoroplethOptions` | Color the polygons of `geojson` by a property value. A property name, or `{ property, colors?, range? }` |
78
+ | `heatmap` | `boolean \| string \| HeatmapOptions` | Draw the points of `geojson` as a heatmap. `true` for equal weight, a property name for its weight, or `{ weight?, colors? }` |
79
+ | `circle` | `CircleOptions` | Size and color the points of `geojson` by property values: `{ size?, color?, colors? }` |
74
80
  | `3d` | `boolean` | Enable 3D mode |
75
81
  | `className` | `string` | CSS class for the container |
76
82
  | `containerStyle` | `CSSProperties` | Inline styles for the container |
@@ -80,6 +86,14 @@ Root component. Creates a Geolonia map instance and provides it to children via
80
86
 
81
87
  `ControlPosition` = `'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'`
82
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 change them on a live map, use `<SimpleStyle>` below.
90
+
91
+ ```tsx
92
+ <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12} geojson={meshes} choropleth="population" />
93
+ <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12} geojson={spots} heatmap="weight" />
94
+ <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12} geojson={spots} circle={{ size: 'weight', color: 'category' }} />
95
+ ```
96
+
83
97
  ### `<Marker>`
84
98
 
85
99
  Displays a marker at the given coordinates. Children are rendered as a popup.
@@ -112,9 +126,32 @@ Displays a standalone popup at the given coordinates.
112
126
  |------|------|-------------|
113
127
  | `lat` | `number` | Latitude (required) |
114
128
  | `lng` | `number` | Longitude (required) |
115
- | `onClose` | `() => void` | Callback when popup is closed |
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 |
116
130
  | `children` | `ReactNode` | Popup content |
117
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
+
118
155
  ### `<Source>` / `<Layer>`
119
156
 
120
157
  Declaratively add MapLibre sources and layers. Nesting `<Layer>` inside `<Source>` automatically inherits the source ID.
@@ -129,7 +166,69 @@ Declaratively add MapLibre sources and layers. Nesting `<Layer>` inside `<Source
129
166
  </Source>
130
167
  ```
131
168
 
132
- `<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.
133
232
 
134
233
  ### `useMap()`
135
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 };