Python script to find the distance between two cities using opencage and geopy:

def find_distance(A,B):
    key = 'YourAPIkey'  # get api key from:  https://opencagedata.com
    geocoder = OpenCageGeocode(key)

    result_A = geocoder.geocode(A)
    lat_A = result_A[0]['geometry']['lat']
    lng_A = result_A[0]['geometry']['lng']

    result_B = geocoder.geocode(B)
    lat_B = result_B[0]['geometry']['lat']
    lng_B = result_B[0]['geometry']['lng']  

    return (geodesic((lat_A,lng_A), (lat_B,lng_B)).kilometers)

Built With

Share this project:

Updates