【python调用百度(高德)地图API获取经纬度信息】
@TOC
百度地图开放平台注册
首先百度搜索“百度地图开放平台”(高德地图类似):
接下来选择控制台,点击应用管理
然后点击创建应用,选择“服务端”,点击提交即可创建成功应用(如果没有认证,还需进行认证),其中访问应用(AK)中记录了API key,一般用户的每天限额为5000次。
代码部分
如下所示:
import requests
def get_location_by_address(address, api_key):
# 百度地理编码API的URL
url = "http://api.map.baidu.com/geocoding/v3/"
# 请求参数
params = {
'address': address,
'output': 'json',
'ak': api_key
}
# 发送请求
response = requests.get(url, params=params)
# 解析响应
if response.status_code == 200:
result = response.json()
if result['status'] == 0: # 百度API返回0表示成功
location = result['result']['location']
lng = location['lng'] # 经度
lat = location['lat'] # 纬度
return lng, lat
else:
print(f"无法获取该地址的经纬度: {result['msg']}")
return None, None
else:
print("请求失败")
return None, None
# 示例使用
api_key = "API key"#你的API key
address = "广州塔"#地点名称
lng, lat = get_location_by_address(address, api_key)
if lng and lat:
print(f"地址 '{address}' 的经纬度坐标是: 经度={lng}, 纬度={lat}")
else:
print("获取经纬度失败")
运行结果为
类似的,高德地图API的调用代码为:
import requests
def get_location_by_address(address, api_key):
# 高德地理编码API的URL
url = "https://restapi.amap.com/v3/geocode/geo"
# 请求参数
params = {
'address': address,
'key': api_key
}
# 发送请求
response = requests.get(url, params=params)
# 解析响应
if response.status_code == 200:
result = response.json()
if result['status'] == '1' and len(result['geocodes']) > 0:
location = result['geocodes'][0]['location']
return location
else:
return "无法获取该地址的经纬度"
else:
return "请求失败"
# 示例使用
api_key = "你的高德API Key"
address = "北京市朝阳区望京街道"
location = get_location_by_address(address, api_key)
print(f"地址 '{address}' 的经纬度坐标是: {location}")
运行结果:
此外,经纬度坐标转换的代码为(由gcj02坐标转为wgs84坐标):
def transform_lat(x, y):
pi = math.pi
ee = 0.00669342162296594323
a = 6378245.0
dlat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * math.sqrt(abs(x))
dlat += (20.0 * math.sin(6.0 * x * pi) + 20.0 * math.sin(2.0 * x * pi)) * 2.0 / 3.0
dlat += (20.0 * math.sin(y * pi) + 40.0 * math.sin(y / 3.0 * pi)) * 2.0 / 3.0
dlat += (160.0 * math.sin(y / 12.0 * pi) + 320 * math.sin(y * pi / 30.0)) * 2.0 / 3.0
return dlat
def transform_lon(x, y):
pi = math.pi
ee = 0.00669342162296594323
a = 6378245.0
dlon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * math.sqrt(abs(x))
dlon += (20.0 * math.sin(6.0 * x * pi) + 20.0 * math.sin(2.0 * x * pi)) * 2.0 / 3.0
dlon += (20.0 * math.sin(x * pi) + 40.0 * math.sin(x / 3.0 * pi)) * 2.0 / 3.0
dlon += (150.0 * math.sin(x / 12.0 * pi) + 300.0 * math.sin(x / 30.0 * pi)) * 2.0 / 3.0
return dlon
def gcj02_to_wgs84(lon, lat):
if lon is None:
return None,None
lon,lat=float(lon),float(lat)
pi = math.pi
ee = 0.00669342162296594323
a = 6378245.0
dlat = transform_lat(lon - 105.0, lat - 35.0)
dlon = transform_lon(lon - 105.0, lat - 35.0)
radlat = lat / 180.0 * pi
magic = math.sin(radlat)
magic = 1 - ee * magic * magic
sqrtmagic = math.sqrt(magic)
dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi)
dlon = (dlon * 180.0) / (a / sqrtmagic * math.cos(radlat) * pi)
mglat = lat + dlat
mglon = lon + dlon
return lon * 2 - mglon, lat * 2 - mglat
作者:恰恰·