Spaces:
Sleeping
Sleeping
File size: 1,086 Bytes
f4bf832 | 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 | import os
import googlemaps
_client: googlemaps.Client | None = None
def get_client() -> googlemaps.Client:
global _client
if _client is None:
api_key = os.getenv("GOOGLE_MAPS_API_KEY")
if not api_key:
raise RuntimeError("GOOGLE_MAPS_API_KEY environment variable is not set")
_client = googlemaps.Client(key=api_key)
return _client
def search_nearby(
lat: float,
lng: float,
keyword: str,
radius: int = 5000,
) -> list[dict]:
client = get_client()
result = client.places_nearby(
location=(lat, lng),
radius=radius,
keyword=keyword,
language="ko",
)
places = []
for place in result.get("results", [])[:5]:
loc = place["geometry"]["location"]
places.append(
{
"name": place.get("name", ""),
"lat": loc["lat"],
"lng": loc["lng"],
"address": place.get("vicinity", ""),
"phone": place.get("formatted_phone_number", ""),
}
)
return places
|