Skip to content

Colota - Android GPS Location tracker

fix(map): use a fixed zoom for a single point so the map doesn't overzoom (#523)

Max Dietrich5b24b725dee2
diff --git a/apps/mobile/src/components/features/inspector/TrackMap.tsx b/apps/mobile/src/components/features/inspector/TrackMap.tsx
index 697b5bb0..a75ed742 100644
--- a/apps/mobile/src/components/features/inspector/TrackMap.tsx
+++ b/apps/mobile/src/components/features/inspector/TrackMap.tsx
@@ -21,7 +21,7 @@ import {
type TrackLocation
} from "../map/mapUtils"
import { getSpeedUnit } from "../../../utils/geo"
-import { HIT_SLOP_MD, MAP_ANIMATION_DURATION_MS } from "../../../constants"
+import { DEFAULT_MAP_ZOOM, HIT_SLOP_MD, MAP_ANIMATION_DURATION_MS } from "../../../constants"
const HAS_NOTE = ["!=", ["get", "note"], ""]
const trackPointStyle: any = {
@@ -73,12 +73,17 @@ export function TrackMap({ locations, colors, trips, trackColor, fitVersion, onP
if (fitVersion === fittedVersionRef.current) return
// Defer to next frame so the map's GL context is fully ready after onDidFinishLoadingMap
requestAnimationFrame(() => {
- if (!mapRef.current?.camera) return
+ const camera = mapRef.current?.camera
+ if (!camera) return
fittedVersionRef.current = fitVersion ?? 0
- mapRef.current.camera.fitBounds([bounds.sw[0], bounds.sw[1], bounds.ne[0], bounds.ne[1]], {
- padding: { top: 60, right: 60, bottom: 60, left: 60 },
- duration: MAP_ANIMATION_DURATION_MS
- })
+ if (bounds.sw[0] === bounds.ne[0] && bounds.sw[1] === bounds.ne[1]) {
+ camera.setStop({ center: bounds.sw, zoom: DEFAULT_MAP_ZOOM, duration: MAP_ANIMATION_DURATION_MS })
+ } else {
+ camera.fitBounds([bounds.sw[0], bounds.sw[1], bounds.ne[0], bounds.ne[1]], {
+ padding: { top: 60, right: 60, bottom: 60, left: 60 },
+ duration: MAP_ANIMATION_DURATION_MS
+ })
+ }
})
}, [bounds, mapReady, fitVersion])
diff --git a/apps/mobile/src/components/features/inspector/__tests__/TrackMap.test.tsx b/apps/mobile/src/components/features/inspector/__tests__/TrackMap.test.tsx
index 0323e349..01cf9421 100644
--- a/apps/mobile/src/components/features/inspector/__tests__/TrackMap.test.tsx
+++ b/apps/mobile/src/components/features/inspector/__tests__/TrackMap.test.tsx
@@ -1,8 +1,10 @@
import React from "react"
import { render, act } from "@testing-library/react-native"
import { TrackMap } from "../TrackMap"
+import { DEFAULT_MAP_ZOOM } from "../../../../constants"
const mockFitBounds = jest.fn()
+const mockSetStop = jest.fn()
jest.mock("../../map/ColotaMapView", () => {
const R = require("react")
@@ -12,7 +14,7 @@ jest.mock("../../map/ColotaMapView", () => {
ColotaMapView: R.forwardRef(function MockColotaMapView(props: any, ref: any) {
const onMapReady = props.onMapReady
R.useImperativeHandle(ref, () => ({
- camera: { fitBounds: mockFitBounds },
+ camera: { fitBounds: mockFitBounds, setStop: mockSetStop },
mapView: null
}))
R.useEffect(() => {
@@ -78,6 +80,7 @@ const loc = (lat: number, lon: number) => ({
describe("TrackMap auto-fit", () => {
beforeEach(() => {
mockFitBounds.mockClear()
+ mockSetStop.mockClear()
jest.spyOn(globalThis, "requestAnimationFrame").mockImplementation((cb: any) => {
cb(0)
return 0
@@ -107,4 +110,12 @@ describe("TrackMap auto-fit", () => {
})
expect(mockFitBounds).toHaveBeenCalledTimes(2)
})
+
+ it("centers a single point at a fixed zoom instead of fitting bounds", () => {
+ render(<TrackMap locations={[loc(52.5, 13.4)]} colors={colors} trackColor="#000" fitVersion={1} />)
+
+ // A zero-extent bounds would make fitBounds zoom to the max level, so a lone point must not fit
+ expect(mockFitBounds).not.toHaveBeenCalled()
+ expect(mockSetStop).toHaveBeenCalledWith(expect.objectContaining({ center: [13.4, 52.5], zoom: DEFAULT_MAP_ZOOM }))
+ })
})