SoFunction
Updated on 2025-04-11

Python uses gmplot to create dynamic map visualization

What is gmplot?

gmplot is a Python library for visualization of static maps based on Google Maps. It provides a simple API to draw geographic information data such as markers, paths, heat maps, etc.

With gmplot, users can easily generate and save static maps in HTML format on Google Maps and view them in their browser.

gmplot installation

To install gmplot, you can use the following command:

pip install gmplot

Create a basic map

Here is a simple example of drawing a basic map:

from gmplot import gmplot

# Initialize the map, set the center coordinates and zoom levelsgmap = (37.7749, -122.4194, 13)  # San Francisco
# Save as HTML file("basic_map.html")

Openbasic_map.htmlThe file can be used to view the generated map in the browser.

Add tags and layers

1. Add marking points on the map

Multiple marking points can be drawn on the map, such as cities, places, etc.

# Initialize the mapgmap = (37.7749, -122.4194, 13)

# Add marking pointslatitude_list = [37.7749, 37.7849, 37.7649]
longitude_list = [-122.4194, -122.4294, -122.4094]
(latitude_list, longitude_list, color='red', size=40, marker=True)

# Save the map("scatter_map.html")

2. Draw the path

You can draw the path between two points, such as walking routes, car routes, etc.

#Latitude and longitude listpath_latitudes = [37.7749, 37.7849, 37.7649]
path_longitudes = [-122.4194, -122.4294, -122.4094]

# Draw path(path_latitudes, path_longitudes, 'blue', edge_width=2.5)

# Save the map("path_map.html")

3. Add polygons

Drawing polygons can be used to mark areas.

# Polygon coordinatespolygon_latitudes = [37.7749, 37.7849, 37.7649, 37.7749]
polygon_longitudes = [-122.4194, -122.4294, -122.4094, -122.4194]

# Draw polygons(polygon_latitudes, polygon_longitudes, color='green')

# Save the map("polygon_map.html")

Add a heat map

Heatmap is a way to visualize data density, and gmplot provides a very simple API.

# Data points (usually used in heat maps)heatmap_latitudes = [37.7749, 37.7849, 37.7649, 37.7549, 37.7449]
heatmap_longitudes = [-122.4194, -122.4294, -122.4094, -122.3994, -122.3894]

# Add a heat map(heatmap_latitudes, heatmap_longitudes)

# Save the map("")

Using Google Maps API Key

After you get the Google Maps API key from Google Cloud Platform, you can add it to gmplot. This allows advanced map functionality to be supported.

from gmplot import gmplot

# Initialize the map with API keysgmap = (37.7749, -122.4194, 13, apikey="YOUR_API_KEY")

# Draw scatter pointslatitude_list = [37.7749, 37.7849, 37.7649]
longitude_list = [-122.4194, -122.4294, -122.4094]
(latitude_list, longitude_list, color='red', size=40, marker=True)

# Save the map("map_with_apikey.html")

Customize map styles

gmplot provides some customization options that allow users to adjust the style of the map.

# Customize map styles = "/v1/%s/"
([37.7749], [-122.4194], color='purple', marker=True)

# Save the map("custom_style_map.html")

A complete example

Here is a complete code example combining multiple functions:

from gmplot import gmplot

# Initialize the mapgmap = (37.7749, -122.4194, 13)

# Add scatter pointslatitude_list = [37.7749, 37.7849, 37.7649]
longitude_list = [-122.4194, -122.4294, -122.4094]
(latitude_list, longitude_list, color='red', size=40, marker=True)

# Add pathpath_latitudes = [37.7749, 37.7849, 37.7649]
path_longitudes = [-122.4194, -122.4294, -122.4094]
(path_latitudes, path_longitudes, 'blue', edge_width=2.5)

# Add a heat mapheatmap_latitudes = [37.7749, 37.7849, 37.7649, 37.7549, 37.7449]
heatmap_longitudes = [-122.4194, -122.4294, -122.4094, -122.3994, -122.3894]
(heatmap_latitudes, heatmap_longitudes)

# Save the map("complete_map.html")

Advantages and limitations of gmplot

Advantages:

  1. Simple and easy to use, suitable for quickly generating map visualizations.
  2. Supports multiple functions, such as heat maps, paths, polygons, etc.
  3. You can get started without professional GIS knowledge.

limit:

  1. The generated map is static and has poor interactivity.
  2. Limited support for large data sets, and performance issues may occur when processing large amounts of data.
  3. Relying on the Google Maps API, if advanced features are required, you need to obtain and set the API key.

Summarize

gmplot is a lightweight geodata visualization tool suitable for quickly generating static maps based on Google Maps. If you need to deal with more complex geographic data or implement dynamic interactions, you can use it in conjunction with other libraries such as Folium or Plotly. I hope this article can help you quickly understand how to use gmplot!

The above is the detailed content of Python using gmplot to create dynamic map visualization. For more information about Python gmplot dynamic visualization, please follow my other related articles!