Last active
July 30, 2023 19:16
-
-
Save fswair/c7b4fc588ef8a555e2f8baedc345a742 to your computer and use it in GitHub Desktop.
distance calculator api with locations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,) | |
), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
subprocess.run("\ | |
pip install --upgrade pip &&\ | |
pip install fastapi uvicorn geopy haversine &&\ | |
uvicorn api:app --port 5000 --reload\ | |
", shell=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example request url: http://127.0.0.1:5000/get/locationDistance?loc1=Velibaba%20mahallesi,%20Pendik,%20Istanbul,%20TURKEY%2034912&loc2=Yeni%C5%9Fehir%20mahallesi,%20Pendik,%20Istanbul,%20TURKEY%2034912