How to Capture 1‑km Map Snapshots with Baidu Maps API and Python
This article explains how to use Baidu Maps' API with Python to programmatically generate map images covering a 1‑kilometer radius around a point, including step‑by‑step instructions, required API registration, coordinate setup, and a complete code example.
1. Introduction
In a Python community a user asked how to capture a map image of a 1‑km radius around a point on Baidu Maps and how to draw four such circles simultaneously.
2. Implementation
Baidu Maps does not provide a direct visual tool, but its API can be used. The steps are:
Register for a Baidu Maps developer account and obtain an API key.
Familiarize yourself with the “rectangle area search” feature to define the southwest and northeast coordinates of the desired area.
Write Python code to call the API and retrieve the snapshot.
import requests
# Replace with your API key
api_key = 'YOUR_API_KEY'
# Coordinates of southwest and northeast corners
south_west_lat = 39.983424
north_east_lat = 40.016969
south_west_lng = 116.322987
north_east_lng = 116.407574
# Image size and zoom
width = 600
height = 400
zoom = 13
# Build request URL
url = f'http://api.map.baidu.com/map/v2/copyright/render?ak={api_key}&xb=1&lb=1&ts=1681826822&z={zoom}&mcode=6E9A7D76511E76E4E8F0EFD18C4D8FD3&latlng={south_west_lat},{south_west_lng}|{north_east_lat},{north_east_lng}&width={width}&height={height}&marker=1,{south_west_lat},{south_west_lng}'
# Send request
response = requests.get(url)
if response.status_code == 200:
with open('map_snapshot.png', 'wb') as f:
f.write(response.content)
else:
print('Failed to get map snapshot:', response.status_code)Note that the coordinates in the example are for an area near Tiananmen Square in Beijing; replace them with the desired location.
Save the API response content as an image file.
3. Conclusion
The article demonstrates a practical solution for generating map snapshots of a 1‑km radius using Baidu Maps API and Python, providing a reusable code snippet for similar geospatial tasks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
