Gmplot is one of the python libraries that allows us to add marker pins on google maps and create an HTML file locally. Marker pins can show anobject'ss location as its applications. Methods of gmplot's class named GooogleMapPlotter
can be used for adding marker pins.
We can add static, dynamic markers on google maps using the above library. In this Answer, we'll understand the methods that allow us to add markers on google maps.
Static markers need to be declared in the Python script. Marker pins can be draggable by setting the bool to true
. We use the marker
method to create markers on the maps. These static markers can be created as many as we want, but all need to be declared in Python script.
The following is the syntax of the marker method:
GoogleMapPlotter.marker(lati, longi, color='#FF0000', title=None, precision=6, label=None, draggable=False)
lati
: It is the latitude of the marker on the maps.
longi
: It is the longitude of the marker on the maps.
color
: It is a string that sets the color of the marker. By default, it is red.
title
: It is a string that allows us to set the title of the marker.
precision
: It is an integer that allows us to round off the coordinates to that digit.
label
: It is a string that gives a label to the marker.
draggable
: It is a boolean that enables the markers to be draggable.
lati
and longi
: They are required parameters, but other all are optional parameters.
The following example code will show the simple markers on google maps:
import gmplot gmap = gmplot.GoogleMapPlotter(31.51293675223902, 74.33353008126531,15) gmap.marker(31.51293675223902, 74.33353008126531, color='green', title='Cricket stadium') gmap.marker(31.512022090722684, 74.3308693297954, color='black', title='stadium', draggable=True) gmap.draw("maps.html")
Line 1: We import gmplot library to use its built-in packages.
Line 2: We pass the coordinates, zoomed to 15 in the constructor of the GoogleMapPlotter
, and stored it in the variable.
Lines 4–5: We call the marker()
method. We set the markers colors orange and black. We allow the black markers to be draggable by setting it true.
Line 7: We create the index.html file and store the google map in the file.
Dynamic markers need only a one-time call of the function in the Python script. The markers can be added to the maps by clicking on the location. All markers will have the same color markers. To have this functionality, we use enable_marker_dropping
methods in the script.
The syntax of the enable_marker_dropping method is the following:
GoogleMapPlotter.enable_marker_dropping(color, draggable=False)
color
: It is a string that sets the color of the markers.
draggable
: It is a boolean that allows the markers to be draggable. It is an optional parameter.
Let's look at the code below:
import gmplot gmap = gmplot.GoogleMapPlotter(31.513285464691442, 74.33348448433989,15) gmap.enable_marker_dropping('orange') gmap.draw("maps.html")
Line 1: We import gmplot
library to use its built-in packages.
Line 2: We pass the coordinates, zoomed to 15 in the constructor of the GoogleMapPlotter
, and stored it in tmarkers'ble.
Lines 4: We call the enable_marker_dropping()
method and set the markers' color to orange.
Line 6: We create the index.html
file and store the google map in the file.
Free Resources