All files / src/services handleGeocode.ts

100% Statements 18/18
100% Branches 16/16
100% Functions 2/2
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49                                                11x 10x   10x 1x     9x 8x 7x 5x 4x 3x   2x               6x    
// src/services/geocoding.ts
import * as Location from "expo-location";
 
interface Coords {
  latitude: number;
  longitude: number;
}
 
/**
 * Reverse-geocodes a coordinate pair into the most human-readable
 * place name available.
 *
 * Resolution order:
 *   1. Named landmark / POI  (r.name)
 *   2. Street address        (r.streetNumber + r.street)
 *   3. Street only           (r.street)
 *   4. District / borough    (r.district)
 *   5. Sub-region            (r.subregion)
 *   6. City                  (r.city)
 *   7. Raw coords fallback   ("45.49700, -73.57900")
 *
 * Throws if expo-location itself throws (caller should handle).
 */
export async function reverseGeocode(coords: Coords): Promise<string> {
  const results = await Location.reverseGeocodeAsync(coords);
  const r = results[0];
 
  if (!r) {
    return formatCoords(coords);
  }
 
  if (r.name) return r.name;
  if (r.streetNumber && r.street) return `${r.streetNumber} ${r.street}`;
  if (r.street) return r.street;
  if (r.district) return r.district;
  if (r.subregion) return r.subregion;
  if (r.city) return r.city;
 
  return formatCoords(coords);
}
 
/**
 * Formats coordinates as a compact, readable fallback string.
 * e.g. "45.49700, -73.57900"
 */
export function formatCoords(coords: Coords): string {
  return `${coords.latitude.toFixed(5)}, ${coords.longitude.toFixed(5)}`;
}