@geolonia/maps-react 0.6.0 → 0.7.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
@@ -77,6 +77,11 @@ Root component. Creates a Geolonia map instance and provides it to children via
77
77
  | `choropleth` | `string \| ChoroplethOptions` | Color the polygons of `geojson` by a property value. A property name, or `{ property, colors?, range? }` |
78
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
79
  | `circle` | `CircleOptions` | Size and color the points of `geojson` by property values: `{ size?, color?, colors? }` |
80
+ | `extrusion` | `string \| ExtrusionOptions` | Draw the polygons of `geojson` as columns whose height follows a property. A property name sets height and color; `{ height, color? }` sets them apart. Set `pitch` too, or it looks flat |
81
+ | `projection` | `'mercator' \| 'globe'` | Map projection. Changing it after the map exists switches it with `setProjection`, and it is applied again after each `setStyle` (a projection changed with `<GlobeControl>` goes back to the prop then). Removing the prop stops it being applied: the current projection stays until the next `setStyle`, after which maps-core's stored value or the style's own projection applies |
82
+ | `rasterTiles` | `RasterTilesOptions` | Lay another raster tile source under the labels: `{ url, attribution?, opacity? }`. `url` contains `{z}`, `{x}` and `{y}`. Read once when the map is created |
83
+ | `mask` | `string \| MaskOptions` | Darken everything outside a boundary (GeoJSON URL, or `{ geojson, color?, opacity?, outline?, beforeId? }`). Read once when the map is created. To change it on a live map, use `<Mask>` below |
84
+ | `transformRequest` | `RequestTransformFunction` | Rewrite a request before it is sent, as in MapLibre: `(url, resourceType?) => RequestParameters \| Promise<RequestParameters> \| undefined`. Called for Geolonia sources and sprites too (maps-core 0.7.0+), with the URL after maps-core's own changes (API key, session ID, stage host). What you return replaces maps-core's result, so keep those query parameters when you rewrite the URL. Read once when the map is created |
80
85
  | `3d` | `boolean` | Enable 3D mode |
81
86
  | `className` | `string` | CSS class for the container |
82
87
  | `containerStyle` | `CSSProperties` | Inline styles for the container |
@@ -86,7 +91,7 @@ Root component. Creates a Geolonia map instance and provides it to children via
86
91
 
87
92
  `ControlPosition` = `'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'`
88
93
 
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.
94
+ `geojson`, `cluster`, `clusterColor`, `choropleth`, `heatmap`, `circle` and `extrusion` 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
95
 
91
96
  ```tsx
92
97
  <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={12} geojson={meshes} choropleth="population" />
@@ -182,14 +187,34 @@ Draws GeoJSON on the map the way `<Map geojson>` does, but as a child of `<Map>`
182
187
  |---|---|---|
183
188
  | `geojson` | `string \| FeatureCollection` | GeoJSON, or its URL. Changing it updates the data on the live map (the view is not moved) |
184
189
  | `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 |
190
+ | `cluster`, `clusterColor`, `choropleth`, `heatmap`, `circle`, `extrusion` | 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
191
  | `fitBounds` | `boolean` | Fit the view to the data when the layers are added (also when rebuilt). Default `false` |
187
192
 
188
193
  The layers are removed when the component unmounts, and added again after the map's `style` changes.
189
194
 
195
+ `choropleth`, `circle` and `extrusion` colors also take a `ColorMap` (`{ [value]: color }`) in place of a color array, for one color per value. The option types (`ExtrusionOptions`, `ColorMap`, `MaskOptions`, `RasterTilesOptions` and so on) are exported.
196
+
197
+ ### `<Mask>`
198
+
199
+ Darkens everything outside a boundary, such as a city, on a live map. maps-core fixes the source and layer IDs, so a map holds one `<Mask>`.
200
+
201
+ ```tsx
202
+ <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={11}>
203
+ <Mask geojson="/boundary.geojson" outline fitBounds />
204
+ </Map>
205
+ ```
206
+
207
+ | Prop | Type | Description |
208
+ |---|---|---|
209
+ | `geojson` | `string \| Feature \| FeatureCollection \| Polygon \| MultiPolygon \| GeometryCollection` | The boundary, or the URL of one |
210
+ | `color`, `opacity`, `outline`, `beforeId` | same as `MaskOptions` | Changing one rebuilds the mask. Object values are compared by content, so a new literal does not rebuild |
211
+ | `fitBounds` | `boolean` | Fit the view to the boundary when the mask is added (also when rebuilt). Default `false` |
212
+
213
+ The mask is removed when the component unmounts, and added again after the map's `style` changes.
214
+
190
215
  ### `<GeocoderControl>`
191
216
 
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.
217
+ 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: a search fetches what it needs from the address data API, and the package keeps what it fetched in memory, so repeated searches in the same city (or the same town, when the address includes a street number) may not hit the network.
193
218
 
194
219
  ```tsx
195
220
  <Map apiKey="YOUR-API-KEY" center={[139.76, 35.68]} zoom={10}>
package/dist/index.d.ts CHANGED
@@ -1,8 +1,9 @@
1
+ import { GeoloniaMapOptions, GeoloniaMap, MaskOptions } from '@geolonia/maps-core';
2
+ export { ChoroplethOptions, CircleOptions, ColorMap, ExtrusionColorOptions, ExtrusionHeightOptions, ExtrusionOptions, HeatmapOptions, MaskOptions, RasterTilesOptions } from '@geolonia/maps-core';
1
3
  import * as react from 'react';
2
4
  import { ReactNode, CSSProperties } from 'react';
3
5
  import { Map as Map$1, AttributionControlOptions, FullscreenControlOptions, GeolocateControlOptions, NavigationControlOptions, ScaleControlOptions, IControl, LayerSpecification, PointLike, PositionAnchor, Alignment, SourceSpecification, CanvasSourceSpecification } from 'maplibre-gl';
4
6
  import * as react_jsx_runtime from 'react/jsx-runtime';
5
- import { GeoloniaMapOptions, GeoloniaMap } from '@geolonia/maps-core';
6
7
 
7
8
  type ControlPosition = "top-left" | "top-right" | "bottom-left" | "bottom-right";
8
9
  type ControlProps = {
@@ -58,7 +59,9 @@ type GeocodeResult = {
58
59
  * 日本の住所を検索して、位置と表示に適した範囲を返します。
59
60
  *
60
61
  * 住所の解釈には `@geolonia/normalize-japanese-addresses` を使います。住所データは
61
- * 検索のたびに、住所データ API から必要な分だけ取得します。
62
+ * 持たず、検索したときに、必要な分を住所データ API から取得します。取得した分は
63
+ * NJA がメモリに保持するので、同じ市区町村(番地まで含めるときは同じ町丁目)を続けて
64
+ * 検索すると、通信しないことがあります。
62
65
  *
63
66
  * 失敗すると、`code` を持つ {@link GeocodeError} で reject します。
64
67
  */
@@ -206,6 +209,16 @@ type MarkerProps = {
206
209
  };
207
210
  declare function Marker(props: MarkerProps): react.ReactPortal | null;
208
211
 
212
+ type MaskProps = MaskOptions & {
213
+ /** Fit the view to the boundary when the mask is added. Default `false`. */
214
+ fitBounds?: boolean;
215
+ };
216
+ /**
217
+ * Darkens everything outside a boundary (a city, for example). A map holds one
218
+ * `<Mask>`: maps-core fixes the source and layer IDs.
219
+ */
220
+ declare function Mask({ fitBounds, ...options }: MaskProps): null;
221
+
209
222
  type PopupProps = {
210
223
  lat: number;
211
224
  lng: number;
@@ -221,7 +234,7 @@ type PopupProps = {
221
234
  };
222
235
  declare function Popup(props: PopupProps): react.ReactPortal;
223
236
 
224
- type SimpleStyleOptions = Pick<GeoloniaMapOptions, "cluster" | "clusterColor" | "choropleth" | "heatmap" | "circle">;
237
+ type SimpleStyleOptions = Pick<GeoloniaMapOptions, "cluster" | "clusterColor" | "choropleth" | "heatmap" | "circle" | "extrusion">;
225
238
  type SimpleStyleProps = SimpleStyleOptions & {
226
239
  /** GeoJSON to draw, or the URL of one. Changing it updates the data on the live map. */
227
240
  geojson: NonNullable<GeoloniaMapOptions["geojson"]>;
@@ -233,7 +246,7 @@ type SimpleStyleProps = SimpleStyleOptions & {
233
246
  /** Fit the view to the data when it is added to the map. Default `false`. */
234
247
  fitBounds?: boolean;
235
248
  };
236
- declare function SimpleStyle({ geojson, id, cluster, clusterColor, choropleth, heatmap, circle, fitBounds, }: SimpleStyleProps): null;
249
+ declare function SimpleStyle({ geojson, id, cluster, clusterColor, choropleth, heatmap, circle, extrusion, fitBounds, }: SimpleStyleProps): null;
237
250
 
238
251
  type SourceProps = (SourceSpecification | CanvasSourceSpecification) & {
239
252
  id: string;
@@ -258,5 +271,5 @@ declare function useMaps(): MapCollection;
258
271
  declare const SourceContext: react.Context<string | null>;
259
272
  declare function useSourceId(): string | null;
260
273
 
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 };
274
+ export { AttributionControl, Control, FullscreenControl, GeocodeError, GeocoderControl, GeolocateControl, GlobeControl, Layer, Map, MapContext, MapProvider, MapProviderContext, Marker, Mask, NavigationControl, Popup, ScaleControl, SimpleStyle, Source, SourceContext, geocode, useControl, useMap, useMaps, useSourceId };
275
+ export type { AttributionControlProps, ControlPosition, ControlProps, FullscreenControlProps, GeocodeErrorCode, GeocodeResult, GeocoderControlProps, GeolocateControlProps, GlobeControlProps, LayerProps, MapCollection, MapEventProps, MapProps, MapProviderProps, MapRegistry, MarkerProps, MaskProps, NavigationControlProps, PopupProps, ScaleControlProps, SimpleStyleProps, SourceProps };
package/dist/index.js CHANGED
@@ -891,6 +891,7 @@ var Map = forwardRef((props, ref) => {
891
891
  const minPitch = rest.minPitch;
892
892
  const maxPitch = rest.maxPitch;
893
893
  const style = rest.style;
894
+ const projection = rest.projection;
894
895
  const prevStyleRef = useRef4(style);
895
896
  useEffect11(() => {
896
897
  if (!map) return;
@@ -933,6 +934,27 @@ var Map = forwardRef((props, ref) => {
933
934
  maxPitch ?? null
934
935
  );
935
936
  }, [map, maxPitch]);
937
+ useEffect11(() => {
938
+ if (!map || !projection) return;
939
+ const m = map;
940
+ let pending = true;
941
+ const apply = () => {
942
+ pending = false;
943
+ if (m.getProjection()?.type !== projection) {
944
+ m.setProjection({ type: projection });
945
+ }
946
+ };
947
+ const onIdle = () => {
948
+ if (pending && m.isStyleLoaded()) apply();
949
+ };
950
+ if (m.isStyleLoaded()) apply();
951
+ m.on("style.load", apply);
952
+ m.on("idle", onIdle);
953
+ return () => {
954
+ m.off("style.load", apply);
955
+ m.off("idle", onIdle);
956
+ };
957
+ }, [map, projection]);
936
958
  useEffect11(() => {
937
959
  if (!map) return;
938
960
  if (!style) return;
@@ -970,9 +992,61 @@ function MapProvider({ children }) {
970
992
  return /* @__PURE__ */ jsx3(MapProviderContext.Provider, { value, children });
971
993
  }
972
994
 
995
+ // src/components/mask.tsx
996
+ import { Mask as CoreMask } from "@geolonia/maps-core";
997
+ import { useEffect as useEffect12, useRef as useRef5 } from "react";
998
+ var SOURCE_ID = "geolonia-mask";
999
+ function Mask({ fitBounds, ...options }) {
1000
+ const map = useMap();
1001
+ const fitBoundsRef = useRef5(fitBounds);
1002
+ useEffect12(() => {
1003
+ fitBoundsRef.current = fitBounds;
1004
+ });
1005
+ const optionsKey = JSON.stringify(options);
1006
+ useEffect12(() => {
1007
+ const given = {};
1008
+ for (const [key, value] of Object.entries(options)) {
1009
+ if (value !== void 0) given[key] = value;
1010
+ }
1011
+ let cancelled = false;
1012
+ let instance = null;
1013
+ let refused = false;
1014
+ function build() {
1015
+ instance = new CoreMask(given);
1016
+ instance.addTo(map);
1017
+ if (!map.getSource(SOURCE_ID)) {
1018
+ refused = true;
1019
+ return;
1020
+ }
1021
+ if (fitBoundsRef.current) instance.fitBounds();
1022
+ }
1023
+ function sync() {
1024
+ if (cancelled || refused || !map.isStyleLoaded()) return;
1025
+ if (!instance) {
1026
+ build();
1027
+ } else if (!map.getSource(SOURCE_ID) || !map.getLayer(SOURCE_ID)) {
1028
+ instance.remove();
1029
+ build();
1030
+ }
1031
+ }
1032
+ const events = ["styledata", "style.load", "idle"];
1033
+ for (const event of events) map.on(event, sync);
1034
+ sync();
1035
+ return () => {
1036
+ cancelled = true;
1037
+ for (const event of events) map.off(event, sync);
1038
+ try {
1039
+ instance?.remove();
1040
+ } catch {
1041
+ }
1042
+ };
1043
+ }, [map, optionsKey]);
1044
+ return null;
1045
+ }
1046
+
973
1047
  // src/components/popup.tsx
974
1048
  import * as maplibregl8 from "maplibre-gl";
975
- import { useEffect as useEffect12, useRef as useRef5 } from "react";
1049
+ import { useEffect as useEffect13, useRef as useRef6 } from "react";
976
1050
  import { createPortal as createPortal3 } from "react-dom";
977
1051
  function defined2(obj) {
978
1052
  const out = {};
@@ -996,11 +1070,11 @@ function Popup3(props) {
996
1070
  onClose
997
1071
  } = props;
998
1072
  const map = useMap();
999
- const containerEl = useRef5(document.createElement("div"));
1000
- const popupRef = useRef5(null);
1001
- const onCloseRef = useRef5(onClose);
1073
+ const containerEl = useRef6(document.createElement("div"));
1074
+ const popupRef = useRef6(null);
1075
+ const onCloseRef = useRef6(onClose);
1002
1076
  onCloseRef.current = onClose;
1003
- useEffect12(() => {
1077
+ useEffect13(() => {
1004
1078
  let mapRemoving = false;
1005
1079
  const handleMapRemove = () => {
1006
1080
  mapRemoving = true;
@@ -1028,17 +1102,17 @@ function Popup3(props) {
1028
1102
  if (popup.isOpen()) popup.remove();
1029
1103
  };
1030
1104
  }, [map, anchor, closeButton, closeOnClick, closeOnMove]);
1031
- useEffect12(() => {
1105
+ useEffect13(() => {
1032
1106
  popupRef.current?.setLngLat([lng, lat]);
1033
1107
  }, [lat, lng]);
1034
- useEffect12(() => {
1108
+ useEffect13(() => {
1035
1109
  popupRef.current?.setOffset(offset);
1036
1110
  }, [offset]);
1037
- useEffect12(() => {
1111
+ useEffect13(() => {
1038
1112
  popupRef.current?.setMaxWidth(maxWidth ?? "240px");
1039
1113
  }, [maxWidth]);
1040
- const prevClassNameRef = useRef5(className);
1041
- useEffect12(() => {
1114
+ const prevClassNameRef = useRef6(className);
1115
+ useEffect13(() => {
1042
1116
  const popup = popupRef.current;
1043
1117
  if (!popup) return;
1044
1118
  const prev = prevClassNameRef.current;
@@ -1054,7 +1128,7 @@ function Popup3(props) {
1054
1128
  import {
1055
1129
  SimpleStyle as CoreSimpleStyle
1056
1130
  } from "@geolonia/maps-core";
1057
- import { useEffect as useEffect13, useRef as useRef6 } from "react";
1131
+ import { useEffect as useEffect14, useRef as useRef7 } from "react";
1058
1132
  var DEFAULT_ID = "geolonia-simple-style";
1059
1133
  function SimpleStyle({
1060
1134
  geojson,
@@ -1064,26 +1138,36 @@ function SimpleStyle({
1064
1138
  choropleth,
1065
1139
  heatmap,
1066
1140
  circle,
1141
+ extrusion,
1067
1142
  fitBounds
1068
1143
  }) {
1069
1144
  const map = useMap();
1070
- const geojsonRef = useRef6(geojson);
1145
+ const geojsonRef = useRef7(geojson);
1071
1146
  geojsonRef.current = geojson;
1072
- const fitBoundsRef = useRef6(fitBounds);
1147
+ const fitBoundsRef = useRef7(fitBounds);
1073
1148
  fitBoundsRef.current = fitBounds;
1074
- const instanceRef = useRef6(null);
1075
- const appliedRef = useRef6(geojson);
1149
+ const instanceRef = useRef7(null);
1150
+ const appliedRef = useRef7(geojson);
1076
1151
  const optionsKey = JSON.stringify([
1077
1152
  id,
1078
1153
  cluster,
1079
1154
  clusterColor,
1080
1155
  choropleth,
1081
1156
  heatmap,
1082
- circle
1157
+ circle,
1158
+ extrusion
1083
1159
  ]);
1084
- useEffect13(() => {
1160
+ useEffect14(() => {
1085
1161
  const options = {};
1086
- const given = { id, cluster, clusterColor, choropleth, heatmap, circle };
1162
+ const given = {
1163
+ id,
1164
+ cluster,
1165
+ clusterColor,
1166
+ choropleth,
1167
+ heatmap,
1168
+ circle,
1169
+ extrusion
1170
+ };
1087
1171
  for (const [key, value] of Object.entries(given)) {
1088
1172
  if (value !== void 0) options[key] = value;
1089
1173
  }
@@ -1118,7 +1202,7 @@ function SimpleStyle({
1118
1202
  }
1119
1203
  };
1120
1204
  }, [map, optionsKey]);
1121
- useEffect13(() => {
1205
+ useEffect14(() => {
1122
1206
  const instance = instanceRef.current;
1123
1207
  if (!instance || appliedRef.current === geojson) return;
1124
1208
  if (!map.getSource(id ?? DEFAULT_ID)) return;
@@ -1130,7 +1214,7 @@ function SimpleStyle({
1130
1214
  }
1131
1215
 
1132
1216
  // src/components/source.tsx
1133
- import { Fragment as Fragment2, useEffect as useEffect14, useRef as useRef7, useState as useState5 } from "react";
1217
+ import { Fragment as Fragment2, useEffect as useEffect15, useRef as useRef8, useState as useState5 } from "react";
1134
1218
  import { jsx as jsx4 } from "react/jsx-runtime";
1135
1219
  var SUPPORTED_UPDATE_PROPS = {
1136
1220
  geojson: ["data"],
@@ -1146,10 +1230,10 @@ function Source({ id, children, ...spec }) {
1146
1230
  const map = useMap();
1147
1231
  const [ready, setReady] = useState5(false);
1148
1232
  const [generation, setGeneration] = useState5(0);
1149
- const prevSpecRef = useRef7(null);
1150
- const specRef = useRef7(spec);
1233
+ const prevSpecRef = useRef8(null);
1234
+ const specRef = useRef8(spec);
1151
1235
  specRef.current = spec;
1152
- useEffect14(() => {
1236
+ useEffect15(() => {
1153
1237
  let cancelled = false;
1154
1238
  let added = false;
1155
1239
  let ownSource;
@@ -1199,7 +1283,7 @@ function Source({ id, children, ...spec }) {
1199
1283
  }
1200
1284
  };
1201
1285
  }, [map, id]);
1202
- useEffect14(() => {
1286
+ useEffect15(() => {
1203
1287
  if (!ready) return;
1204
1288
  const source = map.getSource(id);
1205
1289
  if (!source) return;
@@ -1288,6 +1372,7 @@ export {
1288
1372
  MapProvider,
1289
1373
  MapProviderContext,
1290
1374
  Marker,
1375
+ Mask,
1291
1376
  NavigationControl2 as NavigationControl,
1292
1377
  Popup3 as Popup,
1293
1378
  ScaleControl2 as ScaleControl,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/maps-react",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -14,7 +14,7 @@
14
14
  "dist"
15
15
  ],
16
16
  "dependencies": {
17
- "@geolonia/maps-core": "^0.8.0",
17
+ "@geolonia/maps-core": "^0.9.0",
18
18
  "@geolonia/normalize-japanese-addresses": "^3.1.3"
19
19
  },
20
20
  "peerDependencies": {