Sherry245's picture
Initial Deploy
b2f8c01
Raw History Blame Contribute Delete
770 Bytes
import math
from app.schemas.rides import Coordinate
async def google_directions(pickup: Coordinate, dropoff: Coordinate) -> tuple[float, float]:
# TODO: replace with real Google Directions API call (needs httpx + GOOGLE_MAPS_KEY).
distance_km = _haversine_km(pickup.lat, pickup.lng, dropoff.lat, dropoff.lng)
duration_min = (distance_km / 30.0) * 60.0
return round(distance_km, 2), round(duration_min, 1)
def _haversine_km(lat1: float, lng1: float, lat2: float, lng2: float) -> float:
R = 6371.0
p1, p2 = math.radians(lat1), math.radians(lat2)
dp = math.radians(lat2 - lat1)
dl = math.radians(lng2 - lng1)
a = math.sin(dp / 2) ** 2 + math.cos(p1) * math.cos(p2) * math.sin(dl / 2) ** 2
return 2 * R * math.asin(math.sqrt(a))