Drawing Geographic Locations on Google Maps using Python #
There are different ways to draw geographic coordinates (latitudes and longitudes) on Google Maps. You can do it using Darrin Ward’s online Map Tool. However, if you want to save it in a local file, you would need another way to do it. One of the ways is to use a Python module called gmplot. It uses Google’s geocoding service and generates a Javascript-based HTML file that renders all geolocation data on top of Google Maps. The gmplot
module supports location-based map initialization and zoom-level initialization, among other things.
I tested gmplot
with Python 3. The geographic data was taken from Yelp dataset. The geographic coordinates are locations of businesses. I fetched the data from a PostgreSQL database. You can populate the data in other ways too.
But you need to understand how gmplot
handles the data. The latitudes and longitudes fed into gmplot
are transformed into two tuples using zip()
function. Meaning the latitude and longitude values must be in iterable format.
With this information in mind we can now proceed with the process. First, we need to install gmplot
module in Python. Since I was using python3
, I installed the module through pip3
.
$ sudo pip3 install gmplot
If you haven’t used Postgres with Python in your machine before, you also need to install the psycopg2
module.
$ sudo pip3 install psycopg2
The script to draw the locations as points on Google Maps goes like the following.
import psycopg2
import gmplot
# Connect to the database
db_conn = psycopg2.connect("dbname='database_name' host='host_address' user='username' password='your_password'")
#Set the cursor
cur = db_conn.cursor()
# Execute the database query. I am fetching business locations in a particular zip.
cur.execute("select latitude, longitude from business where postal_code='89109';")
# Fetch all the data returned by the database query as a list
lat_long = cur.fetchall()
# Initialize two empty lists to hold the latitude and longitude values
latitude = []
longitude = []
# Transform the the fetched latitude and longitude data into two separate lists
for i in range(len(lat_long)):
latitude.append(lat_long[i][0])
longitude.append(lat_long[i][1])
# Initialize the map to the first location in the list
gmap = gmplot.GoogleMapPlotter(latitude[0],longitude[0])
# Draw the points on the map. I created my own marker for '#FF66666'.
# You can use other markers from the available list of markers.
# Another option is to place your own marker in the folder -
# /usr/local/lib/python3.5/dist-packages/gmplot/markers/
gmap.scatter(latitude, longitude, '#FF6666', edge_width=10)
# Write the map in an HTML file
gmap.draw('map.html')
# Close the cursor and the database connection
cur.close()
db_conn.close()
One thing I noticed from the drawn locations is that the points are slightly off. I believe, this has nothing to do with gmplot
, because drawing the points in Darrin Ward’s tool showed the same inconsistencies in location data.