forked from x4nth055/pythoncode-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeolocator.py
More file actions
28 lines (24 loc) · 751 Bytes
/
geolocator.py
File metadata and controls
28 lines (24 loc) · 751 Bytes
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
from geopy.geocoders import Nominatim
import time
from pprint import pprint
# instantiate a new Nominatim client
app = Nominatim(user_agent="tutorial")
# get location raw data
location = app.geocode("Nairobi, Kenya").raw
# print raw data
pprint(location)
def get_location_by_address(address):
"""This function returns a location as raw from an address
will repeat until success"""
time.sleep(1)
try:
return app.geocode(address).raw
except:
return get_location_by_address(address)
address = "Makai Road, Masaki, Dar es Salaam, Tanzania"
location = get_location_by_address(address)
latitude = location["lat"]
longitude = location["lon"]
print(f"{latitude}, {longitude}")
# print all returned data
pprint(location)