1 import json
2 import requests
3
4
5 def baiduMap(input_para):
6 headers = {
7 \'User-Agent\': \'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit\'
8 \'/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safar\'
9 \'i/537.36\'
10 }
11 if not isinstance(input_para, str):
12 input_para = str(input_para)
13 base_url = \'http://api.map.baidu.com/geocoder/v2/\'
14 ak = \'your ak\'
15 output = \'json\'
16 url = base_url + \'?\' + input_para + \'&output=\' + output + \'&ak=\' + ak
17 req = requests.get(url, headers=headers).text
18 temp = json.loads(req)
19 return temp
20
21 def getLngLat(address):
22 place = \'address=\' + address
23 temp = baiduMap(place)
24 if \'result\' in temp.keys():
25 lat = temp[\'result\'][\'location\'][\'lat\']
26 lng = temp[\'result\'][\'location\'][\'lng\']
27 return lat, lng
28 else:
29 print(address + \'无法访问\')
30
31 # lat_lng: tuple格式
32 def getPlace(lat_lng):
33 if not isinstance(lat_lng, str):
34 lat_lng = str(lat_lng)
35 lat_lng = \'location=\' + lat_lng[1:-1]
36 temp = baiduMap(lat_lng)
37 if \'result\' in temp.keys():
38 return temp[\'result\'][\'formatted_address\']
39 else:
40 print(\'经纬度\' + lat_lng + \'无法访问\')