通过百度地图API--获取全国地图的经纬度

时间:2023-03-09 15:36:11
通过百度地图API--获取全国地图的经纬度

因为要做一个前端画图需要经纬度,一个个的查询过麻烦,最终弄出这个,以备后查!

 import threading , time
import requests
from decimal import Decimal
#爬取数据
def hq(address,name_id):
url = 'http://api.map.baidu.com/geocoder?output=json&key=f247cdb592eb43ebac6ccd27f796e2d2&location=' + str(address)
response = requests.get(url)
answer = response.json()
print('得到反解数据', answer)
lng = answer['result']['location']['lng']
lat = answer['result']['location']['lat']
formatted_address = answer['result']['formatted_address']
business = answer['result']['business']
city = answer['result']['addressComponent']['city']
direction = answer['result']['addressComponent']['city']
distance = answer['result']['addressComponent']['direction']
district = answer['result']['addressComponent']['district']
province = answer['result']['addressComponent']['province']
street = answer['result']['addressComponent']['street']
street_number = answer['result']['addressComponent']['street_number']
cityCode = answer['result']['cityCode']
lin_list = str('%6f' % lng) + '|' + str('%6f' % lat) + '|' + str(formatted_address) + '|' + str(business) + '|' + str(
city) + '|' + str(direction) + '|' + str(distance) + '|' + str(district) + '|' + str(province) + '|' + str(
street) + '|' + str(street_number) + '|' + str(cityCode)
if cityCode==0:
#print('外国')
pass
else: name=str(name_id)+'list'
print(name)
with open(name, 'a+', encoding=('utf-8')) as f:
f.write(lin_list+'\n')
print('文件写入完成') #经度纬度处理
def longitude_proces(longitude, interval, latitude, latitude_end, name_id):
while longitude >= latitude:
address = '%s,%s' % (longitude, latitude_end) # 请求时,经度,纬度,需要互换
hq(address, name_id)
longitude -= interval class Thre(threading.Thread):#继承线程中的类
def __init__(self,lists,interval,name_id,times):
super(Thre,self).__init__()
self.interval=interval
self.lists=lists
self.name_id=name_id
self.times=times
def run(self):
print('执行线程开始时间:',self.times,self.lists,'==================start=============================================')
slog,elog,slat,elat=self.lists
#print(slog,elog,slat,elat)
longitude = Decimal(slog)#经度longitude开始
longitude_end = Decimal(elog)#经度结束
latitude=Decimal(slat)# 纬度latitude开始
latitude_end=Decimal(elat)#纬度结束 while latitude>=latitude_end:
latitude -= self.interval
longitude_proces(longitude,self.interval,longitude_end,latitude,self.name_id)
else:
den_time=time.time()-self.times
print('执行线程所用时间:',den_time,self.lists,'==================end=============================================') def main():
itude_list=[
['42.000000', '30.000000', '105.000000', '79.800000'], #
['42.000000', '21.000000', '129.000000', '105.000000'], #
['50.000000', '42.000000', '135.000000','129.000000'], #
['54.000000', '42.000000', '129.000000', '115.000000'], #
['45.500000', '42.000000', '115.000000', '105.000000'], #
['49.200000', '42.000000', '91.500000', '79.800000'], #
['42.000000', '30.000000', '79.800000', '73.400000'], #
['45.500000', '42.000000', '105.000000', '91.500000'], #
['30.000000', '21.000000', '105.000000', '97.300000'], #
['42.000000', '30.000000', '97.300000', '79.800000'], #
['21.000000', '3.000000', '129.000000', '105.000000'], # (南海)
] interval = Decimal('3.0001000')#间隔
number = 0
thre_list=[] for itude in itude_list:
start_times=time.time()
number += 1
temp=Thre(itude_list,interval,number,start_times)
thre_list.append(temp) for thre in thre_list:
thre.start() if __name__ == '__main__':
main()