How to add image overlay on Google maps using gmplot in Python
Google maps are used to show a person’s location or highlight areas in applications. We can also extract information from Google maps using web scraping.
We can manipulate Google maps by adding markers, circles, or even an image like a car or bike using Python. To perform these tasks, we need to use the gmplot library.
In this Answer, we'll learn how we can add images on top of Google maps using a third-person library called gmplot. We can add the image using gmplot.GoogleMapPlotter class’s ground_overlay() method.
Syntax
We can add images from the internet on top of maps using ground_overlay() method. The syntax of this method is s follows:
GoogleMapPlotter.ground_overlay(url, bounds, opacity=1.0)
url: It is the string that is the URL of the image. It cannot load images from the local storage.
bounds: They are bordering lines that confine an area. The “north” bound refers to the latitude of the top border, while “south” refers to the latitude of the bottom border. Similarly, “east” is the longitude of the right border and “west” is the longitude of the left border. Theboundsparameter is a dictionary whose keys arenorth,south,east, andwest.
opacity: It is an optional parameter that sets the opacity of the image.
Code example
Let's look at the code below:
import gmplot
gmap = gmplot.GoogleMapPlotter(31.513503879050337, 74.33410182086186, 18)
url='https://openclipart.org/download/247271/hombre-hello-remix-cyberscooty.svg'
bounds = {'north': 31.51358166525338, 'south': 31.513210019476748, 'east': 74.33334500058436, 'west': 74.33283133607337}
gmap.ground_overlay(url, bounds)
gmap.draw("index.html")
Code explanation
Line 1: We import gmplot library to use its built-in packages.
Line 2: We pass the latitude, longitude, and zoom level of 18 in the constructor
GoogleMapPlotter, and store it in the variable.Lines 4–6: We store the URL of the image in the
urlvariable.Line 6: We store the directional coordinates called the
boundsin a dictionary.Line 8: We pass the
urlandboundsdictionary to theground_overlaymethod.Line 10: This line creates the
index.htmlfile and store the Google maps in the file.
Free Resources