Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] I Created a Python Program to Visualize Strings on Google Maps

#1
I Created a Python Program to Visualize Strings on Google Maps

<div>
<div class="kk-star-ratings kksr-auto kksr-align-left kksr-valign-top" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1259455&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;0&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;title&quot;:&quot;I Created a Python Program to Visualize Strings on Google Maps&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> <span class="kksr-muted">Rate this post</span> </div>
</p></div>
<p>No, ChatGPT really doesn’t help with this (yet…) <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> but it did help me write the code snippet explanation (which I attached after each code output).</p>
<p>In my example, we will take a special route in Budapest, using one of the easiest to use library, <code>gmplot</code>.</p>
<p>We can create our maps directly in HTML code using its many methods.<br />To access some functions, you need a Google Maps API key.</p>
<p>I used Python’s built-in <code>zip</code> function to create the Lat – Lon coordinates, you can learn more about <code>zip</code> here: </p>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Recommended</strong>: <a href="https://blog.finxter.com/python-ziiiiiiip-a-helpful-guide/" data-type="URL" data-id="https://blog.finxter.com/python-ziiiiiiip-a-helpful-guide/" target="_blank" rel="noreferrer noopener">Python <code>zip()</code> Built-in Function</a></p>
<p>We can work with the following methods of the <code>GoogleMapPlotter</code> object, which I will describe in detail:</p>
<h2 class="wp-block-heading">GoogleMapPlotter</h2>
<ul>
<li><code>from_geocode(location, zoom=10, apikey='')</code></li>
<li><code>draw(path_file)</code></li>
<li><code>get()</code></li>
<li><code>geocode(location, apikey='')</code></li>
<li><code>text(lat, lon, text, color='red')</code></li>
<li><code>marker(lat, lon, color='#FF0000', title=None, precision=6, label=None, **kwargs)</code></li>
<li><code>enable_marker_dropping(color, **kwargs)</code></li>
<li><code>directions</code></li>
<li><code>scatter</code></li>
<li><code>circle</code></li>
<li><code>plot</code></li>
<li><code>heatmap</code></li>
<li><code>ground_overlay</code></li>
<li><code>polygon</code></li>
<li><code>grid</code></li>
</ul>
<p>Let’s go through the functions using “code snippets”:</p>
<h2 class="wp-block-heading">Find a location by name (from_geocode)</h2>
<p>With the <code>geocode</code> method of the <code>GoogleMapPlotter</code> object, we can display a given address and its neighbourhood on the map.</p>
<p>Parameters of the method: <code>location</code> (str), <code>zoom</code> (int), <code>apikey</code> (str)</p>
<p>After creating your map, you have two options, either save the HTML file (<code>.draw()</code>) or store it as a string (with <code>.get()</code>). I use the <code>.draw()</code> method, where the only parameter is the file to create and its path.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import gmplot
apikey = '' # (your API key here)
gmap = gmplot.GoogleMapPlotter.from_geocode('Budapest', apikey=apikey)
gmap.draw("budapest_map.html")</pre>
<p>Result:</p>
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="714" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-2-1024x714.png" alt="" class="wp-image-1259470" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-2-1024x714.png 1024w, https://blog.finxter.com/wp-content/uplo...00x209.png 300w, https://blog.finxter.com/wp-content/uplo...68x535.png 768w, https://blog.finxter.com/wp-content/uplo...mage-2.png 1278w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<p>The code imports the <code>gmplot</code> library, which is a Python wrapper for the Google Maps API. It then creates a new <code>GoogleMapPlotter</code> object named <code>gmap</code> for the location “Budapest” using the <code>from_geocode()</code> method. This method uses the Google Maps API to retrieve the latitude and longitude of the location, which is necessary to display the map.</p>
<p>Finally, the <code>draw()</code> method is called on the <code>gmap</code> object to generate and save the map as an HTML file named <code>budapest_map.html</code>.</p>
<h2 class="wp-block-heading">Coordinates of a location (geocode)</h2>
<p>If you want to know the coordinates of a location, use <code>.geocode()</code>. 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).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import gmplot
apikey = '' # (your API key here)
location = gmplot.GoogleMapPlotter.geocode('Budapest, Hungary', apikey=apikey)
print(location)</pre>
<p>Result:</p>
<p><code>(47.497912, 19.040235)</code></p>
<p>The code calls the <code>geocode()</code> method on the <code>GoogleMapPlotter</code> object to obtain the latitude and longitude of a location specified as a string. In this case, the location is “Budapest, Hungary”. The <code>apikey</code> parameter is also passed to this method to authenticate the Google Maps API.</p>
<h2 class="wp-block-heading">Text on your map (text)</h2>
<p>If you want to place custom text on your map, you can do it with <code>.text()</code>, using the text label’s Latitude and Longitude parameter.</p>
<p>It is possible to color the text with the <code>color=str</code> parameter, which can be the color name (<code>'black'</code>), hexadecimal (<code>'#000000'</code>), or matplotlib-like (<code>'k'</code>).</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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.001</pre>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="510" height="332" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-3.png" alt="" class="wp-image-1259472" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-3.png 510w, https://blog.finxter.com/wp-content/uplo...00x195.png 300w" sizes="(max-width: 510px) 100vw, 510px" /></figure>
</div>
<h3 class="wp-block-heading">Drop marker (marker)</h3>
<p>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:</p>
<ul>
<li><code>color</code> (str) which can be the name of the color (<code>'black'</code>), hexadecimal (<code>'#000000'</code>), or matplotlib-like (<code>'k'</code>)</li>
<li><code>title</code> (str) : Hover-over title of the marker.</li>
<li><code>precision</code> (int) : Number of digits after the decimal to round to for lat/long values. Defaults to 6.</li>
<li><code>label</code> (str) : Label displayed on the marker.</li>
<li><code>info_window</code> (str) : HTML content to be displayed in a pop-up info window.</li>
<li><code>draggable</code> (bool) : Whether or not the marker is draggable.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import gmplot
apikey = ' ' # (your API key here)
gmap.marker(47.51503432784726, 19.005350430919034, label = 'finxter', info_window = "&lt;a href='https://finxter.com/'>The finxter Academy&lt;/a>", draggable = False)</pre>
<h2 class="wp-block-heading">gmap.enable_marker_dropping(color=’black’)</h2>
<p><code>gmap.enable_marker_dropping()</code> allows markers to be dropped onto the map when clicked. Clicking on a dropped marker will delete it. </p>
<p class="has-base-2-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: Calling this function multiple times will just overwrite the existing dropped marker settings.</p>
<p>Parameters:</p>
<ul>
<li><code>color</code> str: Color of the markers to be dropped.</li>
<li><code>title</code> str: Hover-over title of the markers to be dropped.</li>
<li><code>label</code> str: Label displayed on the markers to be dropped.</li>
<li><code>draggable</code> bool: Whether or not the markers to be dropped are draggable.</li>
</ul>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="592" height="387" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-4.png" alt="" class="wp-image-1259474" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-4.png 592w, https://blog.finxter.com/wp-content/uplo...00x196.png 300w" sizes="(max-width: 592px) 100vw, 592px" /></figure>
</div>
<p>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'</code>. </p>
<p>The <code>info_window</code> parameter sets the information displayed when the user clicks on the marker. In this case, it is a link to the <a href="https://academy.finxter.com/" target="_blank" rel="noreferrer noopener">Finxter Academy website</a>. </p>
<p>The <code>draggable</code> parameter is set to <code>False</code>, meaning that the user cannot move the marker.</p>
<p>The fourth line enables marker dropping, meaning the user can add new markers to the plot by clicking on the map.</p>
<p>The final line saves the plot to an HTML file named <code>marker.html</code>.</p>
<h2 class="wp-block-heading">Route planning (directions)</h2>
<p>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>waypoints</code> as list of tuples and the <code>travel_mode</code> as str can also be specified. The travel modes are:</p>
<ul>
<li><code>DRIVING</code> (Default) indicates standard driving directions using the road network.</li>
<li><code>BICYCLING</code> requests bicycling directions via bicycle paths &amp; preferred streets.</li>
<li><code>TRANSIT</code> requests directions via public transit routes.</li>
<li><code>WALKING</code> requests walking directions via pedestrian paths &amp; sidewalks.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')</pre>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="870" height="637" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-5.png" alt="" class="wp-image-1259475" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-5.png 870w, https://blog.finxter.com/wp-content/uplo...00x220.png 300w, https://blog.finxter.com/wp-content/uplo...68x562.png 768w" sizes="(max-width: 870px) 100vw, 870px" /></figure>
</div>
<p>The fourth line adds a route to the plot using the <code>directions()</code> method. </p>
<p>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. </p>
<p>The <code>waypoints</code> parameter is set to a <a href="https://blog.finxter.com/python-lists/" data-type="post" data-id="7332" target="_blank" rel="noreferrer noopener">list</a> containing one set of latitude and longitude coordinates (47.520888742275, 18.99871408933636).</p>
<p>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.</p>
<p>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).</p>
<h2 class="wp-block-heading">Display many points (scatter)</h2>
<p>The <code>scatter()</code> allows you to place many points at once. In addition to the necessary <code>lat</code> (float) and <code>lon</code> (float) parameters, the following optional parameters are:</p>
<ul>
<li><code>color</code></li>
<li><code>size</code></li>
<li><code>marker</code></li>
<li><code>symbol</code></li>
<li><code>title</code></li>
<li><code>label</code></li>
<li><code>precision</code></li>
<li><code>face_alpha</code></li>
<li><code>edge_alpha</code></li>
<li><code>edge_width</code></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')</pre>
<p>Result:</p>
<p>The fourth line defines a list of latitude and longitude coordinates as <a href="https://blog.finxter.com/the-ultimate-guide-to-python-tuples/" data-type="post" data-id="12043" target="_blank" rel="noreferrer noopener">tuples</a>, representing the locations of individual letters of the word <code>'finxter'</code>.</p>
<figure class="wp-block-image size-full"><img decoding="async" loading="lazy" width="741" height="533" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-6.png" alt="" class="wp-image-1259477" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-6.png 741w, https://blog.finxter.com/wp-content/uplo...00x216.png 300w" sizes="(max-width: 741px) 100vw, 741px" /></figure>
<p>The fifth line uses the <code>scatter()</code> method of adding the letters to the plot as points. The <code>scatter()</code> method takes the latitude and longitude coordinates as separate arguments using the <a href="https://blog.finxter.com/python-unpacking/" data-type="post" data-id="396420" target="_blank" rel="noreferrer noopener">unpacking operator</a> (<code>*letters</code>). </p>
<p>The <code>color</code> parameter is set to a list of colors that correspond to the letters. The <code>s</code> parameter specifies the size of the points, the <code>ew</code> parameter specifies the width of the edge around the points, and the <code>title</code> and <code>label</code> parameters specify the title and label of each point, respectively.</p>
<h2 class="wp-block-heading">Draw circle (circle)</h2>
<p>Sometimes it is useful to draw a circle. In addition to specifying the center <code>lat</code>, <code>lng</code>, and <code>radius</code> of the circle, you can also to specify the following:</p>
<ul>
<li><code>edge_alpha/ea</code> float: Opacity of the circle’s edge, ranging from 0 to 1. Defaults to 1.0.</li>
<li><code>edge_width/ew</code> int: Width of the circle’s edge, in pixels. Defaults to 1.</li>
<li><code>face_alpha/alpha</code> float: Opacity of the circle’s face, ranging from 0 to 1. Defaults to 0.5.</li>
<li><code>color/c/face_color/fc</code> str: Color of the circle’s face. Can be hex (“#00FFFF”), named (“cyan”), or matplotlib-like (“c”). Defaults to black.</li>
<li><code>color/c/edge_color/ec</code></li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')</pre>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="677" height="484" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-7.png" alt="" class="wp-image-1259479" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-7.png 677w, https://blog.finxter.com/wp-content/uplo...00x214.png 300w" sizes="(max-width: 677px) 100vw, 677px" /></figure>
</div>
<p>The fourth line uses the <code>circle()</code> method to add a circle to the plot. </p>
<p>The <code>circle()</code> 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. </p>
<p>The <code>face_alpha</code> parameter specifies the transparency of the circle fill, while the <code>ec</code> and <code>fc</code> parameters specify the color of the circle edge and fill, respectively.</p>
<h2 class="wp-block-heading">Polyline (plot)</h2>
<p>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>plot</code> method. In addition to the usual <code>lats</code> [float], <code>lons</code> [float] parameters, you can specify the following optional parameters:</p>
<ul>
<li><code>color</code>/<code>c</code>/<code>edge_color</code>/<code>ec</code> str : Color of the polyline. Can be hex (“#00FFFF”), named (‘cyan’), or matplotlib-like (‘c’). Defaults to black.</li>
<li><code>alpha</code>/<code>edge_alpha</code>/<code>ea</code> float: Opacity of the polyline, ranging from 0 to 1. Defaults to 1.0.</li>
<li><code>edge_width</code>/<code>ew</code> int: Width of the polyline, in pixels. Defaults to 1.</li>
<li><code>precision</code> int: Number of digits after the decimal to round to for lat/lng values. Defaults to 6.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')</pre>
<p>Result: </p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="611" height="421" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-8.png" alt="" class="wp-image-1259482" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-8.png 611w, https://blog.finxter.com/wp-content/uplo...00x207.png 300w" sizes="(max-width: 611px) 100vw, 611px" /></figure>
</div>
<p><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)])</code></p>
<p>This line creates a list of latitude-longitude pairs that define the vertices of a polygon. The <code>zip(*[...])</code> function is used to transpose the list so that each pair of latitude-longitude values becomes a separate tuple.</p>
<p><code>gmap.plot(*f, edge_width = 7, color = 'limegreen')</code></p>
<p>This line plots the polygon on the Google Map. The <code>plot()</code> function takes the <code>*f</code> argument, which is unpacked as separate arguments, representing the latitude and longitude values of the polygon vertices. The <code>edge_width</code> parameter sets the width of the polygon edges in pixels and the <code>color</code> parameter sets the color of the edges.</p>
<h2 class="wp-block-heading">Create heatmap (heatmap)</h2>
<p>Plot a <a href="https://blog.finxter.com/heatmaps-with-seaborn/" data-type="post" data-id="19568" target="_blank" rel="noreferrer noopener">heatmap</a>. </p>
<p><strong>Parameters</strong>:</p>
<ul>
<li><code>Latitudes</code> [float],</li>
<li><code>Longitudes</code> [float]</li>
</ul>
<p><strong>Optional Parameters:</strong></p>
<ul>
<li><code>radius</code> [int]: Radius of influence for each data point, in pixels. Defaults to 10.</li>
<li><code>gradient</code> [(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.</li>
<li><code>opacity</code> [float]: Opacity of the heatmap, ranging from 0 to 1. Defaults to 0.6.</li>
<li><code>max_intensity</code> [int]: Maximum intensity of the heatmap. Defaults to 1.</li>
<li><code>dissipating</code> [bool]: True to dissipate the heatmap on zooming, False to disable dissipation.</li>
<li><code>precision</code> [int]: Number of digits after the decimal to round to for lat/lng values. Defaults to 6.</li>
<li><code>weights</code> [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.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')</pre>
<p>Result: </p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="646" height="447" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-9.png" alt="" class="wp-image-1259487" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-9.png 646w, https://blog.finxter.com/wp-content/uplo...00x208.png 300w" sizes="(max-width: 646px) 100vw, 646px" /></figure>
</div>
<p>First, a list of tuples called <code>letters</code> is created. Each tuple contains two values representing latitude and longitude coordinates of a point on the map.</p>
<p>Then, an instance of the <code>GoogleMapPlotter</code> class is created with a specified center point, zoom level, and an API key.</p>
<p>Next, the <code>heatmap</code> method of the <code>GoogleMapPlotter</code> object is called, passing in the <code>letters</code> list as positional arguments, along with other parameters. </p>
<p>The <code>radius</code> parameter determines the radius of each data point’s influence on the heatmap, while the <code>weights</code> parameter determines the intensity of each data point’s contribution to the heatmap. </p>
<p>The <code>gradient</code> 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. </p>
<p>Finally, the <code>opacity</code> parameter determines the transparency of the heatmap.</p>
<h2 class="wp-block-heading">Picture above the map (ground_overlay)</h2>
<p>Overlay an image from a given URL onto the map.</p>
<p><strong>Parameters:</strong></p>
<ul>
<li><code>url</code> [str]: URL of image to overlay.</li>
<li><code>bounds</code> [dict]: Image bounds, as a <code><a href="https://blog.finxter.com/python-dictionary/" data-type="post" data-id="5232" target="_blank" rel="noreferrer noopener">dict</a></code> of the form <code>{'north':, 'south':, 'east':, 'west':}</code>.</li>
</ul>
<p><strong>Optional Parameters:</strong></p>
<ul>
<li><code>opacity</code> [float]: Opacity of the overlay, ranging from 0 to 1. Defaults to 1.0.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')</pre>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="745" height="543" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-10.png" alt="" class="wp-image-1259489" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-10.png 745w, https://blog.finxter.com/wp-content/uplo...00x219.png 300w" sizes="(max-width: 745px) 100vw, 745px" /></figure>
</div>
<p>The variable <code>url</code> contains the URL of an image that will be used as the ground overlay. The <code>bounds</code> dictionary defines the north, south, east, and west coordinates of the image on the map.</p>
<p>Finally, the <code>ground_overlay</code> method is called on the <code>GoogleMapPlotter</code> 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 <code>overlay.html</code> using the <code>draw</code> method.</p>
<h2 class="wp-block-heading">Plot a Polygon</h2>
<p><strong>Parameters</strong>:</p>
<ul>
<li><code>lats</code> [float]: Latitudes.</li>
<li><code>lngs</code> [float]: Longitudes.</li>
</ul>
<p><strong>Optional Parameters:</strong></p>
<ul>
<li><code>color</code>/<code>c</code>/<code>edge_color</code>/<code>ec</code> str: Color of the polygon’s edge. Can be hex (“#00FFFF”), named (“cyan”), or matplotlib-like (“c”). Defaults to black.</li>
<li><code>alpha</code>/<code>edge_alpha</code>/<code>ea</code> float: Opacity of the polygon’s edge, ranging from 0 to 1. Defaults to 1.0.</li>
<li><code>edge_width</code>/<code>ew</code> int: Width of the polygon’s edge, in pixels. Defaults to 1.</li>
<li><code>alpha</code>/<code>face_alpha</code>/<code>fa</code> float: Opacity of the polygon’s face, ranging from 0 to 1. Defaults to 0.3.</li>
<li><code>color</code>/<code>c</code>/<code>face_color</code>/<code>fc</code> str: Color of the polygon’s face. Can be hex (“#00FFFF”), named (“cyan”), or matplotlib-like (“c”). Defaults to black.</li>
<li><code>precision</code> int: Number of digits after the decimal to round to for lat/lng values. Defaults to 6.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')
</pre>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="733" height="521" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-11.png" alt="" class="wp-image-1259490" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-11.png 733w, https://blog.finxter.com/wp-content/uplo...00x213.png 300w" sizes="(max-width: 733px) 100vw, 733px" /></figure>
</div>
<p>Defines a set of coordinates for a polygon named <code>finxter_in_Budapest</code>.</p>
<p>Calls the <code>gmap.polygon()</code> method with the <code>*finxter_in_Budapest</code> argument to draw the polygon on the map. The <code>face_color</code>, <code>face_alpha</code>, <code>edge_color</code>, and <code>edge_width</code> parameters define the appearance of the polygon. </p>
<p>Saves the map as an HTML file named <code>'poligon.html'</code> using the <code>gmap.draw()</code> method.</p>
<h2 class="wp-block-heading">Display grid (grid)</h2>
<p>The parameters are used to specify the grid start and end points and the width and length of the grid. </p>
<p><code>(lat_start, lat_end, lat_increment, lng_start, lng_end, lng_increment)</code></p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')
</pre>
<p>Result:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="859" height="630" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-12.png" alt="" class="wp-image-1259495" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-12.png 859w, https://blog.finxter.com/wp-content/uplo...00x220.png 300w, https://blog.finxter.com/wp-content/uplo...68x563.png 768w" sizes="(max-width: 859px) 100vw, 859px" /></figure>
</div>
<p>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"</code>.</p>
<h2 class="wp-block-heading">Let’s put it together and see where I found “finxter”!</h2>
<p>Import module, create <code>gmap</code> instance</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)
</pre>
<p>Define letter direction routes</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)])</pre>
<p>Define letters routes</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)])</pre>
<p>Plot the letters</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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')
</pre>
<p>Create text on map:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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.001
</pre>
<p>Drop a marker with finxter link, enable marker dropping:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">gmap.marker(47.515703432784726, 19.005350430919034, label = 'finxter', info_window = "&lt;a href='https://finxter.com/'>The finxter academy&lt;/a>")

gmap.enable_marker_dropping(color = 'black')
</pre>
<p>Define and plot scatter points for letters:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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']
)
</pre>
<p>Create heatmap:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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
)
</pre>
<p>Overlay image from URL:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)
</pre>
<p>Draw polygon:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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)
</pre>
<p>Draw map to file:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">gmap.draw('finxter_in_budapest.html')</pre>
<p>Output:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><img decoding="async" loading="lazy" width="850" height="595" src="https://blog.finxter.com/wp-content/uploads/2023/04/image-13.png" alt="" class="wp-image-1259500" srcset="https://blog.finxter.com/wp-content/uploads/2023/04/image-13.png 850w, https://blog.finxter.com/wp-content/uplo...00x210.png 300w, https://blog.finxter.com/wp-content/uplo...68x538.png 768w" sizes="(max-width: 850px) 100vw, 850px" /></figure>
</div>
<h2 class="wp-block-heading">Conclusion</h2>
<p>Congratulations, now you’ve learned how to draw almost anything on Google Maps with a few lines of code. While <code>gmplot</code> 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 <code>geopandas</code> are a good place to learn more.</p>
<h2 class="wp-block-heading">A Few Final Words on gmplot<br /></h2>
<p><code>gmplot</code> 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. </p>
<p>With <code>gmplot</code>, 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. </p>
<p>The library uses the Google Maps API and requires an API key to be able to use it. <code>gmplot</code> is a useful tool for visualizing geospatial data and creating interactive maps for data exploration and analysis.</p>
</div>


https://www.sickgaming.net/blog/2023/04/...ogle-maps/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016