In [10]:
from math import sin, cos, sqrt, atan2, radians
from geopy.geocoders import Nominatim
geolocator= Nominatim()



def cities(first, second):
    
    ###City 1
    location_one=geolocator.geocode(first) 
    loc_a=location_one.address
    
    lat1=location_one.latitude
    lon1=location_one.longitude
    
    print lat1,lon1
    
    #### City 2
    
    location_second=geolocator.geocode(second)
    loc_as=location_second.address
    
    lat2=location_second.latitude
    lon2=location_second.longitude
  
    print lat2,lon2
    
    dlon = lon2 - lon1
    dlat = lat2 - lat1
    a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
    print a
    c = 2 * atan2(sqrt(a), sqrt(1-a))
    print c
    distance = 6367 * c

    print("Result: %.2f" %distance)

In [11]:
cities("Jacksonville" , "Atlanta")


30.3321838 -81.655651
33.7490987 -84.3901849
0.67113785159
1.92013415178
Result: 12225.49

In [ ]:


In [12]:
from pygeocoder import Geocoder
import numpy as np
import sys

def get_distance(locA, locB):
    #use haversine forumla
    earth_rad = 6371.0
    print locA[0],locB[0]
    print locA[1],locB[1]
    
    dlat = np.deg2rad(locB[0] - locA[0])
    dlon = np.deg2rad(locB[1] - locA[1])
    a = np.sin(dlat/2) * np.sin(dlat/2) + \
        np.cos(np.deg2rad(locA[0])) * np.cos(np.deg2rad(locB[0])) * \
        np.sin(dlon/2) * np.sin(dlon/2) 
    print a
    c = 2 * np.arctan2(np.sqrt(a), np.sqrt(1-a))
    print c
    return earth_rad * c 

def get_latlongs(location):
    return Geocoder.geocode(location)[0].coordinates
        
def convert_km_to_miles(km):
    miles_per_km = 0.621371192
    return km * miles_per_km
    
def main():
    #get first city
    print 'Type the first City: '
    cityA = raw_input()
    
    #get second city
    print 'Type the second city: '
    cityB = raw_input()
    
    #get units
    units = ''
    while (units != 'km') & (units != 'm'):
        print 'Type distance units (miles or kilometers): '
        units = str.lower(raw_input())
        if units in ['clicks', 'km', 'kilometers', 'kilometer']:
            units = 'km'
        elif units in ['m', 'mile', 'miles']:
            units = 'm'
        else:
            print 'units not recognised, please try again'
            
    #find the distance in km
    try:
        distance = get_distance(get_latlongs(cityA),
                                get_latlongs(cityB))
        #display the distance
        if units == 'km':
            print str(distance),' km'   
        else:
            distance = convert_km_to_miles(distance)
            print str(distance), ' miles' 
            
    except:
        print 'Error raised.  Are the input cities correct?'
        
            
if __name__ == '__main__':
    sys.exit(main())


Type the first City: 
Jacksonville
Type the second city: 
Atlanta
Type distance units (miles or kilometers): 
km
30.3321838 33.7489954
-81.655651 -84.3879824
0.00129674972699
0.0720363973259
458.943887364  km
An exception has occurred, use %tb to see the full traceback.

SystemExit

In [ ]: