Plotting Google Map using gmplot package in Python?


There are numerous ways you can draw geographical coordinates on Google Maps. However, in case you want to save it in a local file, one better way to accomplish is through a python module called gmplot.

Python library gmplot allows us to plot data on google maps. gmplot has a matplotlib-like interface to generate the HTML and javascript to deliver all the additional data on top of Google Maps.

Installation

It is easy to install gmplot using pip incase gmplot is not already installed −

pip install gmplot

On running above command, you may see output something like −

From above, we can see the latest gmplot-1.2.0 version is installed onto our computer.

gmplot library has several plotting methods to create exploratory maps views very simple. Gmplot is very flexible to create google map as we can use it to generate html directly.

Below are different ways to accomplish it −

Case1 − To create the base map using gmplot

If you want to place the map to a particular location you need to write the latitude-longitude value of that location and the zoom resolution.

# Import gmplot library.
from gmplot import *
# Place map
# First two arugments are the geogrphical coordinates .i.e. Latitude and Longitude
#and the zoom resolution.
gmap = gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Location where you want to save your file.
gmap.draw( "C:\Users\rajesh\Desktop\map11.html" )

Output1

Note − Above screen display we see this because Google Maps service is not free now in case you are accessing through an API. You need to add your API_KEY to see a better google map view. Below is the code to accomplish this −

Case 1 (with GOOGLE_API_KEY added)

To create the base map using gmplot

# Import gmplot library.
from gmplot import *
# Place map
# First two arugments are the geogrphical coordinates .i.e. Latitude and Longitude
#and the zoom resolution.
gmap=gmplot.GoogleMapPlotter(17.438139, 78.39583, 18)
# Because google maps is not a free service now, you need to get an api key. Else commenting
# below line you will see the maps with "For Development Purpose Only" on the screen and maps
# with low resolution.
#gmap.apikey = "Your_API_KEY"
gmap.apikey = "AIzaSyDeRNMnZ__VnQDiATiuz4kPjF_c9r1kWe8"
# Location where you want to save your file.
gmap.draw( "C:\Users\rajesh\Desktop\map11.html" )

Note − You need to add google maps API key(‘Your_API_KEY’) & set it equals to gmap.apikey. Below output is coming because I use my own key, which you can also get from the below link −

https://developers.google.com/maps/documentation/embed/get-api-key

Output 1

Case 2 − Drawing a polygon on google map

# import gmplot package
import gmplot
latitude_list = [ 17.4567417, 17.5587901, 17.6245545]
longitude_list = [ 78.2913637, 78.007699, 77.9266135 ]
gmap = gmplot.GoogleMapPlotter(17.438139, 78.3936413, 11)
gmap.scatter( latitude_list, longitude_list, '# FF0000', size = 40, marker = False)
# polygon method Draw a polygon with
# the help of coordinates
gmap.polygon(latitude_list, longitude_list, color = 'cornflowerblue')
gmap.apikey = "Your_API_KEY"
gmap.draw( "C:\Users\rajesh\Desktop\map3.html" )

Output 2

Case 3 − Scatter points on google map and draw a line in between given coordinates.

# import gmplot package
import gmplot
#Set different latitude and longitude points
Charminar_top_attraction_lats, Charminar_top_attraction_lons = zip(*[
   (17.3833, 78.4011),(17.4239, 78.4738),(17.3713, 78.4804),(17.3616, 78.4747),
   (17.3578, 78.4717),(17.3604, 78.4736),(17.2543, 78.6808),(17.4062, 78.4691),
   (17.3950, 78.3968),(17.3587, 78.2988),(17.4156, 78.4750)])
#declare the center of the map, and how much we want the map zoomed in
gmap3 = gmplot.GoogleMapPlotter(17.3616, 78.4747, 13)
# Scatter map
gmap3.scatter( Charminar_top_attraction_lats, Charminar_top_attraction_lons, '#FF0000',size = 50, marker = False )
# Plot method Draw a line in between given coordinates
gmap3.plot(Charminar_top_attraction_lats, Charminar_top_attraction_lons, 'cornflowerblue', edge_width = 3.0)
#Your Google_API_Key
gmap.apikey = " API_Key”
# save it to html
gmap3.draw(r"c:\users\rajesh\desktop\maps\scatter.html")

Output 3

Case 4: Heatmap and scatter in one graph to represent the earthquakes.

#Import important libraries
import gmplot
import numpy as np
# generate 700 random lats and lons
latitude = (np.random.random_sample(size = 700) - 0.5) * 180
longitude = (np.random.random_sample(size = 700) - 0.5) * 360
# declare the center of the map, and how much we want the map zoomed in
gmap = gmplot.GoogleMapPlotter(0, 0, 2)
# plot heatmap
gmap.heatmap(latitude, longitude)
gmap.scatter(latitude, longitude, c='r', marker=True)
#Your Google_API_Key
gmap.apikey = " Your_Google_API_Key "
# save it to html
gmap.draw(r"c:\users\rajesh\desktop\maps\country_heatmap.html")

Output

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements