// Turning coordinates back into something a person recognises. // // Shared by the initial location fix and the map pin adjuster, so the address // a rider sees while dragging the pin is formatted exactly like the one that // was filled in for them automatically — two different shapes for the same // place would read as a bug. // // Uses expo-location's on-device geocoder rather than the Places API: it costs // nothing, works without the Google key, and this is a label, not a search. import * as Location from "expo-location"; import { tr } from "@/lib/i18n"; /** * A short, human address for a point — "Hamra, Beirut" — or the generic "your * location" label when the geocoder has nothing useful. Never throws: a failed * lookup costs the label, never the coordinates. */ export const addressForCoords = async ( latitude: number, longitude: number, ): Promise => { try { const [place] = await Location.reverseGeocodeAsync({ latitude, longitude }); if (!place) return tr("common.yourLocation"); // Street-level first, falling back through progressively coarser fields: // a pin dropped in the middle of a field still deserves a name. const line = [ place.name ?? place.street, place.district ?? place.city ?? place.subregion, place.region, ] .filter(Boolean) // The geocoder often repeats a value across fields ("Beirut, Beirut"). .filter((part, index, all) => all.indexOf(part) === index) .slice(0, 2) .join(", "); return line || tr("common.yourLocation"); } catch (error) { console.log("[REVERSE_GEOCODE]: ", error); return tr("common.yourLocation"); } };