Skip to content

Commit 505a772

Browse files
dfallingclaude
andcommitted
Cull offscreen markers to keep panning performant
Marker is a native View that reprojects every camera frame even when offscreen, so the accumulated set was growing unbounded. Filter to the last-known viewport bounds before rendering; cached pins outside the viewport stay in memory and reappear instantly on pan-back. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 5afe060 commit 505a772

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

src/map/MapScreen.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,18 @@ export function MapScreen() {
8585
});
8686
}, [data?.elements]);
8787

88-
const elements = useMemo(
89-
() => Array.from(elementsById.values()),
90-
[elementsById],
91-
);
88+
// Only render markers whose location falls inside the last-known viewport.
89+
// <Marker> is a native View — rendering offscreen ones still costs layout
90+
// and reprojection on every camera frame, so we cull client-side.
91+
const visibleElements = useMemo(() => {
92+
if (!bounds) return [];
93+
const {left, right, bottom, top} = bounds;
94+
return Array.from(elementsById.values()).filter(el => {
95+
if (!el.location) return false;
96+
const {longitude: lng, latitude: lat} = el.location;
97+
return lng >= left && lng <= right && lat >= bottom && lat <= top;
98+
});
99+
}, [elementsById, bounds]);
92100

93101
return (
94102
<View style={styles.container}>
@@ -98,7 +106,7 @@ export function MapScreen() {
98106
onRegionDidChange={onRegionDidChange}>
99107
<Camera ref={cameraRef} initialViewState={{center: [0, 20], zoom: 1}} />
100108
<UserLocation animated accuracy />
101-
{elements.map(el =>
109+
{visibleElements.map(el =>
102110
el.location ? (
103111
<Marker
104112
key={el.id}

0 commit comments

Comments
 (0)