Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions examples/basic/src/components/LoadingOverlay.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useEffect, useRef, useState } from 'react';

/** Shorter loads finish before the dialog appears, so small samples do not flash it. */
const SHOW_DELAY_MS = 150;

/**
* "Loading…" over the map while `visible`, put inside `<MapViewContainer>`
* beside the collections it waits for. It does not block the map.
*/
export function LoadingOverlay({ visible }: { visible: boolean }) {
const [shown, setShown] = useState(false);
useEffect(() => {
if (!visible) {
setShown(false);
return;
}
const timer = setTimeout(() => setShown(true), SHOW_DELAY_MS);
return () => clearTimeout(timer);
}, [visible]);
if (!shown) return null;
return (
<div className="loading-overlay" role="status" aria-live="polite">
<div className="loading-overlay-dialog">
<span className="loading-overlay-spinner" aria-hidden="true" />
Loading...
</div>
</div>
);
}

/**
* Whether any of a page's collections (`<Markers>`, `<Circles>`, `<Polylines>`,
* `<Polygons>`) is still putting its states on the map.
*
* Spread `bind(key)` into each collection, one key per collection. A
* collection that stops being rendered while it loads gets no `onAddEnd`, so a
* key not bound in a render stops counting.
*/
export function useLoadingOverlay() {
const [active, setActive] = useState<ReadonlySet<string>>(() => new Set());
const bound = new Set<string>();
const boundRef = useRef(bound);
boundRef.current = bound;

useEffect(() => {

Check warning on line 45 in examples/basic/src/components/LoadingOverlay.tsx

View workflow job for this annotation

GitHub Actions / Unit tests

react-hooks(exhaustive-deps)

examples/basic/src/components/LoadingOverlay.tsx:45:3: React Hook useEffect contains a call to setState. Without a list of dependencies, this can lead to an infinite chain of updates.
setActive(current => {
const kept = [...current].filter(key => boundRef.current.has(key));
return kept.length === current.size ? current : new Set(kept);
});
});

const bind = (key: string) => {
bound.add(key);
return {
onAddStart: () => setActive(current => (current.has(key) ? current : new Set(current).add(key))),
onAddEnd: () =>
setActive(current => {
if (!current.has(key)) return current;
const next = new Set(current);
next.delete(key);
return next;
}),
};
};

return { active: active.size > 0, bind };
}
38 changes: 38 additions & 0 deletions examples/basic/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1184,3 +1184,41 @@ body,
.diagnostic.error {
color: #b91c1c;
}

/* "Loading..." while a collection puts its states on the map (LoadingOverlay). */
.loading-overlay {
position: absolute;
inset: 0;
display: flex;
align-items: center;
justify-content: center;
pointer-events: none;
z-index: 1500;
}

.loading-overlay-dialog {
display: flex;
align-items: center;
gap: 10px;
padding: 12px 20px;
border-radius: 8px;
background: rgba(17, 24, 39, 0.85);
color: white;
font-size: 0.95rem;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
}

.loading-overlay-spinner {
width: 16px;
height: 16px;
border: 2px solid rgba(255, 255, 255, 0.35);
border-top-color: white;
border-radius: 50%;
animation: loading-overlay-spin 0.8s linear infinite;
}

@keyframes loading-overlay-spin {
to {
transform: rotate(360deg);
}
}
5 changes: 4 additions & 1 deletion examples/basic/src/pages/MarkerPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createGeoPoint, createMarkerState, type MarkerState } from '@mapconduct
import { Markers } from '@mapconductor/js-sdk-react';
import { Toast, useToast } from '../components/Toast';
import { MapViewContainer } from '../MapViewContainer';
import { LoadingOverlay, useLoadingOverlay } from '../components/LoadingOverlay';

const INIT_CAMERA = { lat: 21.2910, lng: -157.8380, zoom: 12 };

Expand All @@ -13,6 +14,7 @@ const MARKER_DATA = [
];

export function MarkerPage() {
const loading = useLoadingOverlay();
const { messages, showToast, dismissToast } = useToast();
const showToastRef = useRef(showToast);
showToastRef.current = showToast;
Expand All @@ -34,7 +36,8 @@ export function MarkerPage() {

return (
<MapViewContainer initialCamera={INIT_CAMERA}>
<Markers states={markers} />
<Markers states={markers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
<Toast messages={messages} onDismiss={dismissToast} />
</MapViewContainer>
);
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/PolygonPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import { Toast, useToast } from '../components/Toast';
import { POLYGON_VERTICES } from '../data/storeData';
import { MapViewContainer } from '../MapViewContainer';
import { useSampleI18n } from '../samples/i18n';
import { LoadingOverlay, useLoadingOverlay } from '../components/LoadingOverlay';

const INIT_CAMERA = { lat: 41.7969, lng: 140.7569, zoom: 16 };

export function PolygonPage() {
const loading = useLoadingOverlay();
const { t } = useSampleI18n();
const [vertices, setVertices] = useState<GeoPoint[]>(POLYGON_VERTICES);
const [fillOpacity, setFillOpacity] = useState(0.3);
Expand Down Expand Up @@ -68,7 +70,8 @@ export function PolygonPage() {
return (
<MapViewContainer initialCamera={INIT_CAMERA}>
<Polygon state={polygonState} />
<Markers states={vertexMarkers} />
<Markers states={vertexMarkers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
<ControlPanel title={t(
{
en: 'Polygon Example',
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/PolylinePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Markers, Polyline } from '@mapconductor/js-sdk-react';
import { ControlPanel } from '../components/ControlPanel';
import { MapViewContainer } from '../MapViewContainer';
import { useSampleI18n } from '../samples/i18n';
import { LoadingOverlay, useLoadingOverlay } from '../components/LoadingOverlay';

const INIT_CAMERA = { lat: 21.382314, lng: -157.933097, zoom: 15 };

Expand All @@ -33,6 +34,7 @@ const INITIAL_POINTS: GeoPoint[] = [
];

export function PolylinePage() {
const loading = useLoadingOverlay();
const { t } = useSampleI18n();
const [points, setPoints] = useState<GeoPoint[]>(INITIAL_POINTS);
const setPointsRef = useRef(setPoints);
Expand Down Expand Up @@ -80,7 +82,8 @@ export function PolylinePage() {
return (
<MapViewContainer initialCamera={INIT_CAMERA}>
<Polyline state={polylineState} />
<Markers states={wayPointMarkers} />
<Markers states={wayPointMarkers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
<ControlPanel title={t({ en: 'Description', ja: '説明', 'es-419': 'Descripción', de: 'Beschreibung', th: 'คำอธิบาย', hi: 'विवरण' })}>
<p className="control-panel-note">
{t(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createCircleState, createGeoPoint, type CircleEvent, type CircleState }
import { Circles, InfoBubble } from '@mapconductor/js-sdk-react';
import { MapViewContainer } from '../../../MapViewContainer';
import { ControlPanel } from '../../../components/ControlPanel';
import { LoadingOverlay, useLoadingOverlay } from '../../../components/LoadingOverlay';

import { decodeAntennas, type Antenna } from './antennaData';
const CARRIERS = ['AT&T', 'T-Mobile / Sprint', 'Verizon', 'Others'];
Expand All @@ -24,6 +25,7 @@ export function AntennaHorizonsPage() {
const [carriers, setCarriers] = useState([0, 1, 2, 3]);
const [alaska, setAlaska] = useState(false);
const [tap, setTap] = useState<Antenna | null>(null);
const loading = useLoadingOverlay();
useEffect(() => {
const abort = new AbortController();
fetch('/antennas/us-antennas.bin', { signal: abort.signal }).then(r => {
Expand Down Expand Up @@ -59,7 +61,8 @@ export function AntennaHorizonsPage() {
const selection = useMemo(() => new CircleSelection(states), [states]);
useEffect(() => { setTap(null); }, [states]);
return <MapViewContainer key={String(alaska)} initialCamera={alaska ? { lat: 64, lng: -150, zoom: 5 } : { lat: 39, lng: -98, zoom: 4 }} onMapClick={() => setTap(null)}>
<Circles states={selection} />
<Circles states={selection} {...loading.bind('circles')} />
<LoadingOverlay visible={loading.active} />
{tap && <InfoBubble position={createGeoPoint(tap)}><div>{CARRIERS[tap.carrier]}<br />Cell position · Display radius: 500 m</div></InfoBubble>}
<ControlPanel title="US cell positions (MCC 310–314)">
<div>{error || (!data ? 'Loading OpenCellID data…' : `${states.length.toLocaleString()} / ${data.length.toLocaleString()} circles`)}</div>
Expand Down
7 changes: 5 additions & 2 deletions examples/basic/src/pages/circle/boston/StackCheck.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useMemo } from 'react';
import { createGeoPoint, createMarkerState, createPolygonState, createPolylineState, type GeoPoint } from '@mapconductor/js-sdk-core';
import { Marker, Polygons, Polylines } from '@mapconductor/js-sdk-react';
import { LoadingOverlay, useLoadingOverlay } from '../../../components/LoadingOverlay';

/** Opaque colours, so a pixel shows which one is on top. */
export const STACK_SQUARE_COLOR = '#7C3AED';
Expand All @@ -21,6 +22,7 @@ const MARKER_BELOW_ROAD = 0.0001;
* in every cell; the road of the cell at the centre runs across the marker.
*/
export function StackCheck({ center }: { center: GeoPoint }) {
const loading = useLoadingOverlay();
const { squares, roads } = useMemo(() => {
const squares = [];
const roads = [];
Expand Down Expand Up @@ -55,8 +57,9 @@ export function StackCheck({ center }: { center: GeoPoint }) {
}), [center]);
return (
<>
<Polygons states={squares} />
<Polylines states={roads} />
<Polygons states={squares} {...loading.bind('polygons')} />
<Polylines states={roads} {...loading.bind('polylines')} />
<LoadingOverlay visible={loading.active} />
<Marker state={marker} />
</>
);
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/groundimage/GroundImagePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import { ControlPanel, SliderControl } from '../../components/ControlPanel';
import { Toast, useToast } from '../../components/Toast';
import { MapViewContainer } from '../../MapViewContainer';
import { useSampleI18n } from '../../samples/i18n';
import { LoadingOverlay, useLoadingOverlay } from '../../components/LoadingOverlay';

const INIT_CAMERA = { lat: -26.479235, lng: 31.306239, zoom: 15 };
const GROUND_IMAGE_URL = `${import.meta.env.BASE_URL}university_of_eswatini.png`;

export function GroundImagePage() {
const loading = useLoadingOverlay();
const { t } = useSampleI18n();
const { messages, showToast, dismissToast } = useToast();
const [southWest, setSouthWest] = useState(createGeoPoint({ latitude: -26.484901389754125, longitude: 31.2995982170105 }));
Expand Down Expand Up @@ -110,7 +112,8 @@ export function GroundImagePage() {
return (
<MapViewContainer initialCamera={INIT_CAMERA}>
<GroundImage state={image} />
<Markers states={markers} />
<Markers states={markers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
<Polyline state={polylineState} />
<ControlPanel title={t(
{
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/infobubble/MultipleBubblesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { useMemo, useState } from 'react';
import { ColorDefaultIcon, createGeoPoint, createMarkerState } from '@mapconductor/js-sdk-core';
import { InfoBubble, Markers } from '@mapconductor/js-sdk-react';
import { MapViewContainer } from '../../MapViewContainer';
import { LoadingOverlay, useLoadingOverlay } from '../../components/LoadingOverlay';

const INIT_CAMERA = { lat: 37.7749, lng: -122.4194, zoom: 15, tilt: 45 };

export function MultipleBubblesPage() {
const loading = useLoadingOverlay();
const [selectedMarkerIds, setSelectedMarkerIds] = useState<Set<string>>(
() => new Set(['marker_0', 'marker_1', 'marker_2'])
);
Expand Down Expand Up @@ -48,7 +50,8 @@ export function MultipleBubblesPage() {

return (
<MapViewContainer initialCamera={INIT_CAMERA} onMapClick={() => setSelectedMarkerIds(new Set())}>
<Markers states={markers} />
<Markers states={markers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
{markers.map(marker =>
selectedMarkerIds.has(marker.id) ? (
<InfoBubble key={marker.id} marker={marker} bubbleColor="#ffffff" borderColor="#000000">
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/map/basic/StoreMapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { InfoBubble, Markers } from '@mapconductor/js-sdk-react';
import { STORES, type StoreInfo } from '../../../data/storeData';
import { MapViewContainer } from '../../../MapViewContainer';
import { StoreInfoView } from './StoreInfoView';
import { LoadingOverlay, useLoadingOverlay } from '../../../components/LoadingOverlay';

const INIT_CAMERA = { lat: 21.382314, lng: -157.933097, zoom: 10 };

Expand All @@ -22,6 +23,7 @@ const STORE_ICON_URLS: Record<string, string> = {
};

export function MapPage() {
const loading = useLoadingOverlay();
// Gets the mapViewState for controll map view.
const [mapViewState, setMapViewState] = useState<MapViewStateInterface<MapDesignTypeInterface<unknown>> | null>(null);
// Holds the selected marker state.
Expand Down Expand Up @@ -76,7 +78,8 @@ export function MapPage() {

return (
<MapViewContainer initialCamera={INIT_CAMERA} onStateReady={setMapViewState} onMapClick={() => setSelectedMarker(null)}>
<Markers states={markers} />
<Markers states={markers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
{selectedMarker && (
<InfoBubble marker={selectedMarker} bubbleColor="#ffffff">
<StoreInfoView info={selectedMarker.extra as StoreInfo} />
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/map/flyto/FlyToPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Markers, Polyline } from '@mapconductor/js-sdk-react';
import { ControlPanel } from '../../../components/ControlPanel';
import { MapViewContainer } from '../../../MapViewContainer';
import { useSampleI18n } from '../../../samples/i18n';
import { LoadingOverlay, useLoadingOverlay } from '../../../components/LoadingOverlay';

const INIT_CAMERA = { lat: 35.0, lng: 0.0, zoom: 3 };

Expand All @@ -34,6 +35,7 @@ const CONNECTIONS = [
] as const;

function FlyToContent({ mapViewState }: { mapViewState: MapViewStateInterface<MapDesignTypeInterface<unknown>> }) {
const loading = useLoadingOverlay();
const { t } = useSampleI18n();
const [icons, setIcons] = useState<Map<string, ImageIcon> | null>(null);
const [geodesic, setGeodesic] = useState(false);
Expand Down Expand Up @@ -82,7 +84,8 @@ function FlyToContent({ mapViewState }: { mapViewState: MapViewStateInterface<Ma
return (
<>
{polylines.map(polyline => <Polyline key={polyline.id} state={polyline} />)}
<Markers states={markers} />
<Markers states={markers} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
<ControlPanel title={t(
{
en: 'Fly To',
Expand Down
5 changes: 4 additions & 1 deletion examples/basic/src/pages/map/tilt/TiltPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Circle, Markers } from '@mapconductor/js-sdk-react';
import { ControlPanel, SliderControl } from '../../../components/ControlPanel';
import { MapViewContainer } from '../../../MapViewContainer';
import { useSampleI18n } from '../../../samples/i18n';
import { LoadingOverlay, useLoadingOverlay } from '../../../components/LoadingOverlay';

/**
* ほとんどのプロバイダは 60 度で頭打ちになるが、ArcGIS だけは 90 度近くまで実際に傾く。
Expand Down Expand Up @@ -167,13 +168,15 @@ function TiltContent({ mapViewState }: { mapViewState: MapViewStateInterface<Map
}

export function TiltPage() {
const loading = useLoadingOverlay();
const [mapViewState, setMapViewState] = useState<MapViewStateInterface<MapDesignTypeInterface<unknown>> | null>(null);
return (
<MapViewContainer initialCamera={INIT_CAMERA} onStateReady={setMapViewState}>
{ANCHOR_CIRCLES.map(circle => (
<Circle key={circle.id} state={circle} />
))}
<Markers states={TILT_MARKERS} />
<Markers states={TILT_MARKERS} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
{mapViewState && <TiltContent mapViewState={mapViewState} />}
</MapViewContainer>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Markers } from '@mapconductor/js-sdk-react';
import { ControlPanel } from '../../../components/ControlPanel';
import { MapViewContainer } from '../../../MapViewContainer';
import { useSampleI18n } from '../../../samples/i18n';
import { LoadingOverlay, useLoadingOverlay } from '../../../components/LoadingOverlay';

// android の AnimationPageViewModel.kt / ios の AnimationPageViewModel.swift と同一仕様:
// Drop / Bounce のラベルを持つ 2 マーカーを表示し、タップすると各マーカーに
Expand All @@ -24,10 +25,12 @@ const MARKERS = SPOTS.map(spot =>
);

export function MarkerAnimationPage() {
const loading = useLoadingOverlay();
const { t } = useSampleI18n();
return (
<MapViewContainer initialCamera={INIT_CAMERA}>
<Markers states={MARKERS} />
<Markers states={MARKERS} {...loading.bind('markers')} />
<LoadingOverlay visible={loading.active} />
<ControlPanel title={t(
{
en: 'Marker Animation',
Expand Down
Loading
Loading