Skip to content

Colota - Android GPS Location tracker

2.7 KB
Raw Blame
/**
 * Copyright (C) 2026 Max Dietrich
 * Licensed under the GNU AGPLv3. See LICENSE in the project root for details.
 */

import { elevation } from "../../../constants"
import React, { useMemo } from "react"
import { View, StyleSheet } from "react-native"
import { GeoJSONSource, Layer, Marker } from "@maplibre/maplibre-react-native"
import type { LocationCoords, ThemeColors } from "../../../types/global"
import { radius } from "@colota/shared"

/** meters-per-pixel at zoom 0 on the equator (Web Mercator constant) */
const METERS_PER_PX_Z0 = 156543.03

interface Props {
  coords: LocationCoords
  isPaused: boolean
  colors: ThemeColors
}

/**
 * Shared overlay for DashboardMap and GeofenceScreen:
 *  - accuracy circle (Layer type="circle" with zoom-based radius)
 *  - user position dot (Marker)
 */
export function UserLocationOverlay({ coords, isPaused, colors }: Props) {
  const markerColor = isPaused ? colors.textDisabled : colors.primary

  const radiusFactor = useMemo(() => {
    if (!coords.accuracy || coords.accuracy <= 0) return 0
    return coords.accuracy / (METERS_PER_PX_Z0 * Math.cos((coords.latitude * Math.PI) / 180))
  }, [coords.latitude, coords.accuracy])

  const accuracyGeoJSON = useMemo(() => {
    if (radiusFactor <= 0) return null
    return {
      type: "FeatureCollection" as const,
      features: [
        {
          type: "Feature" as const,
          properties: {},
          geometry: {
            type: "Point" as const,
            coordinates: [coords.longitude, coords.latitude]
          }
        }
      ]
    }
  }, [coords.latitude, coords.longitude, radiusFactor])

  const accuracyStyle = useMemo(
    () => ({
      circleRadius: ["interpolate", ["exponential", 2], ["zoom"], 0, radiusFactor, 24, radiusFactor * 16777216] as any,
      circleColor: colors.primary,
      circleOpacity: 0.12,
      circleStrokeColor: colors.primary,
      circleStrokeWidth: 1.5,
      circleStrokeOpacity: 0.4
    }),
    [radiusFactor, colors.primary]
  )

  const markerCoordinate = useMemo(
    () => [coords.longitude, coords.latitude] as [number, number],
    [coords.longitude, coords.latitude]
  )

  return (
    <>
      {accuracyGeoJSON && (
        <GeoJSONSource id="user-accuracy" data={accuracyGeoJSON}>
          <Layer id="user-accuracy-fill" type="circle" style={accuracyStyle} />
        </GeoJSONSource>
      )}

      <Marker lngLat={markerCoordinate} anchor="center">
        <View style={[styles.markerDot, { backgroundColor: markerColor }]} />
      </Marker>
    </>
  )
}

const styles = StyleSheet.create({
  markerDot: {
    width: 24,
    height: 24,
    borderRadius: radius.pill,
    borderWidth: 3,
    borderColor: "white",
    elevation: elevation.floating
  }
})