Skip to content

Instantly share code, notes, and snippets.

@fswair
Last active July 30, 2023 19:16
Show Gist options
  • Save fswair/c7b4fc588ef8a555e2f8baedc345a742 to your computer and use it in GitHub Desktop.
Save fswair/c7b4fc588ef8a555e2f8baedc345a742 to your computer and use it in GitHub Desktop.
distance calculator api with locations
import geopy, haversine as hs
from fastapi import FastAPI
app = FastAPI()
@app.get("/get/locationDistance")
async def distanceCalc(loc1: str, loc2: str):
locator = geopy.Nominatim(user_agent="MacOS 13.5;")
location: geopy.Location = (locator.geocode(loc1))
location2: geopy.Location = (locator.geocode(loc2))
point1 = (location.latitude, location.longitude)
point2 = (location2.latitude, location2.longitude)
distance_unit = float(hs.haversine(point1, point2))
return dict(
distance=distance_unit,
details=dict(
point1=dict(
coords=(*point1,),
address=location.address
),
point2=dict(
coords=(*point2,),
address=location2.address,)
),
)
import subprocess
subprocess.run("\
pip install --upgrade pip &&\
pip install fastapi uvicorn geopy haversine &&\
uvicorn api:app --port 5000 --reload\
", shell=True)