Connect to google maps to get an address

pull/4/head
Václav Dekanovský 2020-07-16 15:44:54 +02:00
commit d1b8171608
1 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,141 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"import json\n",
"import urllib"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# https://developers.google.com/maps/documentation/geocoding/intro\n",
"base_url= \"https://maps.googleapis.com/maps/api/geocode/json?\"\n",
"AUTH_KEY = \"Your Key\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# set up your search parameters - address and API key\n",
"parameters = {\"address\": \"Tuscany, Italy\",\n",
" \"key\": AUTH_KEY}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"https://maps.googleapis.com/maps/api/geocode/json?address=Tuscany%2C+Italy&key=AIzaSyDH9LtdH2ujWvxt9FV2F6NweXw6Z6UhnLE\n"
]
}
],
"source": [
"# urllib.parse.urlencode turns parameters into url\n",
"print(f\"{base_url}{urllib.parse.urlencode(parameters)}\")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"r = requests.get(f\"{base_url}{urllib.parse.urlencode(parameters)}\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'results': [{'address_components': [{'long_name': 'Tuscany',\n",
" 'short_name': 'Tuscany',\n",
" 'types': ['administrative_area_level_1', 'political']},\n",
" {'long_name': 'Italy',\n",
" 'short_name': 'IT',\n",
" 'types': ['country', 'political']}],\n",
" 'formatted_address': 'Tuscany, Italy',\n",
" 'geometry': {'bounds': {'northeast': {'lat': 44.4726899, 'lng': 12.3713555},\n",
" 'southwest': {'lat': 42.2376686, 'lng': 9.6867213}},\n",
" 'location': {'lat': 43.7710513, 'lng': 11.2486208},\n",
" 'location_type': 'APPROXIMATE',\n",
" 'viewport': {'northeast': {'lat': 44.4726899, 'lng': 12.3713555},\n",
" 'southwest': {'lat': 42.2376686, 'lng': 9.6867213}}},\n",
" 'place_id': 'ChIJezSAEFMr1BIRq1kgW7rDxro',\n",
" 'types': ['administrative_area_level_1', 'political']}],\n",
" 'status': 'OK'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = json.loads(r.content)\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'lat': 43.7710513, 'lng': 11.2486208}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.get(\"results\")[0].get(\"geometry\").get(\"location\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}