@gugell/nitro-ui-maps 0.0.0-stage → 0.2.1
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/CHANGELOG.md +28 -0
- package/LICENSE +21 -0
- package/README.md +66 -2
- package/package.json +54 -3
- package/src/MapProvider.tsx +31 -0
- package/src/createMapView.tsx +18 -0
- package/src/defaults.ts +37 -0
- package/src/engine.ts +31 -0
- package/src/index.ts +26 -0
- package/src/region.ts +32 -0
- package/src/renderMapView.tsx +19 -0
- package/src/resolve.ts +93 -0
- package/src/stable.ts +38 -0
- package/src/types/clustering.ts +26 -0
- package/src/types/engine.ts +38 -0
- package/src/types/geo.ts +42 -0
- package/src/types/markers.ts +66 -0
- package/src/types/provider.ts +10 -0
- package/src/types/view.ts +88 -0
- package/src/useEngineWarnings.ts +19 -0
- package/src/useMapViewOptions.ts +47 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.2.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#2](https://github.com/gugell/nitro-ui-maps/pull/2) [`0ec81a2`](https://github.com/gugell/nitro-ui-maps/commit/0ec81a27b3b38f59389aa26e857e39d46a28a75f) Thanks [@gugell](https://github.com/gugell)! - Faster updates. Picking a marker now changes a layer filter and the one-marker source instead of
|
|
8
|
+
re-sending every marker; marker features are serialized once per marker object; only sources whose
|
|
9
|
+
data changed reach native, and native parses them off the main thread. Marker icons are drawn off
|
|
10
|
+
the main thread and only when new or changed, and icons no longer in use are removed. Inline
|
|
11
|
+
options (camera, gestures, clustering, …) and inline callbacks no longer rebuild the style or
|
|
12
|
+
re-send props each render. The selected marker now stays counted in its cluster.
|
|
13
|
+
|
|
14
|
+
## 0.2.0
|
|
15
|
+
|
|
16
|
+
- The package is now the engine-neutral core: `MapView`, `MapProvider`, `createMapView(engine)`,
|
|
17
|
+
`defineMapEngine` and the shared option types. The MapLibre Native view moved to
|
|
18
|
+
`@gugell/nitro-ui-maps-maplibre`.
|
|
19
|
+
- Options in plain terms instead of MapLibre paint: `pointStyle`, `clustering` (`true` or steps,
|
|
20
|
+
colours and sizes), `gestures`, `userLocation`, `attribution`. Defaults live in the core, so every
|
|
21
|
+
engine draws the same map.
|
|
22
|
+
- Marker icons (`markerIcons`, was `markerImages`) take a per-platform `glyph`
|
|
23
|
+
(`{ sfSymbol, androidDrawable }`), and markers name them with `icon` (was `image`).
|
|
24
|
+
- Engines declare capabilities; the view warns once about an option its engine ignores.
|
|
25
|
+
|
|
26
|
+
## 0.1.0
|
|
27
|
+
|
|
28
|
+
- `MapView` on MapLibre Native (iOS 6.26.0, Android 13.2.0 OpenGL).
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 IAUTI LABS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
9
|
+
of the Software, and to permit persons to whom the Software is furnished to do
|
|
10
|
+
so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,67 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @gugell/nitro-ui-maps
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The engine-neutral core of [nitro-ui-maps](../../README.md): one map API for NitroUI apps. It
|
|
4
|
+
draws nothing by itself; install an engine too (`@gugell/nitro-ui-maps-maplibre`).
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pnpm add @gugell/nitro-ui-maps @gugell/nitro-ui-maps-maplibre # or bun add / npm install
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Peers: `react` 19.3+, `nitro-ui-renderer` 0.3 or 0.4.
|
|
13
|
+
|
|
14
|
+
## Pick the engine
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
import { MapProvider, MapView, createMapView } from '@gugell/nitro-ui-maps'
|
|
18
|
+
import { maplibre } from '@gugell/nitro-ui-maps-maplibre'
|
|
19
|
+
|
|
20
|
+
const engine = maplibre() // module scope: one engine, one view component
|
|
21
|
+
|
|
22
|
+
// App-wide: screens use MapView and never import the engine.
|
|
23
|
+
<MapProvider engine={engine}><Screens /></MapProvider>
|
|
24
|
+
|
|
25
|
+
// Bound: the view also takes the engine's own props, typed.
|
|
26
|
+
const CityMap = createMapView(engine)
|
|
27
|
+
<CityMap mapStyle="https://tiles.example/style.json" />
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## MapView options
|
|
31
|
+
|
|
32
|
+
All optional. Unset options take the core's defaults, the same on every engine.
|
|
33
|
+
|
|
34
|
+
| Option | What it does |
|
|
35
|
+
| --- | --- |
|
|
36
|
+
| `camera` | Controlled: `{ latitude, longitude, zoom, heading? }`. `zoom` is the vector-map zoom (a 512-point world at zoom 0). Feed `onCameraChange` back; a camera within a point of where the map is doesn't move it. Memoize it. |
|
|
37
|
+
| `onCameraChange(camera, userInitiated)` | The camera settled, after a gesture or a move. |
|
|
38
|
+
| `markers` | `{ id, coordinate, icon?, properties? }`. Markers off the globe are dropped. |
|
|
39
|
+
| `selectedMarkerId` | Drawn above the rest, also when it sits in a cluster (the cluster still counts it). Picking a marker changes no marker data, only a filter and a one-marker source. |
|
|
40
|
+
| `markerIcons` | `{ name, shape: 'circle' \| 'roundedSquare', size, fillColor, borderColor?, borderWidth?, glyph?, glyphColor? }`. A marker's `icon` names one. `glyph` is per platform: `{ sfSymbol?: 'wineglass', androidDrawable?: 'ic_bar' }` (a drawable in the app's resources). |
|
|
41
|
+
| `pointStyle` | Markers without an icon: `{ color, radius, borderColor, borderWidth }`. |
|
|
42
|
+
| `clustering` | `true` for the defaults, or `{ radius, maxZoom, steps: [{ minCount, diameter, color }], borderColor, borderWidth, textColor, textSize }`. A cluster tap zooms to where it splits. |
|
|
43
|
+
| `gestures` | `{ pan, zoom, rotate, pitch }`; pitch is off by default. |
|
|
44
|
+
| `userLocation` | `true`, or `{ visible?, color? }`. Never asks for permission by itself. |
|
|
45
|
+
| `insets` | Room taken by views over the map; the camera centres in the rest. |
|
|
46
|
+
| `attribution` | The engine's credit and logo; on by default. |
|
|
47
|
+
| `onMarkerPress(id)`, `onMapPress(coordinate)` | Single taps. |
|
|
48
|
+
|
|
49
|
+
The ref (`MapViewHandle`) has `locateUser(zoom?)`: asks for when-in-use permission the first time,
|
|
50
|
+
shows the user and centres on them; resolves `'located'`, `'denied'` or `'unavailable'`.
|
|
51
|
+
|
|
52
|
+
Pass stable arrays and objects (memoize them): the view resolves each option only when it changes,
|
|
53
|
+
and engines redo native work only for what changed.
|
|
54
|
+
|
|
55
|
+
## For engine authors
|
|
56
|
+
|
|
57
|
+
- `defineMapEngine({ id, capabilities, View })` checks the id (kebab-case), fills undeclared
|
|
58
|
+
capabilities as off and freezes the engine.
|
|
59
|
+
- `View` receives `MapEngineViewProps<YourProps>`: the resolved options (`ResolvedMapViewOptions`),
|
|
60
|
+
layout props, the ref and your engine's props.
|
|
61
|
+
- `resolveMapViewOptions`, the `resolve*` pieces, `useMapViewOptions` and `unsupportedOptions` are
|
|
62
|
+
exported for engines and tests.
|
|
63
|
+
- `regionForZoom` / `zoomForRegion` convert a camera to a centre and span and back.
|
|
64
|
+
|
|
65
|
+
## License
|
|
66
|
+
|
|
67
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,57 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gugell/nitro-ui-maps",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "One map API for NitroUI apps on iOS and Android, drawn by the engine you install (MapLibre Native first): camera, markers with icons and clustering, gestures, the user's location.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"nitro-ui",
|
|
7
|
+
"nitroui",
|
|
8
|
+
"nitro",
|
|
9
|
+
"map",
|
|
10
|
+
"maps",
|
|
11
|
+
"mapview",
|
|
12
|
+
"ios",
|
|
13
|
+
"android"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"homepage": "https://github.com/gugell/nitro-ui-maps#readme",
|
|
17
|
+
"bugs": {
|
|
18
|
+
"url": "https://github.com/gugell/nitro-ui-maps/issues"
|
|
19
|
+
},
|
|
20
|
+
"repository": {
|
|
21
|
+
"type": "git",
|
|
22
|
+
"url": "git+https://github.com/gugell/nitro-ui-maps.git",
|
|
23
|
+
"directory": "packages/core"
|
|
24
|
+
},
|
|
25
|
+
"type": "module",
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"exports": {
|
|
28
|
+
".": "./src/index.ts",
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"nitro-ui-renderer": ">=0.3.0 <0.5.0",
|
|
33
|
+
"react": ">=19.3.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/react": "19.3.0",
|
|
37
|
+
"nitro-core": "1.0.0-alpha.nightly.137.gd4793c40b8b8",
|
|
38
|
+
"nitro-ui-renderer": "0.3.0",
|
|
39
|
+
"react": "19.3.0",
|
|
40
|
+
"typescript": "7.0.2",
|
|
41
|
+
"vitest": "4.1.12"
|
|
42
|
+
},
|
|
43
|
+
"publishConfig": {
|
|
44
|
+
"access": "public"
|
|
45
|
+
},
|
|
46
|
+
"files": [
|
|
47
|
+
"src",
|
|
48
|
+
"!src/**/*.test.ts",
|
|
49
|
+
"README.md",
|
|
50
|
+
"CHANGELOG.md",
|
|
51
|
+
"LICENSE"
|
|
52
|
+
],
|
|
53
|
+
"scripts": {
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"typecheck": "tsc --noEmit -p tsconfig.json"
|
|
56
|
+
}
|
|
6
57
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react'
|
|
2
|
+
import { useMapViewElement } from './renderMapView'
|
|
3
|
+
import type { MapProviderProps } from './types/provider'
|
|
4
|
+
import type { MapEngine, NoEngineProps } from './types/engine'
|
|
5
|
+
import type { MapViewProps } from './types/view'
|
|
6
|
+
|
|
7
|
+
const EngineContext = createContext<MapEngine<NoEngineProps> | null>(null)
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The engine every `MapView` below draws with: pick it once at the app's root and screens stay
|
|
11
|
+
* engine-agnostic. Only an engine whose own props are all optional fits here (a `MapView` passes
|
|
12
|
+
* none); for an engine's props, use `createMapView(engine)`.
|
|
13
|
+
*/
|
|
14
|
+
export function MapProvider<EngineProps extends object>({ engine, children }: MapProviderProps<EngineProps>) {
|
|
15
|
+
// Sound, though TypeScript can't see it: ProvidableMapEngine admits only engines whose own props
|
|
16
|
+
// are all optional, and a MapView passes none of them.
|
|
17
|
+
const provided = engine as unknown as MapEngine<NoEngineProps>
|
|
18
|
+
return <EngineContext value={provided}>{children}</EngineContext>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The engine of the nearest MapProvider. */
|
|
22
|
+
export function useMapEngine(): MapEngine<NoEngineProps> {
|
|
23
|
+
const engine = useContext(EngineContext)
|
|
24
|
+
if (!engine) throw new Error('MapView needs a <MapProvider engine={…}> above it (or use createMapView(engine))')
|
|
25
|
+
return engine
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** A map drawn by the engine of the nearest MapProvider. */
|
|
29
|
+
export function MapView(props: MapViewProps) {
|
|
30
|
+
return useMapViewElement(useMapEngine(), props)
|
|
31
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useMapViewElement } from './renderMapView'
|
|
2
|
+
import type { MapEngine } from './types/engine'
|
|
3
|
+
import type { MapViewProps } from './types/view'
|
|
4
|
+
|
|
5
|
+
/** A map view bound to one engine. */
|
|
6
|
+
export type BoundMapView<EngineProps extends object> = ((props: MapViewProps & EngineProps) => ReturnType<typeof useMapViewElement>) & { displayName: string }
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A map view bound to an engine: takes the shared options and that engine's own props, typed.
|
|
10
|
+
* Call it once at module scope (not in a render), so the component keeps its identity.
|
|
11
|
+
*
|
|
12
|
+
* const CityMap = createMapView(maplibre({ fallbackStyle }))
|
|
13
|
+
* <CityMap mapStyle={styleUrl} markers={markers} clustering />
|
|
14
|
+
*/
|
|
15
|
+
export function createMapView<EngineProps extends object>(engine: MapEngine<EngineProps>): BoundMapView<EngineProps> {
|
|
16
|
+
const View = (props: MapViewProps & EngineProps) => useMapViewElement(engine, props)
|
|
17
|
+
return Object.assign(View, { displayName: `MapView(${engine.id})` })
|
|
18
|
+
}
|
package/src/defaults.ts
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// The one place defaults live: engines receive resolved options and never apply their own, so a
|
|
2
|
+
// map looks the same on every engine.
|
|
3
|
+
import type { MapClusterStyle } from './types/clustering'
|
|
4
|
+
import type { MapInsets } from './types/geo'
|
|
5
|
+
import type { MapPointStyle } from './types/markers'
|
|
6
|
+
import type { MapGestures, MapUserLocation } from './types/view'
|
|
7
|
+
|
|
8
|
+
export const WHITE = '#FFFFFF'
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_POINT_STYLE: MapPointStyle = {
|
|
11
|
+
color: '#7C3AED',
|
|
12
|
+
radius: 8,
|
|
13
|
+
borderColor: WHITE,
|
|
14
|
+
borderWidth: 3,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const DEFAULT_CLUSTER_STYLE: MapClusterStyle = {
|
|
18
|
+
radius: 50,
|
|
19
|
+
maxZoom: 14,
|
|
20
|
+
steps: [
|
|
21
|
+
{ minCount: 1, diameter: 40, color: '#7C3AED' },
|
|
22
|
+
{ minCount: 10, diameter: 52, color: '#6D28D9' },
|
|
23
|
+
{ minCount: 50, diameter: 64, color: '#5B21B6' },
|
|
24
|
+
],
|
|
25
|
+
borderColor: WHITE,
|
|
26
|
+
borderWidth: 3,
|
|
27
|
+
textColor: WHITE,
|
|
28
|
+
textSize: 13,
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const DEFAULT_GESTURES: MapGestures = { pan: true, zoom: true, rotate: true, pitch: false }
|
|
32
|
+
|
|
33
|
+
export const DEFAULT_USER_LOCATION: MapUserLocation = { visible: false, color: null }
|
|
34
|
+
|
|
35
|
+
export const NO_INSETS: MapInsets = { top: 0, right: 0, bottom: 0, left: 0 }
|
|
36
|
+
|
|
37
|
+
export const DEFAULT_ICON_BORDER_WIDTH = 2
|
package/src/engine.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { MapEngine, MapEngineCapabilities, MapEngineDefinition, NoEngineProps } from './types/engine'
|
|
2
|
+
import type { MapCapabilityOptions } from './types/view'
|
|
3
|
+
|
|
4
|
+
/** An engine that draws nothing beyond the basics: what capabilities not declared fall back to. */
|
|
5
|
+
export const NO_CAPABILITIES: MapEngineCapabilities = { clustering: false, rotation: false, pitch: false, userLocation: false, glyphs: [] }
|
|
6
|
+
|
|
7
|
+
const ENGINE_ID = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Makes an engine: checks its id, fills undeclared capabilities as off, and freezes it. Engine
|
|
11
|
+
* packages export a factory that calls this once per configuration (`maplibre(options)`), so an
|
|
12
|
+
* app holds engines as values and swaps them without touching its screens.
|
|
13
|
+
*/
|
|
14
|
+
export function defineMapEngine<EngineProps extends object = NoEngineProps>(definition: MapEngineDefinition<EngineProps>): MapEngine<EngineProps> {
|
|
15
|
+
if (!ENGINE_ID.test(definition.id)) throw new Error(`Map engine id "${definition.id}" must be kebab-case (e.g. "maplibre", "apple-maps")`)
|
|
16
|
+
if (typeof definition.View !== 'function' && typeof definition.View !== 'object') throw new Error(`Map engine "${definition.id}" has no View`)
|
|
17
|
+
const capabilities: MapEngineCapabilities = Object.freeze({ ...NO_CAPABILITIES, ...definition.capabilities })
|
|
18
|
+
return Object.freeze({ id: definition.id, capabilities, View: definition.View })
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** The options an engine would ignore, as messages (empty when it draws them all). */
|
|
22
|
+
export function unsupportedOptions(options: MapCapabilityOptions, capabilities: MapEngineCapabilities): string[] {
|
|
23
|
+
const messages: string[] = []
|
|
24
|
+
if (options.clustering && !capabilities.clustering) messages.push('clustering is not supported: markers show unclustered')
|
|
25
|
+
if (options.gestures.rotate && !capabilities.rotation) messages.push('rotation is not supported: the map stays north up')
|
|
26
|
+
if (options.gestures.pitch && !capabilities.pitch) messages.push('pitch is not supported')
|
|
27
|
+
if (options.userLocation.visible && !capabilities.userLocation) messages.push("the user's location is not supported")
|
|
28
|
+
const glyphKinds = new Set(options.markerIcons.flatMap((icon) => (icon.glyph ? Object.keys(icon.glyph) : [])))
|
|
29
|
+
for (const kind of glyphKinds) if (!capabilities.glyphs.some((k) => k === kind)) messages.push(`${kind} glyphs are not drawn`)
|
|
30
|
+
return messages
|
|
31
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// @gugell/nitro-ui-maps: one map API for NitroUI apps, drawn by whichever engine the app installs
|
|
2
|
+
// (@gugell/nitro-ui-maps-maplibre, …). Screens use MapView and these types; the app picks the engine.
|
|
3
|
+
export { MapProvider, MapView, useMapEngine } from './MapProvider'
|
|
4
|
+
export { createMapView, type BoundMapView } from './createMapView'
|
|
5
|
+
export { defineMapEngine, unsupportedOptions, NO_CAPABILITIES } from './engine'
|
|
6
|
+
export { useMapViewOptions } from './useMapViewOptions'
|
|
7
|
+
export { structuralKey, useLatestCallback, useStructuralValue } from './stable'
|
|
8
|
+
export {
|
|
9
|
+
isOnGlobe,
|
|
10
|
+
resolveClustering,
|
|
11
|
+
resolveGestures,
|
|
12
|
+
resolveMapViewOptions,
|
|
13
|
+
resolveMarkerIcon,
|
|
14
|
+
resolveMarkerIcons,
|
|
15
|
+
resolvePointStyle,
|
|
16
|
+
resolveUserLocation,
|
|
17
|
+
validMarkers,
|
|
18
|
+
} from './resolve'
|
|
19
|
+
export { DEFAULT_CLUSTER_STYLE, DEFAULT_GESTURES, DEFAULT_POINT_STYLE, DEFAULT_USER_LOCATION, NO_INSETS } from './defaults'
|
|
20
|
+
export { WORLD_SIZE, regionForZoom, zoomForRegion } from './region'
|
|
21
|
+
export type * from './types/geo'
|
|
22
|
+
export type * from './types/markers'
|
|
23
|
+
export type * from './types/clustering'
|
|
24
|
+
export type * from './types/view'
|
|
25
|
+
export type * from './types/engine'
|
|
26
|
+
export type * from './types/provider'
|
package/src/region.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// A camera as a region (centre and span) and back, for callers who think in spans. The world is
|
|
2
|
+
// 512 points wide at zoom 0 (the vector-map zoom MapCamera counts in, Web Mercator). Pure.
|
|
3
|
+
import type { MapCamera, MapRegion, MapViewSize } from './types/geo'
|
|
4
|
+
|
|
5
|
+
/** The world's width in points at zoom 0. */
|
|
6
|
+
export const WORLD_SIZE = 512
|
|
7
|
+
const MIN_SPAN = 1e-6
|
|
8
|
+
const MAX_LATITUDE = 85.05112878
|
|
9
|
+
|
|
10
|
+
const mercatorY = (latitude: number) => {
|
|
11
|
+
const lat = (Math.max(-MAX_LATITUDE, Math.min(MAX_LATITUDE, latitude)) * Math.PI) / 180
|
|
12
|
+
return Math.log(Math.tan(Math.PI / 4 + lat / 2))
|
|
13
|
+
}
|
|
14
|
+
const latitudeOf = (y: number) => (Math.atan(Math.sinh(y)) * 180) / Math.PI
|
|
15
|
+
|
|
16
|
+
/** The region a camera shows in a view of this size (unrotated). */
|
|
17
|
+
export function regionForZoom(camera: MapCamera, size: MapViewSize): MapRegion {
|
|
18
|
+
const worldWidth = WORLD_SIZE * 2 ** camera.zoom
|
|
19
|
+
const longitudeDelta = Math.min(360, (size.width / worldWidth) * 360)
|
|
20
|
+
const half = (size.height / worldWidth) * Math.PI
|
|
21
|
+
const y = mercatorY(camera.latitude)
|
|
22
|
+
return { latitude: camera.latitude, longitude: camera.longitude, latitudeDelta: latitudeOf(y + half) - latitudeOf(y - half), longitudeDelta }
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** The camera that fits a region in a view of this size: its centre, at the zoom where both spans fit. */
|
|
26
|
+
export function zoomForRegion(region: MapRegion, size: MapViewSize): MapCamera {
|
|
27
|
+
const byWidth = Math.log2((size.width * 360) / (WORLD_SIZE * Math.max(MIN_SPAN, region.longitudeDelta)))
|
|
28
|
+
const top = mercatorY(region.latitude + region.latitudeDelta / 2)
|
|
29
|
+
const bottom = mercatorY(region.latitude - region.latitudeDelta / 2)
|
|
30
|
+
const byHeight = Math.log2((size.height * 2 * Math.PI) / (WORLD_SIZE * Math.max(MIN_SPAN, top - bottom)))
|
|
31
|
+
return { latitude: region.latitude, longitude: region.longitude, zoom: Math.max(0, Math.min(byWidth, byHeight)) }
|
|
32
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEngineWarnings } from './useEngineWarnings'
|
|
2
|
+
import { useMapViewOptions } from './useMapViewOptions'
|
|
3
|
+
import type { MapEngine, MapEngineViewProps } from './types/engine'
|
|
4
|
+
import type { MapViewProps } from './types/view'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Renders a map on an engine: splits the shared options from layout and engine props, resolves the
|
|
8
|
+
* options, and hands everything to the engine's view. A hook (it calls hooks): only call it at the
|
|
9
|
+
* top of a component, with the same engine on every render.
|
|
10
|
+
*/
|
|
11
|
+
export function useMapViewElement<EngineProps extends object>(engine: MapEngine<EngineProps>, props: MapViewProps & EngineProps) {
|
|
12
|
+
const { camera, markers, selectedMarkerId, markerIcons, pointStyle, clustering, gestures, userLocation, insets, attribution, onCameraChange, onMarkerPress, onMapPress, ...rest } = props
|
|
13
|
+
const resolved = useMapViewOptions({ camera, markers, selectedMarkerId, markerIcons, pointStyle, clustering, gestures, userLocation, insets, attribution, onCameraChange, onMarkerPress, onMapPress })
|
|
14
|
+
useEngineWarnings(engine.id, engine.capabilities, resolved)
|
|
15
|
+
// `rest` is the layout props, the ref and the engine's own props: TypeScript can't narrow a rest of a generic.
|
|
16
|
+
const viewProps = { ...rest, ...resolved } as MapEngineViewProps<EngineProps>
|
|
17
|
+
const View = engine.View
|
|
18
|
+
return <View {...viewProps} />
|
|
19
|
+
}
|
package/src/resolve.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// Options in, resolved options out: the defaults applied, invalid markers dropped, duplicate icons
|
|
2
|
+
// merged. Pure; each piece is exported so the view can memoize it on its own input.
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_CLUSTER_STYLE,
|
|
5
|
+
DEFAULT_GESTURES,
|
|
6
|
+
DEFAULT_ICON_BORDER_WIDTH,
|
|
7
|
+
DEFAULT_POINT_STYLE,
|
|
8
|
+
DEFAULT_USER_LOCATION,
|
|
9
|
+
NO_INSETS,
|
|
10
|
+
WHITE,
|
|
11
|
+
} from './defaults'
|
|
12
|
+
import type { MapClusterStep, MapClusterStyle } from './types/clustering'
|
|
13
|
+
import type { MapMarker, MapMarkerIcon, MapPointStyle, ResolvedMapMarkerIcon } from './types/markers'
|
|
14
|
+
import type { MapGestures, MapUserLocation, MapViewOptions, ResolvedMapViewOptions } from './types/view'
|
|
15
|
+
|
|
16
|
+
const NO_MARKERS: readonly MapMarker[] = []
|
|
17
|
+
const NO_ICONS: readonly ResolvedMapMarkerIcon[] = []
|
|
18
|
+
|
|
19
|
+
export function resolvePointStyle(input: Partial<MapPointStyle> | undefined): MapPointStyle {
|
|
20
|
+
return input ? { ...DEFAULT_POINT_STYLE, ...input } : DEFAULT_POINT_STYLE
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const byMinCount = (a: MapClusterStep, b: MapClusterStep) => a.minCount - b.minCount
|
|
24
|
+
|
|
25
|
+
/** Off for undefined or false; the defaults for true; otherwise the defaults with the given values, steps smallest first. */
|
|
26
|
+
export function resolveClustering(input: boolean | Partial<MapClusterStyle> | undefined): MapClusterStyle | null {
|
|
27
|
+
if (input === undefined || input === false) return null
|
|
28
|
+
if (input === true) return DEFAULT_CLUSTER_STYLE
|
|
29
|
+
const steps = input.steps && input.steps.length > 0 ? [...input.steps].sort(byMinCount) : DEFAULT_CLUSTER_STYLE.steps
|
|
30
|
+
return { ...DEFAULT_CLUSTER_STYLE, ...input, steps }
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function resolveGestures(input: Partial<MapGestures> | undefined): MapGestures {
|
|
34
|
+
return input ? { ...DEFAULT_GESTURES, ...input } : DEFAULT_GESTURES
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** Hidden for undefined or false; shown in the engine's colour for true; otherwise shown unless `visible: false`. */
|
|
38
|
+
export function resolveUserLocation(input: boolean | Partial<MapUserLocation> | undefined): MapUserLocation {
|
|
39
|
+
if (input === undefined || input === false) return DEFAULT_USER_LOCATION
|
|
40
|
+
if (input === true) return { ...DEFAULT_USER_LOCATION, visible: true }
|
|
41
|
+
return { ...DEFAULT_USER_LOCATION, visible: true, ...input }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function resolveMarkerIcon(icon: MapMarkerIcon): ResolvedMapMarkerIcon {
|
|
45
|
+
return {
|
|
46
|
+
name: icon.name,
|
|
47
|
+
shape: icon.shape,
|
|
48
|
+
size: icon.size,
|
|
49
|
+
fillColor: icon.fillColor,
|
|
50
|
+
borderColor: icon.borderColor ?? WHITE,
|
|
51
|
+
borderWidth: icon.borderWidth ?? DEFAULT_ICON_BORDER_WIDTH,
|
|
52
|
+
glyph: icon.glyph ?? null,
|
|
53
|
+
glyphColor: icon.glyphColor ?? WHITE,
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Each icon once: the first of a name wins. */
|
|
58
|
+
export function resolveMarkerIcons(icons: readonly MapMarkerIcon[] | undefined): readonly ResolvedMapMarkerIcon[] {
|
|
59
|
+
if (!icons || icons.length === 0) return NO_ICONS
|
|
60
|
+
const byName = new Map<string, ResolvedMapMarkerIcon>()
|
|
61
|
+
for (const icon of icons) if (!byName.has(icon.name)) byName.set(icon.name, resolveMarkerIcon(icon))
|
|
62
|
+
return [...byName.values()]
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A marker on the globe: finite, latitude within ±90 and longitude within ±180. */
|
|
66
|
+
export const isOnGlobe = (marker: MapMarker): boolean => {
|
|
67
|
+
const { latitude, longitude } = marker.coordinate
|
|
68
|
+
return Number.isFinite(latitude) && Number.isFinite(longitude) && Math.abs(latitude) <= 90 && Math.abs(longitude) <= 180
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** The markers an engine can draw: those on the globe. */
|
|
72
|
+
export function validMarkers(markers: readonly MapMarker[] | undefined): readonly MapMarker[] {
|
|
73
|
+
if (!markers || markers.length === 0) return NO_MARKERS
|
|
74
|
+
return markers.every(isOnGlobe) ? markers : markers.filter(isOnGlobe)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function resolveMapViewOptions(options: MapViewOptions): ResolvedMapViewOptions {
|
|
78
|
+
return {
|
|
79
|
+
camera: options.camera ?? null,
|
|
80
|
+
markers: validMarkers(options.markers),
|
|
81
|
+
selectedMarkerId: options.selectedMarkerId ?? null,
|
|
82
|
+
markerIcons: resolveMarkerIcons(options.markerIcons),
|
|
83
|
+
pointStyle: resolvePointStyle(options.pointStyle),
|
|
84
|
+
clustering: resolveClustering(options.clustering),
|
|
85
|
+
gestures: resolveGestures(options.gestures),
|
|
86
|
+
userLocation: resolveUserLocation(options.userLocation),
|
|
87
|
+
insets: options.insets ?? NO_INSETS,
|
|
88
|
+
attribution: options.attribution !== false,
|
|
89
|
+
onCameraChange: options.onCameraChange,
|
|
90
|
+
onMarkerPress: options.onMarkerPress,
|
|
91
|
+
onMapPress: options.onMapPress,
|
|
92
|
+
}
|
|
93
|
+
}
|
package/src/stable.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Stable identities for what a screen passes inline: an engine's view only redoes native work, and
|
|
2
|
+
// only crosses into native, for what really changed.
|
|
3
|
+
import { useLayoutEffect, useMemo, useRef } from 'react'
|
|
4
|
+
|
|
5
|
+
/** A value and the JSON that stands for it. */
|
|
6
|
+
interface Keyed<T> {
|
|
7
|
+
readonly key: string
|
|
8
|
+
readonly value: T
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** JSON that stands for a small, plain value (no functions, no cycles). */
|
|
12
|
+
export const structuralKey = (value: unknown): string => (value === undefined ? 'undefined' : JSON.stringify(value))
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The same object back while its content is the same: `gestures={{ pitch: false }}` written inline
|
|
16
|
+
* stays one object across renders. For small option objects only; it serializes the value each
|
|
17
|
+
* render (markers stay compared by identity: memoize those).
|
|
18
|
+
*/
|
|
19
|
+
export function useStructuralValue<T>(value: T): T {
|
|
20
|
+
const kept = useRef<Keyed<T> | null>(null)
|
|
21
|
+
const key = structuralKey(value)
|
|
22
|
+
// Idempotent: a render React throws away leaves an entry equal to what it would compute again.
|
|
23
|
+
if (kept.current === null || kept.current.key !== key) kept.current = { key, value }
|
|
24
|
+
return kept.current.value
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* One function for the component's life that calls the latest `callback`, or undefined while there
|
|
29
|
+
* is none: an inline `onCameraChange={…}` doesn't hand native a new function every render.
|
|
30
|
+
*/
|
|
31
|
+
export function useLatestCallback<Args extends unknown[]>(callback: ((...args: Args) => void) | undefined): ((...args: Args) => void) | undefined {
|
|
32
|
+
const latest = useRef(callback)
|
|
33
|
+
useLayoutEffect(() => {
|
|
34
|
+
latest.current = callback
|
|
35
|
+
})
|
|
36
|
+
const stable = useMemo(() => (...args: Args) => latest.current?.(...args), [])
|
|
37
|
+
return callback ? stable : undefined
|
|
38
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { ColorValue } from 'nitro-ui-renderer'
|
|
2
|
+
|
|
3
|
+
/** A cluster's look from `minCount` points up: its diameter in points and fill. */
|
|
4
|
+
export interface MapClusterStep {
|
|
5
|
+
readonly minCount: number
|
|
6
|
+
readonly diameter: number
|
|
7
|
+
readonly color: ColorValue
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* How close markers merge into counted bubbles. The selected marker is drawn above them (a cluster
|
|
12
|
+
* around it still counts it); a tap on a cluster zooms to where it splits.
|
|
13
|
+
*/
|
|
14
|
+
export interface MapClusterStyle {
|
|
15
|
+
/** How near, in points, markers merge. */
|
|
16
|
+
readonly radius: number
|
|
17
|
+
/** The last zoom that clusters; above it every marker shows. */
|
|
18
|
+
readonly maxZoom: number
|
|
19
|
+
/** Bubble looks by size, smallest first; the first applies from one point up. */
|
|
20
|
+
readonly steps: readonly MapClusterStep[]
|
|
21
|
+
readonly borderColor: ColorValue
|
|
22
|
+
readonly borderWidth: number
|
|
23
|
+
/** The count's colour and size in points. */
|
|
24
|
+
readonly textColor: ColorValue
|
|
25
|
+
readonly textSize: number
|
|
26
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { ComponentType } from 'react'
|
|
2
|
+
import type { MapGlyphKind } from './markers'
|
|
3
|
+
import type { MapLayoutProps, MapViewRefProp, ResolvedMapViewOptions } from './view'
|
|
4
|
+
|
|
5
|
+
/** What an engine can draw, so an app can adapt and the view can warn about options it ignores. */
|
|
6
|
+
export interface MapEngineCapabilities {
|
|
7
|
+
readonly clustering: boolean
|
|
8
|
+
readonly rotation: boolean
|
|
9
|
+
readonly pitch: boolean
|
|
10
|
+
readonly userLocation: boolean
|
|
11
|
+
/** The glyph kinds its marker icons draw. */
|
|
12
|
+
readonly glyphs: readonly MapGlyphKind[]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** No engine-specific props. */
|
|
16
|
+
export type NoEngineProps = Record<never, never>
|
|
17
|
+
|
|
18
|
+
/** What an engine's view receives: resolved options, layout, the ref, and the engine's own props. */
|
|
19
|
+
export type MapEngineViewProps<EngineProps extends object = NoEngineProps> = ResolvedMapViewOptions & MapLayoutProps & MapViewRefProp & EngineProps
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* A map engine: a native map behind the shared options. `EngineProps` are the props only this
|
|
23
|
+
* engine understands (a MapLibre style, a Google map id); a view made with `createMapView(engine)`
|
|
24
|
+
* takes them next to the shared ones, typed.
|
|
25
|
+
*/
|
|
26
|
+
export interface MapEngine<EngineProps extends object = NoEngineProps> {
|
|
27
|
+
/** A short, unique, kebab-case name (`maplibre`, `apple-maps`). */
|
|
28
|
+
readonly id: string
|
|
29
|
+
readonly capabilities: MapEngineCapabilities
|
|
30
|
+
readonly View: ComponentType<MapEngineViewProps<EngineProps>>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What `defineMapEngine` takes: capabilities not given are off. */
|
|
34
|
+
export interface MapEngineDefinition<EngineProps extends object = NoEngineProps> {
|
|
35
|
+
readonly id: string
|
|
36
|
+
readonly capabilities?: Partial<MapEngineCapabilities>
|
|
37
|
+
readonly View: ComponentType<MapEngineViewProps<EngineProps>>
|
|
38
|
+
}
|
package/src/types/geo.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** A point on the map, in degrees (WGS 84). */
|
|
2
|
+
export interface MapCoordinate {
|
|
3
|
+
readonly latitude: number
|
|
4
|
+
readonly longitude: number
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Where the map looks. `zoom` is the vector-map zoom (a 512-point world at zoom 0, as MapLibre and
|
|
9
|
+
* Mapbox count it; one level below the 256-pixel zoom of raster tile URLs). Every engine converts
|
|
10
|
+
* from this one scale. `heading` is degrees clockwise from north; undefined keeps the map's.
|
|
11
|
+
*/
|
|
12
|
+
export interface MapCamera {
|
|
13
|
+
readonly latitude: number
|
|
14
|
+
readonly longitude: number
|
|
15
|
+
readonly zoom: number
|
|
16
|
+
readonly heading?: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Room, in points, kept clear at each edge for views drawn over the map; the camera centres in what's left. */
|
|
20
|
+
export interface MapInsets {
|
|
21
|
+
readonly top: number
|
|
22
|
+
readonly right: number
|
|
23
|
+
readonly bottom: number
|
|
24
|
+
readonly left: number
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A centre and the span around it, in degrees. */
|
|
28
|
+
export interface MapRegion {
|
|
29
|
+
readonly latitude: number
|
|
30
|
+
readonly longitude: number
|
|
31
|
+
readonly latitudeDelta: number
|
|
32
|
+
readonly longitudeDelta: number
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** A view's size in points. */
|
|
36
|
+
export interface MapViewSize {
|
|
37
|
+
readonly width: number
|
|
38
|
+
readonly height: number
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** How a request to centre on the user ended. */
|
|
42
|
+
export type MapLocateResult = 'located' | 'denied' | 'unavailable'
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { ColorValue } from 'nitro-ui-renderer'
|
|
2
|
+
import type { MapCoordinate } from './geo'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* A glyph drawn in the middle of a marker icon, per platform: an SF Symbol name on iOS, a drawable
|
|
6
|
+
* resource name of the app on Android (`R.drawable.<name>`). A platform without one draws the
|
|
7
|
+
* shape alone. An engine lists the kinds it draws in its capabilities.
|
|
8
|
+
*/
|
|
9
|
+
export interface MapGlyph {
|
|
10
|
+
readonly sfSymbol?: string
|
|
11
|
+
readonly androidDrawable?: string
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** The kinds of glyph there are. */
|
|
15
|
+
export type MapGlyphKind = keyof MapGlyph
|
|
16
|
+
|
|
17
|
+
/** The shape a marker icon is drawn in. */
|
|
18
|
+
export type MapMarkerShape = 'circle' | 'roundedSquare'
|
|
19
|
+
|
|
20
|
+
/** A marker icon the engine draws natively, named so markers can refer to it. */
|
|
21
|
+
export interface MapMarkerIcon {
|
|
22
|
+
readonly name: string
|
|
23
|
+
readonly shape: MapMarkerShape
|
|
24
|
+
/** Width and height in points, the border included. */
|
|
25
|
+
readonly size: number
|
|
26
|
+
readonly fillColor: ColorValue
|
|
27
|
+
/** Defaults to white. */
|
|
28
|
+
readonly borderColor?: ColorValue
|
|
29
|
+
/** Defaults to 2. */
|
|
30
|
+
readonly borderWidth?: number
|
|
31
|
+
readonly glyph?: MapGlyph
|
|
32
|
+
/** Defaults to white. */
|
|
33
|
+
readonly glyphColor?: ColorValue
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** A marker icon with every default applied: what an engine draws. */
|
|
37
|
+
export interface ResolvedMapMarkerIcon {
|
|
38
|
+
readonly name: string
|
|
39
|
+
readonly shape: MapMarkerShape
|
|
40
|
+
readonly size: number
|
|
41
|
+
readonly fillColor: ColorValue
|
|
42
|
+
readonly borderColor: ColorValue
|
|
43
|
+
readonly borderWidth: number
|
|
44
|
+
readonly glyph: MapGlyph | null
|
|
45
|
+
readonly glyphColor: ColorValue
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** Extra data a marker carries (an engine may expose it to its own styling, e.g. a category). */
|
|
49
|
+
export type MapMarkerProperties = Readonly<Record<string, string | number | boolean>>
|
|
50
|
+
|
|
51
|
+
/** A point on the map. `icon` names one of the view's `markerIcons`; without one it's drawn as a point (`pointStyle`). */
|
|
52
|
+
export interface MapMarker {
|
|
53
|
+
readonly id: string
|
|
54
|
+
readonly coordinate: MapCoordinate
|
|
55
|
+
readonly icon?: string
|
|
56
|
+
readonly properties?: MapMarkerProperties
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** How a marker without an icon is drawn: a dot with a ring. */
|
|
60
|
+
export interface MapPointStyle {
|
|
61
|
+
readonly color: ColorValue
|
|
62
|
+
/** In points, the ring excluded. */
|
|
63
|
+
readonly radius: number
|
|
64
|
+
readonly borderColor: ColorValue
|
|
65
|
+
readonly borderWidth: number
|
|
66
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import type { MapEngine } from './engine'
|
|
3
|
+
|
|
4
|
+
/** An engine a MapProvider can hold: one whose own props are all optional, else `never`. */
|
|
5
|
+
export type ProvidableMapEngine<EngineProps extends object> = Partial<EngineProps> extends EngineProps ? MapEngine<EngineProps> : never
|
|
6
|
+
|
|
7
|
+
export interface MapProviderProps<EngineProps extends object> {
|
|
8
|
+
readonly engine: ProvidableMapEngine<EngineProps>
|
|
9
|
+
readonly children?: ReactNode
|
|
10
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import type { Ref } from 'react'
|
|
2
|
+
import type { ColorValue, SharedViewProps } from 'nitro-ui-renderer'
|
|
3
|
+
import type { MapClusterStyle } from './clustering'
|
|
4
|
+
import type { MapCamera, MapCoordinate, MapInsets, MapLocateResult } from './geo'
|
|
5
|
+
import type { MapMarker, MapMarkerIcon, MapPointStyle, ResolvedMapMarkerIcon } from './markers'
|
|
6
|
+
|
|
7
|
+
/** Which gestures move the map. The engine's own: inertia, double-tap and two-finger tap included. */
|
|
8
|
+
export interface MapGestures {
|
|
9
|
+
readonly pan: boolean
|
|
10
|
+
readonly zoom: boolean
|
|
11
|
+
readonly rotate: boolean
|
|
12
|
+
readonly pitch: boolean
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** The user's location dot. Shown once the app may read the location; asking is `locateUser`'s job. */
|
|
16
|
+
export interface MapUserLocation {
|
|
17
|
+
readonly visible: boolean
|
|
18
|
+
/** The engine's own colour when null. */
|
|
19
|
+
readonly color: ColorValue | null
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** What a map tells its screen. */
|
|
23
|
+
export interface MapViewEvents {
|
|
24
|
+
/** The camera settled after a move; `userInitiated` when a gesture moved it. Feed it back as `camera`. */
|
|
25
|
+
readonly onCameraChange?: (camera: MapCamera, userInitiated: boolean) => void
|
|
26
|
+
/** A marker was tapped. */
|
|
27
|
+
readonly onMarkerPress?: (markerId: string) => void
|
|
28
|
+
/** The map was tapped away from any marker. */
|
|
29
|
+
readonly onMapPress?: (coordinate: MapCoordinate) => void
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A map view's options, whatever engine draws it. Every one is optional: `resolveMapViewOptions`
|
|
34
|
+
* fills the defaults, so engines never apply their own.
|
|
35
|
+
*/
|
|
36
|
+
export interface MapViewOptions extends MapViewEvents {
|
|
37
|
+
/** Controlled camera. The map reports where it settles through `onCameraChange`. */
|
|
38
|
+
readonly camera?: MapCamera
|
|
39
|
+
readonly markers?: readonly MapMarker[]
|
|
40
|
+
/** Drawn above the others; a cluster around it still counts it. */
|
|
41
|
+
readonly selectedMarkerId?: string | null
|
|
42
|
+
readonly markerIcons?: readonly MapMarkerIcon[]
|
|
43
|
+
/** How markers without an icon are drawn. */
|
|
44
|
+
readonly pointStyle?: Partial<MapPointStyle>
|
|
45
|
+
/** `true` for the default bubbles, an object to change them; off when absent or false. */
|
|
46
|
+
readonly clustering?: boolean | Partial<MapClusterStyle>
|
|
47
|
+
/** Pan, zoom and rotate are on, pitch off, unless set. */
|
|
48
|
+
readonly gestures?: Partial<MapGestures>
|
|
49
|
+
/** `true` shows the dot in the engine's colour; an object sets its colour. */
|
|
50
|
+
readonly userLocation?: boolean | Partial<MapUserLocation>
|
|
51
|
+
readonly insets?: MapInsets
|
|
52
|
+
/** The engine's attribution and logo. On by default: turn it off only if the app credits the map elsewhere. */
|
|
53
|
+
readonly attribution?: boolean
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The options with every default applied and invalid markers dropped: what an engine's view receives. */
|
|
57
|
+
export interface ResolvedMapViewOptions extends MapViewEvents {
|
|
58
|
+
readonly camera: MapCamera | null
|
|
59
|
+
readonly markers: readonly MapMarker[]
|
|
60
|
+
readonly selectedMarkerId: string | null
|
|
61
|
+
readonly markerIcons: readonly ResolvedMapMarkerIcon[]
|
|
62
|
+
readonly pointStyle: MapPointStyle
|
|
63
|
+
readonly clustering: MapClusterStyle | null
|
|
64
|
+
readonly gestures: MapGestures
|
|
65
|
+
readonly userLocation: MapUserLocation
|
|
66
|
+
readonly insets: MapInsets
|
|
67
|
+
readonly attribution: boolean
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The resolved options an engine's capabilities speak to. */
|
|
71
|
+
export type MapCapabilityOptions = Pick<ResolvedMapViewOptions, 'clustering' | 'gestures' | 'userLocation' | 'markerIcons'>
|
|
72
|
+
|
|
73
|
+
/** What a map view's ref can do. */
|
|
74
|
+
export interface MapViewHandle {
|
|
75
|
+
/** Asks for location permission the first time, shows the user, and centres on them at `zoom` (the current zoom when undefined). */
|
|
76
|
+
locateUser(zoom?: number): Promise<MapLocateResult>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** The layout and style props every NitroUI view takes (children excepted: a map has none). */
|
|
80
|
+
export type MapLayoutProps = Omit<SharedViewProps, 'children'>
|
|
81
|
+
|
|
82
|
+
/** The ref a map view takes. */
|
|
83
|
+
export interface MapViewRefProp {
|
|
84
|
+
readonly ref?: Ref<MapViewHandle>
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** A map view's props: options, layout and the ref. */
|
|
88
|
+
export type MapViewProps = MapViewOptions & MapLayoutProps & MapViewRefProp
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useEffect } from 'react'
|
|
2
|
+
import { unsupportedOptions } from './engine'
|
|
3
|
+
import type { MapEngineCapabilities } from './types/engine'
|
|
4
|
+
import type { MapCapabilityOptions } from './types/view'
|
|
5
|
+
|
|
6
|
+
const warned = new Set<string>()
|
|
7
|
+
|
|
8
|
+
/** Warns once per engine and message about options the engine ignores. */
|
|
9
|
+
export function useEngineWarnings(engineId: string, capabilities: MapEngineCapabilities, options: MapCapabilityOptions): void {
|
|
10
|
+
const { clustering, gestures, userLocation, markerIcons } = options
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
for (const message of unsupportedOptions({ clustering, gestures, userLocation, markerIcons }, capabilities)) {
|
|
13
|
+
const key = `${engineId}: ${message}`
|
|
14
|
+
if (warned.has(key)) continue
|
|
15
|
+
warned.add(key)
|
|
16
|
+
console.warn(`[nitro-ui-maps] ${key}`)
|
|
17
|
+
}
|
|
18
|
+
}, [engineId, capabilities, clustering, gestures, userLocation, markerIcons])
|
|
19
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useMemo } from 'react'
|
|
2
|
+
import { NO_INSETS } from './defaults'
|
|
3
|
+
import { resolveClustering, resolveGestures, resolveMarkerIcons, resolvePointStyle, resolveUserLocation, validMarkers } from './resolve'
|
|
4
|
+
import { useLatestCallback, useStructuralValue } from './stable'
|
|
5
|
+
import type { MapViewOptions, ResolvedMapViewOptions } from './types/view'
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* `resolveMapViewOptions` for a component, keeping identities stable so an engine's view redoes
|
|
9
|
+
* native work only for what changed:
|
|
10
|
+
* - small options (camera, styles, clustering, gestures, user location, insets, icons) are compared
|
|
11
|
+
* by content, so writing them inline costs nothing;
|
|
12
|
+
* - callbacks are one function each for the view's life, calling the latest one passed;
|
|
13
|
+
* - markers are compared by identity (a list can be large): memoize the array.
|
|
14
|
+
*/
|
|
15
|
+
export function useMapViewOptions(options: MapViewOptions): ResolvedMapViewOptions {
|
|
16
|
+
const camera = useStructuralValue(options.camera)
|
|
17
|
+
const markerIcons = useStructuralValue(options.markerIcons)
|
|
18
|
+
const pointStyle = useStructuralValue(options.pointStyle)
|
|
19
|
+
const clustering = useStructuralValue(options.clustering)
|
|
20
|
+
const gestures = useStructuralValue(options.gestures)
|
|
21
|
+
const userLocation = useStructuralValue(options.userLocation)
|
|
22
|
+
const insets = useStructuralValue(options.insets)
|
|
23
|
+
const onCameraChange = useLatestCallback(options.onCameraChange)
|
|
24
|
+
const onMarkerPress = useLatestCallback(options.onMarkerPress)
|
|
25
|
+
const onMapPress = useLatestCallback(options.onMapPress)
|
|
26
|
+
const markers = useMemo(() => validMarkers(options.markers), [options.markers])
|
|
27
|
+
const resolvedIcons = useMemo(() => resolveMarkerIcons(markerIcons), [markerIcons])
|
|
28
|
+
const resolvedPointStyle = useMemo(() => resolvePointStyle(pointStyle), [pointStyle])
|
|
29
|
+
const resolvedClustering = useMemo(() => resolveClustering(clustering), [clustering])
|
|
30
|
+
const resolvedGestures = useMemo(() => resolveGestures(gestures), [gestures])
|
|
31
|
+
const resolvedUserLocation = useMemo(() => resolveUserLocation(userLocation), [userLocation])
|
|
32
|
+
return {
|
|
33
|
+
camera: camera ?? null,
|
|
34
|
+
markers,
|
|
35
|
+
selectedMarkerId: options.selectedMarkerId ?? null,
|
|
36
|
+
markerIcons: resolvedIcons,
|
|
37
|
+
pointStyle: resolvedPointStyle,
|
|
38
|
+
clustering: resolvedClustering,
|
|
39
|
+
gestures: resolvedGestures,
|
|
40
|
+
userLocation: resolvedUserLocation,
|
|
41
|
+
insets: insets ?? NO_INSETS,
|
|
42
|
+
attribution: options.attribution !== false,
|
|
43
|
+
onCameraChange,
|
|
44
|
+
onMarkerPress,
|
|
45
|
+
onMapPress,
|
|
46
|
+
}
|
|
47
|
+
}
|