地图可视化必备:3种根据地址查询经纬度的方法
一、腾讯地图
1、构造函数
import requests
def tengxun(addr):
url = "https://apis.map.qq.com/jsapi?" #腾讯地图API接口
para = {
"qt": "geoc",
"addr":addr, #传入地址参数
"output": "json",
"key": "D7EBZ-NHYKX-UAH4A-74TW4-6M2JE-UHFLY", #即腾讯地图API的key
"pf":"jsapi",
"ref":"jsapi"
}
req = requests.get(url,para) #请求数据
req = req.json() #转为json格式
#print(req)
m = req["detail"]
g = f"{m['pointx']},{m['pointy']}" #解析到经纬度数据
print(g)
return g
tengxun(addr="深圳市")
'113.883080,22.553290'
2、读取数据
提取码:fjg3
import pandas as pd
df1 = pd.read_excel("gz_school.xlsx")
df1.head()
import time
time_start = time.time() #程序起始时间
df1['经纬度']=df1['address'].apply(tengxun) #调用函数
time_end = time.time() #程序结束时间
t = time_end-time_start #运行时间
print('共用时%s秒'%t)
共用时52.39904499053955秒
4、保存数据
df1.head()
df1.to_excel('result.xlsx',index = False)
二、高德地图
1、构造函数
import pandas as pd
import requests
import time
import csv
import json
def gaode(addr):
para = {
'key':'你自己的', #高德地图开放平台申请的key
'address':addr #传入地址参数
}
url = 'https://restapi.amap.com/v3/geocode/geo?' #高德地图API接口
req = requests.get(url,para)
req = req.json()
print('-' * 30)
m = req['geocodes'][0]['location']
print(m)
return m
gaode(addr="深圳")
2、应用函数
df2 = pd.read_excel('gz_school.xlsx') #读取地址数据
time_start = time.time()
df2['经纬度'] = df2['address'].apply(gaode) #调用函数
time_end = time.time()
t = time_end-time_start
print('共用时%s秒'%t)
共用时37.740272998809814秒
三、百度地图
1、构造函数
import pandas as pd
import requests
import time
import csv
import json
def baidu(addr):
url = "http://api.map.baidu.com/geocoding/v3/?" #百度地图API接口
para = {
"address": addr, #传入地址参数
"output": "json",
"ak": "你自己的" #百度地图开放平台申请ak
}
req = requests.get(url,para)
req = req.json()
#print(req)
print('-' * 30)
m = req["result"]["location"]
g = f"{m['lng']},{m['lat']}"
print(g)
return g
baidu(addr="深圳")
2、应用函数
df3 = pd.read_excel('gz_school.xlsx')
time_start = time.time()
df3['经纬度'] = df3['address'].apply(baidu)
time_end = time.time()
t = time_end-time_start
print('共用时%s秒'%t)
共用时24.0550799369812秒
四、小结
作者:J哥
_往期文章推荐_
评论