-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeocode.py
More file actions
29 lines (22 loc) · 729 Bytes
/
Copy pathgeocode.py
File metadata and controls
29 lines (22 loc) · 729 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
29
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 12 18:08:43 2019
@author: Bob
"""
from urllib.request import urlopen
import os
import ssl
import json
BASE_URL = 'https://maps.googleapis.com/'
KEY = os.environ.get('GOOGLE_API_KEY')
ctx = ssl.create_default_context()
def GeoCode(location, output_format='json', language=''):
location = location.replace(' ', '+')
URL_EXTENSION = 'maps/api/geocode/'
url = BASE_URL + URL_EXTENSION + output_format
url += '?address=' + location
url += '&key=' + KEY
data = json.loads(urlopen(url, context=ctx).read())
lat = data['results'][0]['geometry']['location']['lat']
long = data['results'][0]['geometry']['location']['lng']
return (lat, long)