Permalink
Browse files

Switch to Google Places API

  • Loading branch information...
1 parent f6822ac commit 99e94f9091a17848fad0650baa2f36b7d27ebbb3 @csev committed Dec 11, 2016
Showing with 27 additions and 22 deletions.
  1. +7 −11 code3/geodata/README.txt
  2. +12 −10 code3/geodata/geoload.py
  3. +8 −1 tools/python-data/data/geojson.php
@@ -1,4 +1,4 @@
-Using the Google Geocoding API with a Database and
+Using the Google Places API with a Database and
Visualizing data on Google Map
In this project, we are using the Google geocoding API
@@ -40,23 +40,19 @@ in the database and if we don't have the data for the location,
call the geocoding API to retrieve the data and store it in
the database.
-As of December 2016, the Google Geocoding API requires an API
-key. To complete this assignment, you can use a subset of that data
+As of December 2016, the Google Geocoding APIs require an API
+key. To complete this assignment without a Google account,
+without an API key, or from a country that blocks
+access to Google, you can use a subset of that data
which is available at:
http://python-data.dr-chuck.net/geojson
+To use this, simply leave the api_key set to False.
+
This URL only has a subset of the data but it has no rate limit so
it is good for testing.
-For example:
-
-http://python-data.dr-chuck.net/geojson?address=Northeastern+University
-
-If you get an API from Google, you can use the original URL:
-
-http://maps.googleapis.com/maps/api/geocode/json?address=Monash+University
-
Here is a sample run after there is already some data in the
database:
@@ -5,16 +5,14 @@
import ssl
import sys
-# Google API (requires API key)
-# serviceurl = "http://maps.googleapis.com/maps/api/geocode/json?"
-# If you are in China use this URL:
-# serviceurl = "http://maps.google.cn/maps/api/geocode/json?"
+api_key = False
+# If you have a Google Places API key, enter it here
+# api_key = 'AIzaSy___IDByT70'
-serviceurl = "http://python-data.dr-chuck.net/geojson?"
-
-# Deal with SSL certificate anomalies Python > 2.7
-# scontext = ssl.SSLContext(ssl.PROTOCOL_TLSv1)
-scontext = None
+if api_key is False:
+ serviceurl = "http://py4e-data.dr-chuck.net/geojson?"
+else :
+ serviceurl = "https://maps.googleapis.com/maps/api/place/textsearch/json?"
conn = sqlite3.connect('geodata.sqlite')
cur = conn.cursor()
@@ -41,12 +39,16 @@
pass
print('Resolving', address)
- url = serviceurl + urllib.parse.urlencode({"address": address})
+ parms = {"query": address}
+ if api_key is not False: parms['key'] = api_key
+ url = serviceurl + urllib.parse.urlencode(parms)
+
print('Retrieving', url)
uh = urllib.request.urlopen(url)
data = uh.read().decode()
print('Retrieved', len(data), 'characters', data[:20].replace('\n', ' '))
count = count + 1
+
try:
js = json.loads(data)
except:
@@ -6,7 +6,14 @@
}
require_once("locations.php");
-$address = isset($_GET['address']) ? $_GET['address'] : false;
+$address = false;
+if ( isset($_GET['address']) ) {
+ $address = $_GET['address'];
+}
+if ( isset($_GET['query']) ) {
+ $address = $_GET['query'];
+}
+
header('Content-Type: application/json; charset=utf-8');
if ( $address === false ) {

0 comments on commit 99e94f9

Please sign in to comment.