04-02-2023, 09:34 AM
I Created a Python Program to Visualize Strings on Google Maps
Rate this post
No, ChatGPT really doesn’t help with this (yet…)
but it did help me write the code snippet explanation (which I attached after each code output).
In my example, we will take a special route in Budapest, using one of the easiest to use library,.
We can create our maps directly in HTML code using its many methods.
To access some functions, you need a Google Maps API key.
I used Python’s built-in function to create the Lat – Lon coordinates, you can learn more about here:
Recommended: Python Built-in Function
We can work with the following methods of the object, which I will describe in detail:
GoogleMapPlotter
Let’s go through the functions using “code snippets”:
Find a location by name (from_geocode)
With the method of the object, we can display a given address and its neighbourhood on the map.
Parameters of the method: (str), (int), (str)
After creating your map, you have two options, either save the HTML file () or store it as a string (with ). I use the method, where the only parameter is the file to create and its path.
Result:
![[Image: image-2-1024x714.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-2-1024x714.png)
The code imports the library, which is a Python wrapper for the Google Maps API. It then creates a new object named for the location “Budapest” using the method. This method uses the Google Maps API to retrieve the latitude and longitude of the location, which is necessary to display the map.
Finally, the method is called on the object to generate and save the map as an HTML file named .
Coordinates of a location (geocode)
If you want to know the coordinates of a location, use. As an input parameter, pass the name (str) of the place you are looking for and your API key. This returns a tuple of the lat/long coordinates of the given location (float, float).
Result:
The code calls the method on the object to obtain the latitude and longitude of a location specified as a string. In this case, the location is “Budapest, Hungary”. The parameter is also passed to this method to authenticate the Google Maps API.
Text on your map (text)
If you want to place custom text on your map, you can do it with, using the text label’s Latitude and Longitude parameter.
It is possible to color the text with the parameter, which can be the color name (), hexadecimal (), or matplotlib-like ().
Result:
![[Image: image-3.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-3.png)
Drop marker (marker)
Show markers. The required parameters are, of course, the Latitude and Longitude coordinates (float, float), and additional optional parameters can be used to customize the markers:
gmap.enable_marker_dropping(color=’black’)
allows markers to be dropped onto the map when clicked. Clicking on a dropped marker will delete it.
Note: Calling this function multiple times will just overwrite the existing dropped marker settings.
Parameters:
Result:
![[Image: image-4.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-4.png)
The code adds a marker to the Google Maps plot. The marker is placed at the latitude and longitude coordinates (47.51503432784726, 19.005350430919034) and is labeled.
The parameter sets the information displayed when the user clicks on the marker. In this case, it is a link to the Finxter Academy website.
The parameter is set to , meaning that the user cannot move the marker.
The fourth line enables marker dropping, meaning the user can add new markers to the plot by clicking on the map.
The final line saves the plot to an HTML file named.
Route planning (directions)
Using the Directions API, you can display route planning between any points. The origin and destination coordinates are given as parameters (float, float). Optionally, the as list of tuples and the as str can also be specified. The travel modes are:
Result:
![[Image: image-5.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-5.png)
The fourth line adds a route to the plot using the method.
The starting point of the route is at latitude 47.5194613766804 and longitude 19.000656008676216, and the ending point is at latitude 47.520243896650946 and longitude 19.00204002854648.
The parameter is set to a list containing one set of latitude and longitude coordinates (47.520888742275, 18.99871408933636).
The fifth line adds another route to the plot, starting at the same point as the previous route and ending at the same point as the previous route, but with different waypoints.
The sixth line adds a third route to the plot, with a starting point at latitude 47.52226897515179 and longitude 19.00018393988221, an ending point at latitude 47.520243896650946 and longitude 19.00204002854648, and a set of waypoints containing one set of latitude and longitude coordinates (47.52088149688948, 19.002871513347902).
Display many points (scatter)
The allows you to place many points at once. In addition to the necessary (float) and (float) parameters, the following optional parameters are:
Result:
The fourth line defines a list of latitude and longitude coordinates as tuples, representing the locations of individual letters of the word.
![[Image: image-6.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-6.png)
The fifth line uses the method of adding the letters to the plot as points. The method takes the latitude and longitude coordinates as separate arguments using the unpacking operator ().
The parameter is set to a list of colors that correspond to the letters. The parameter specifies the size of the points, the parameter specifies the width of the edge around the points, and the and parameters specify the title and label of each point, respectively.
Draw circle (circle)
Sometimes it is useful to draw a circle. In addition to specifying the center, , and of the circle, you can also to specify the following:
Result:
![[Image: image-7.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-7.png)
The fourth line uses the method to add a circle to the plot.
The method takes the latitude and longitude coordinates of the center of the circle as its first two arguments, followed by the radius of the circle in meters.
The parameter specifies the transparency of the circle fill, while the and parameters specify the color of the circle edge and fill, respectively.
Polyline (plot)
A polyline is a line composed of one or more sections. If we want to display such a line on our map, we use the method. In addition to the usual [float], [float] parameters, you can specify the following optional parameters:
Result:
![[Image: image-8.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-8.png)
This line creates a list of latitude-longitude pairs that define the vertices of a polygon. The function is used to transpose the list so that each pair of latitude-longitude values becomes a separate tuple.
This line plots the polygon on the Google Map. The function takes the argument, which is unpacked as separate arguments, representing the latitude and longitude values of the polygon vertices. The parameter sets the width of the polygon edges in pixels and the parameter sets the color of the edges.
Create heatmap (heatmap)
Plot a heatmap.
Parameters:
Optional Parameters:
Result:
![[Image: image-9.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-9.png)
First, a list of tuples called is created. Each tuple contains two values representing latitude and longitude coordinates of a point on the map.
Then, an instance of the class is created with a specified center point, zoom level, and an API key.
Next, the method of the object is called, passing in the list as positional arguments, along with other parameters.
The parameter determines the radius of each data point’s influence on the heatmap, while the parameter determines the intensity of each data point’s contribution to the heatmap.
The parameter is a list of tuples representing the color gradient of the heatmap, with each tuple containing four values representing red, green, blue, and alpha values.
Finally, the parameter determines the transparency of the heatmap.
Picture above the map (ground_overlay)
Overlay an image from a given URL onto the map.
Parameters:
Optional Parameters:
Result:
![[Image: image-10.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-10.png)
The variable contains the URL of an image that will be used as the ground overlay. The dictionary defines the north, south, east, and west coordinates of the image on the map.
Finally, the method is called on the object, passing the URL and bounds variables as arguments. The opacity parameter is set to 0.3 to make the overlay partially transparent. The resulting map is saved to a file called using the method.
Plot a Polygon
Parameters:
Optional Parameters:
Result:
![[Image: image-11.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-11.png)
Defines a set of coordinates for a polygon named.
Calls the method with the argument to draw the polygon on the map. The , , , and parameters define the appearance of the polygon.
Saves the map as an HTML file named using the method.
Display grid (grid)
The parameters are used to specify the grid start and end points and the width and length of the grid.
Result:
![[Image: image-12.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-12.png)
This code generates a Google Map centered at latitude 47.519350864380385 and longitude 19.010462124312387, with a zoom level of 14. It then adds a grid to the map with vertical lines spaced 0.0025 degrees apart between longitude 19.0 and 19.05, and horizontal lines spaced 0.0025 degrees apart between latitude 47.50 and 47.53. Finally, it saves the resulting map with the grid to an HTML file named.
Let’s put it together and see where I found “finxter”!
Import module, create instance
Define letter direction routes
Define letters routes
Plot the letters
Create text on map:
Drop a marker with finxter link, enable marker dropping:
Define and plot scatter points for letters:
Create heatmap:
Overlay image from URL:
Draw polygon:
Draw map to file:
Output:
![[Image: image-13.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-13.png)
Conclusion
Congratulations, now you’ve learned how to draw almost anything on Google Maps with a few lines of code. While is a powerful library, it has some limitations (e.g., I can’t figure out how to change the color of the path), so maybe other modules like are a good place to learn more.
A Few Final Words on gmplot
is a Python library that allows the user to plot data on Google Maps. It provides a simple interface to create various types of maps, including scatterplots, heatmaps, ground overlays, and polygons.
With, the user can add markers, lines, and shapes to the map, customize colors, labels, and other properties, and export the map to a static HTML file.
The library uses the Google Maps API and requires an API key to be able to use it. is a useful tool for visualizing geospatial data and creating interactive maps for data exploration and analysis.
Rate this post
No, ChatGPT really doesn’t help with this (yet…)
but it did help me write the code snippet explanation (which I attached after each code output).In my example, we will take a special route in Budapest, using one of the easiest to use library,
Code:
gmplotWe can create our maps directly in HTML code using its many methods.
To access some functions, you need a Google Maps API key.
I used Python’s built-in
Code:
zipCode:
zip
Recommended: Python Code:
zip()We can work with the following methods of the
Code:
GoogleMapPlotterGoogleMapPlotter
- Code:
from_geocode(location, zoom=10, apikey='')
- Code:
draw(path_file)
- Code:
get()
- Code:
geocode(location, apikey='')
- Code:
text(lat, lon, text, color='red')
- Code:
marker(lat, lon, color='#FF0000', title=None, precision=6, label=None, **kwargs)
- Code:
enable_marker_dropping(color, **kwargs)
- Code:
directions
- Code:
scatter
- Code:
circle
- Code:
plot
- Code:
heatmap
- Code:
ground_overlay
- Code:
polygon
- Code:
grid
Let’s go through the functions using “code snippets”:
Find a location by name (from_geocode)
With the
Code:
geocodeCode:
GoogleMapPlotterParameters of the method:
Code:
locationCode:
zoomCode:
apikeyAfter creating your map, you have two options, either save the HTML file (
Code:
.draw()Code:
.get()Code:
.draw()Code:
import gmplot
apikey = '' # (your API key here)
gmap = gmplot.GoogleMapPlotter.from_geocode('Budapest', apikey=apikey)
gmap.draw("budapest_map.html")Result:
![[Image: image-2-1024x714.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-2-1024x714.png)
The code imports the
Code:
gmplotCode:
GoogleMapPlotterCode:
gmapCode:
from_geocode()Finally, the
Code:
draw()Code:
gmapCode:
budapest_map.htmlCoordinates of a location (geocode)
If you want to know the coordinates of a location, use
Code:
.geocode()Code:
import gmplot
apikey = '' # (your API key here)
location = gmplot.GoogleMapPlotter.geocode('Budapest, Hungary', apikey=apikey)
print(location)Result:
Code:
(47.497912, 19.040235)The code calls the
Code:
geocode()Code:
GoogleMapPlotterCode:
apikeyText on your map (text)
If you want to place custom text on your map, you can do it with
Code:
.text()It is possible to color the text with the
Code:
color=strCode:
'black'Code:
'#000000'Code:
'k'Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 17, apikey=apikey)
finxter_text = ['f', 'i', 'n', 'x', 't', 'e', 'r']
colors = ['limegreen', 'cyan', 'gold','orange', 'limegreen', 'cyan', 'orange']
j = 0
lat = 47.529266431577625
lng = 19.00500303401821
for i in finxter_text: gmap.text(lat, lng, i, color = colors[j]) j += 1 lng += 0.001Result:
![[Image: image-3.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-3.png)
Drop marker (marker)
Show markers. The required parameters are, of course, the Latitude and Longitude coordinates (float, float), and additional optional parameters can be used to customize the markers:
- (str) which can be the name of the color (Code:
color), hexadecimal (Code:'black'), or matplotlib-like (Code:'#000000')Code:'k'
- (str) : Hover-over title of the marker.Code:
title
- (int) : Number of digits after the decimal to round to for lat/long values. Defaults to 6.Code:
precision
- (str) : Label displayed on the marker.Code:
label
- (str) : HTML content to be displayed in a pop-up info window.Code:
info_window
- (bool) : Whether or not the marker is draggable.Code:
draggable
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap.marker(47.51503432784726, 19.005350430919034, label = 'finxter', info_window = "<a href='https://finxter.com/'>The finxter Academy</a>", draggable = False)Code:
gmap.enable_marker_dropping()
Note: Calling this function multiple times will just overwrite the existing dropped marker settings.Parameters:
- str: Color of the markers to be dropped.Code:
color
- str: Hover-over title of the markers to be dropped.Code:
title
- str: Label displayed on the markers to be dropped.Code:
label
- bool: Whether or not the markers to be dropped are draggable.Code:
draggable
Result:
![[Image: image-4.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-4.png)
The code adds a marker to the Google Maps plot. The marker is placed at the latitude and longitude coordinates (47.51503432784726, 19.005350430919034) and is labeled
Code:
'finxter'The
Code:
info_windowThe
Code:
draggableCode:
FalseThe fourth line enables marker dropping, meaning the user can add new markers to the plot by clicking on the map.
The final line saves the plot to an HTML file named
Code:
marker.htmlRoute planning (directions)
Using the Directions API, you can display route planning between any points. The origin and destination coordinates are given as parameters (float, float). Optionally, the
Code:
waypointsCode:
travel_mode- (Default) indicates standard driving directions using the road network.Code:
DRIVING
- requests bicycling directions via bicycle paths & preferred streets.Code:
BICYCLING
- requests directions via public transit routes.Code:
TRANSIT
- requests walking directions via pedestrian paths & sidewalks.Code:
WALKING
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
gmap.directions( (47.5194613766804, 19.000656008676216), (47.520243896650946, 19.00204002854648), waypoints = [(47.520888742275, 18.99871408933636)])
gmap.directions( (47.5194613766804, 19.000656008676216), (47.520243896650946, 19.00204002854648), waypoints = [(47.520888742275, 18.99871408933636)]) gmap.directions( (47.52226897515179, 19.00018393988221), (47.520243896650946, 19.00204002854648), waypoints = [(47.52088149688948, 19.002871513347902)])
gmap.draw('route.html')Result:
![[Image: image-5.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-5.png)
The fourth line adds a route to the plot using the
Code:
directions()The starting point of the route is at latitude 47.5194613766804 and longitude 19.000656008676216, and the ending point is at latitude 47.520243896650946 and longitude 19.00204002854648.
The
Code:
waypointsThe fifth line adds another route to the plot, starting at the same point as the previous route and ending at the same point as the previous route, but with different waypoints.
The sixth line adds a third route to the plot, with a starting point at latitude 47.52226897515179 and longitude 19.00018393988221, an ending point at latitude 47.520243896650946 and longitude 19.00204002854648, and a set of waypoints containing one set of latitude and longitude coordinates (47.52088149688948, 19.002871513347902).
Display many points (scatter)
The
Code:
scatter()Code:
latCode:
lon- Code:
color
- Code:
size
- Code:
marker
- Code:
symbol
- Code:
title
- Code:
label
- Code:
precision
- Code:
face_alpha
- Code:
edge_alpha
- Code:
edge_width
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
letters = zip(*[ (47.51471253011692, 18.990678878050492), (47.51941514547201, 18.993554206158933), (47.52134244386804, 18.998060317311538), (47.52337110249922, 19.002008528961046), (47.52344355313603, 19.009969325319076), (47.52466070898612, 19.013488383565445), (47.526645771633746, 19.02031192332838)])
gmap.scatter(*letters, color=['limegreen', 'cyan','gold','orange', 'limegreen', 'cyan', 'orange'], s=60, ew=1, title=['f', 'i', 'n', 'x', 't', 'e', 'r'], label=['f', 'i', 'n', 'x', 't', 'e', 'r']
)
gmap.draw('scatter.html')Result:
The fourth line defines a list of latitude and longitude coordinates as tuples, representing the locations of individual letters of the word
Code:
'finxter'![[Image: image-6.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-6.png)
The fifth line uses the
Code:
scatter()Code:
scatter()Code:
*lettersThe
Code:
colorCode:
sCode:
ewCode:
titleCode:
labelDraw circle (circle)
Sometimes it is useful to draw a circle. In addition to specifying the center
Code:
latCode:
lngCode:
radius- float: Opacity of the circle’s edge, ranging from 0 to 1. Defaults to 1.0.Code:
edge_alpha/ea
- int: Width of the circle’s edge, in pixels. Defaults to 1.Code:
edge_width/ew
- float: Opacity of the circle’s face, ranging from 0 to 1. Defaults to 0.5.Code:
face_alpha/alpha
- str: Color of the circle’s face. Can be hex (“#00FFFF”), named (“cyan”), or matplotlib-like (“c”). Defaults to black.Code:
color/c/face_color/fc
- Code:
color/c/edge_color/ec
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
gmap.circle(47.51894874729591, 18.99426698678921, 200, face_alpha = 0.4, ec = 'cyan', fc='cyan')Result:
![[Image: image-7.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-7.png)
The fourth line uses the
Code:
circle()The
Code:
circle()The
Code:
face_alphaCode:
ecCode:
fcPolyline (plot)
A polyline is a line composed of one or more sections. If we want to display such a line on our map, we use the
Code:
plotCode:
latsCode:
lons- /Code:
color/Code:c/Code:edge_colorstr : Color of the polyline. Can be hex (“#00FFFF”), named (‘cyan’), or matplotlib-like (‘c’). Defaults to black.Code:ec
- /Code:
alpha/Code:edge_alphafloat: Opacity of the polyline, ranging from 0 to 1. Defaults to 1.0.Code:ea
- /Code:
edge_widthint: Width of the polyline, in pixels. Defaults to 1.Code:ew
- int: Number of digits after the decimal to round to for lat/lng values. Defaults to 6.Code:
precision
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
f = zip(*[(47.513285942712805, 18.994089961008104), (47.51453956566773, 18.991150259891935), (47.51573518971617, 18.992276787691928), (47.51453956566773, 18.991150259891935), (47.51417000363217, 18.992040753295736), (47.515372882275294, 18.99316728109573)])
gmap.plot(*f, edge_width = 7, color = 'limegreen')
gmap.draw('poly.html')Result:
![[Image: image-8.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-8.png)
Code:
f = zip(*[(47.513285942712805, 18.994089961008104), (47.51453956566773, 18.991150259891935), (47.51573518971617, 18.992276787691928), (47.51453956566773, 18.991150259891935), (47.51417000363217, 18.992040753295736), (47.515372882275294, 18.99316728109573)])This line creates a list of latitude-longitude pairs that define the vertices of a polygon. The
Code:
zip(*[...])Code:
gmap.plot(*f, edge_width = 7, color = 'limegreen')This line plots the polygon on the Google Map. The
Code:
plot()Code:
*fCode:
edge_widthCode:
colorCreate heatmap (heatmap)
Plot a heatmap.
Parameters:
- [float],Code:
Latitudes
- [float]Code:
Longitudes
Optional Parameters:
- [int]: Radius of influence for each data point, in pixels. Defaults to 10.Code:
radius
- [(int, int, int, float)]: Color gradient of the heatmap as a list of RGBA colors. The color order defines the gradient moving towards the center of a point.Code:
gradient
- [float]: Opacity of the heatmap, ranging from 0 to 1. Defaults to 0.6.Code:
opacity
- [int]: Maximum intensity of the heatmap. Defaults to 1.Code:
max_intensity
- [bool]: True to dissipate the heatmap on zooming, False to disable dissipation.Code:
dissipating
- [int]: Number of digits after the decimal to round to for lat/lng values. Defaults to 6.Code:
precision
- [float]: List of weights corresponding to each data point. Each point has a weight of 1 by default. Specifying a weight of N is equivalent to plotting the same point N times.Code:
weights
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
letters = zip(*[ (47.51471253011692, 18.990678878050492), (47.51941514547201, 18.993554206158933), (47.52134244386804, 18.998060317311538), (47.52337110249922, 19.002008528961046), (47.52344355313603, 19.009969325319076), (47.52466070898612, 19.013488383565445), (47.526645771633746, 19.02031192332838)]) gmap.heatmap( *letters, radius=55, weights=[0.1, 0.2, 0.5, 0.6, 1.8, 2.10, 1.12], gradient=[(89, 185, 90, 0), (54, 154, 211, 0.5), (254, 179, 19, 0.79), (227, 212, 45, 1)], opacity = 0.7
)
gmap.draw('heatmap.html')Result:
![[Image: image-9.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-9.png)
First, a list of tuples called
Code:
lettersThen, an instance of the
Code:
GoogleMapPlotterNext, the
Code:
heatmapCode:
GoogleMapPlotterCode:
lettersThe
Code:
radiusCode:
weightsThe
Code:
gradientFinally, the
Code:
opacityPicture above the map (ground_overlay)
Overlay an image from a given URL onto the map.
Parameters:
- [str]: URL of image to overlay.Code:
url
- [dict]: Image bounds, as aCode:
boundsof the formCode:dict.Code:{'north':, 'south':, 'east':, 'west':}
Optional Parameters:
- [float]: Opacity of the overlay, ranging from 0 to 1. Defaults to 1.0.Code:
opacity
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
url = 'https://finxter.com/wp-content/uploads/2022/01/image-2.png'
bounds = {'north': 47.53012124664374, 'south': 47.50860174660818, 'east': 19.0247910821219, 'west': 18.985823949220986}
gmap.ground_overlay(url, bounds, opacity=0.3)
gmap.draw('overlay.html')Result:
![[Image: image-10.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-10.png)
The variable
Code:
urlCode:
boundsFinally, the
Code:
ground_overlayCode:
GoogleMapPlotterCode:
overlay.htmlCode:
drawPlot a Polygon
Parameters:
- [float]: Latitudes.Code:
lats
- [float]: Longitudes.Code:
lngs
Optional Parameters:
- /Code:
color/Code:c/Code:edge_colorstr: Color of the polygon’s edge. Can be hex (“#00FFFF”), named (“cyan”), or matplotlib-like (“c”). Defaults to black.Code:ec
- /Code:
alpha/Code:edge_alphafloat: Opacity of the polygon’s edge, ranging from 0 to 1. Defaults to 1.0.Code:ea
- /Code:
edge_widthint: Width of the polygon’s edge, in pixels. Defaults to 1.Code:ew
- /Code:
alpha/Code:face_alphafloat: Opacity of the polygon’s face, ranging from 0 to 1. Defaults to 0.3.Code:fa
- /Code:
color/Code:c/Code:face_colorstr: Color of the polygon’s face. Can be hex (“#00FFFF”), named (“cyan”), or matplotlib-like (“c”). Defaults to black.Code:fc
- int: Number of digits after the decimal to round to for lat/lng values. Defaults to 6.Code:
precision
Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
finxter_in_Budapest = zip(*[ (47.53012124664374, 18.985823949220986), (47.53012124664374, 19.0247910821219), (47.50860174660818, 19.0247910821219), (47.50860174660818, 18.985823949220986), (47.53012124664374, 18.985823949220986)]) gmap.polygon(*finxter_in_Budapest, face_color='grey', face_alpha = 0.15, edge_color='cornflowerblue', edge_width=3)
gmap.draw('poligon.html')Result:
![[Image: image-11.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-11.png)
Defines a set of coordinates for a polygon named
Code:
finxter_in_BudapestCalls the
Code:
gmap.polygon()Code:
*finxter_in_BudapestCode:
face_colorCode:
face_alphaCode:
edge_colorCode:
edge_widthSaves the map as an HTML file named
Code:
'poligon.html'Code:
gmap.draw()Display grid (grid)
The parameters are used to specify the grid start and end points and the width and length of the grid.
Code:
(lat_start, lat_end, lat_increment, lng_start, lng_end, lng_increment)Code:
import gmplot
apikey = ' ' # (your API key here)
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 14, apikey=apikey)
gmap.grid(47.50, 47.53, 0.0025, 19.0, 19.05, 0.0025)
gmap.draw('grid.html')Result:
![[Image: image-12.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-12.png)
This code generates a Google Map centered at latitude 47.519350864380385 and longitude 19.010462124312387, with a zoom level of 14. It then adds a grid to the map with vertical lines spaced 0.0025 degrees apart between longitude 19.0 and 19.05, and horizontal lines spaced 0.0025 degrees apart between latitude 47.50 and 47.53. Finally, it saves the resulting map with the grid to an HTML file named
Code:
"grid.html"Let’s put it together and see where I found “finxter”!
Import module, create
Code:
gmapCode:
import gmplot
#apikey = # (your API key here)
bounds = {'north': 47.623, 'south': 47.323, 'east': 19.208, 'west': 18.808}
gmap = gmplot.GoogleMapPlotter(47.519350864380385, 19.010462124312387, zoom = 13, fit_bounds=bounds, apikey=apikey)Define letter direction routes
Code:
gmap.directions( (47.525977181062025, 19.02052238472371), (47.524798091352515, 19.021546988570606))
gmap.directions( (47.5194613766804, 19.000656008676216), (47.520243896650946, 19.00204002854648), waypoints = [(47.520888742275, 18.99871408933636)]) gmap.directions( (47.52226897515179, 19.00018393988221), (47.520243896650946, 19.00204002854648), waypoints = [(47.52088149688948, 19.002871513347902)])Define letters routes
Code:
r = zip(*[ (47.52356554300279, 19.02012541778466), (47.5259726531124, 19.020546524602626), (47.52484065497139, 19.020254163818944), (47.52481167549813, 19.021541624161788), (47.52479718575549, 19.021541624161788), (47.52398575378016, 19.021906404592265)]) e = zip(*[(47.52529997270366, 19.014375612740643), (47.52403211687861, 19.013828442094937), (47.52369884683277, 19.01479403735207), (47.52514058679826, 19.01530902148921), (47.52369884683277, 19.01479403735207), (47.52335832959914, 19.015786454699683), (47.524981200408554, 19.016301438836823)]) t = zip(*[(47.52326414357012, 19.01047031640738), (47.52316633484429, 19.012573168300694), (47.52319169267961, 19.011403725155944), (47.521873068989784, 19.01161830187975)]) x = zip(*[(47.52149735340168, 19.006154537181025), (47.52312028178215, 19.002850055634386), (47.522424747195465, 19.004411101300082), (47.52301522767022, 19.00516211983341), (47.522424747195465, 19.004411101300082), (47.52161690117839, 19.003729820201993)]) i = zip(*[(47.51762820549394, 18.996203541721567), (47.51873681072719, 18.994594216293006)]) f = zip(*[(47.513285942712805, 18.994089961008104), (47.51453956566773, 18.991150259891935), (47.51573518971617, 18.992276787691928), (47.51453956566773, 18.991150259891935)])Plot the letters
Code:
gmap.plot(*r, edge_width = 7, color = 'orange')
gmap.plot(*e, edge_width = 7, color = 'c')
gmap.plot(*t, edge_width = 7, color = 'limegreen')
gmap.plot(*x, edge_width = 7, color = 'gold')
gmap.plot(*i, edge_width = 7, color = 'cyan')
gmap.plot(*f, edge_width = 7, color = 'limegreen')
gmap.circle(47.51894874729591, 18.99426698678921, 10, face_alpha = 1, ec = 'cyan', fc='cyan')Create text on map:
Code:
finxter_text = ['f', 'i', 'n', 'x', 't', 'e', 'r']
colors = ['limegreen', 'cyan', 'gold','orange', 'limegreen', 'cyan', 'orange']
j = 0
lat = 47.529266431577625
lng = 19.00200303401821
for i in finxter_text: gmap.text(lat, lng, i, color = colors[j], size = 10) j += 1 lng += 0.001Drop a marker with finxter link, enable marker dropping:
Code:
gmap.marker(47.515703432784726, 19.005350430919034, label = 'finxter', info_window = "<a href='https://finxter.com/'>The finxter academy</a>")
gmap.enable_marker_dropping(color = 'black')Define and plot scatter points for letters:
Code:
letters = zip(*[ (47.51471253011692, 18.990678878050492), (47.51941514547201, 18.993554206158933), (47.52134244386804, 18.998060317311538), (47.52337110249922, 19.002008528961046), (47.52344355313603, 19.009969325319076), (47.52466070898612, 19.013488383565445), (47.526645771633746, 19.02031192332838)])
gmap.scatter(*letters, color=['limegreen', 'cyan','gold','orange', 'limegreen', 'cyan', 'orange'], s=60, ew=1, title=['f', 'i', 'n', 'x', 't', 'e', 'r'], label=['f', 'i', 'n', 'x', 't', 'e', 'r']
)Create heatmap:
Code:
letters = zip(*[ (47.51471253011692, 18.990678878050492), (47.51941514547201, 18.993554206158933), (47.52134244386804, 18.998060317311538), (47.52337110249922, 19.002008528961046), (47.52344355313603, 19.009969325319076), (47.52466070898612, 19.013488383565445), (47.526645771633746, 19.02031192332838)])
gmap.heatmap( *letters, radius=55, weights=[0.1, 0.2, 0.5, 0.6, 1.8, 2.10, 1.12], gradient=[(89, 185, 90, 0), (54, 154, 211, 0.5), (254, 179, 19, 0.79), (227, 212, 45, 1)], opacity = 0.7
)Overlay image from URL:
Code:
url = 'https://finxter.com/wp-content/uploads/2022/01/image-2.png'
bounds = {'north': 47.53012124664374, 'south': 47.50860174660818, 'east': 19.0247910821219, 'west': 18.985823949220986}
gmap.ground_overlay(url, bounds, opacity=0.3)Draw polygon:
Code:
finxter_in_Budapest = zip(*[ (47.53012124664374, 18.985823949220986), (47.53012124664374, 19.0247910821219), (47.50860174660818, 19.0247910821219), (47.50860174660818, 18.985823949220986), (47.53012124664374, 18.985823949220986)])
gmap.polygon(*finxter_in_Budapest, face_color='grey', face_alpha = 0.15, edge_color='cornflowerblue', edge_width=3)Draw map to file:
Code:
gmap.draw('finxter_in_budapest.html')Output:
![[Image: image-13.png]](https://blog.finxter.com/wp-content/uploads/2023/04/image-13.png)
Conclusion
Congratulations, now you’ve learned how to draw almost anything on Google Maps with a few lines of code. While
Code:
gmplotCode:
geopandasA Few Final Words on gmplot
Code:
gmplotWith
Code:
gmplotThe library uses the Google Maps API and requires an API key to be able to use it.
Code:
gmplot
