根据百度/高德地图api得到。需要自己申请key。
话不多说,直接上代码。
import requests
# 使用高德API
def geocodeG(address):
par = {'address': address, 'key': '自己申请key'}
base = 'http://restapi.amap.com/v3/geocode/geo'
response = requests.get(base, par)
answer = response.json()
GPS = answer['geocodes'][0]['location'].split(",")
return GPS[0], GPS[1]
# 使用百度API
def geocodeB(address):
base = url = "http://api.map.baidu.com/geocoder?address=" + address + "&output=json&key=自己申请key"
response = requests.get(base)
answer = response.json()
return answer['result']['location']['lng'], answer['result']['location']['lat']
adds=[
"德宏州市",
"怒江州市",
"迪庆州市",
"那曲地区市",
"阿里地区市",
"临夏州市",
"甘南州市",
"海东地区市",
"海北州市",
"黄南州市",
"海南州市",
"果洛州市",
"玉树州市",
"海西州市",
"吐鲁番地区市",
"哈密地区市"
]
for a in adds:
print(geocodeB(a))
结果如下: