How to Turn Any Address into a Map Point with Python and Baidu API
This tutorial explains how to convert a physical address into latitude‑longitude coordinates using the Baidu Maps Geocoding API and then visualize the point on a map with Python's pyecharts library, including code snippets and step‑by‑step instructions.
Overview
In this tutorial we show how to convert a physical address into latitude‑longitude coordinates using the Baidu Maps Geocoding API and then visualize the point on a map with Python’s pyecharts library.
1. Convert address to coordinates
First obtain an API key (AK) from the Baidu Map Open Platform by creating a browser‑side application. Then use the following Python code (requires requests and pandas) to request the geocoding service and extract longitude and latitude.
import pandas as pd
import requests
AK = "Replace with your AK"
def get_position(name, AK):
url = f'http://api.map.baidu.com/geocoding/v3/?address={name}&output=json&ak={AK}'
res = requests.get(url)
val = res.json()
retval = {
'地址': name,
'经度': val['result']['location']['lng'],
'纬度': val['result']['location']['lat'],
'地区标签': val['result']['level'],
'是否精确查找': val['result']['precise']
}
return retval['经度'], retval['纬度']Running the function with an address returns its longitude and latitude, which can be verified on a map.
2. Plot the point on a map
Using pyecharts we create a Geo chart, add the coordinate, and render it directly in a Jupyter notebook.
from pyecharts.charts import Geo
from pyecharts import options
from pyecharts.globals import GeoType
g = Geo().add_schema(maptype="杭州")
g.add_coordinate(addr, longitude, latitude)
data_pair = [(addr, 1)]
g.add("", data_pair, type_=GeoType.EFFECT_SCATTER, symbol_size=20)
g.set_series_opts(label_opts=options.LabelOpts(is_show=False))
g.set_global_opts(title_opts=options.TitleOpts(title="pyecharts地图标点测试"))
g.render_notebook()The resulting map shows the marked location.
For multiple addresses, apply the function with pandas.apply. Heavy usage may require a personal developer account due to API limits.
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.
