標籤:close .json address more 地圖 geo not ext ice
利用高德地圖web服務api實現地理/逆地址編碼
api使用具體方法請查看官方文檔
文檔網址:http://lbs.amap.com/api/webservice/guide/api/georegeo/
1.利用python第三方庫requests實現
Requests庫文檔地址:http://www.python-requests.org/en/master/
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 ‘‘‘ 4 利用高德地圖api實現地址和經緯度的轉換 5 ‘‘‘ 6 import requests 7 8 def geocode(address): 9 parameters = {‘address‘: address, ‘key‘: ‘cb649a25c1f81c1451adbeca73623251‘}10 base = ‘http://restapi.amap.com/v3/geocode/geo‘11 response = requests.get(base, parameters)12 answer = response.json()13 print(address + "的經緯度:", answer[‘geocodes‘][0][‘location‘])14 15 if __name__==‘__main__‘:16 #address = input("請輸入地址:")17 address = ‘北京市海澱區‘18 geocode(address)
2.基於http協議利用標準庫http.client實現
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 ‘‘‘ 4 利用高德地圖api實現地址和經緯度的轉換 5 ‘‘‘ 6 7 8 import http.client 9 import json10 from urllib.parse import quote_plus11 12 base = ‘/v3/geocode/geo‘13 key = ‘cb649a25c1f81c1451adbeca73623251‘14 15 def geocode(address):16 path = ‘{}?address={}&key={}‘.format(base, quote_plus(address), key)17 #print(path)18 connection = http.client.HTTPConnection(‘restapi.amap.com‘,80)19 connection.request(‘GET‘, path)20 rawreply = connection.getresponse().read()21 #print(rawreply)22 reply = json.loads(rawreply.decode(‘utf-8‘))23 print(address + ‘的經緯度:‘,reply[‘geocodes‘][0][‘location‘])24 25 if __name__==‘__main__‘:26 #address = input("請輸入你的地址:")27 address = ‘北京市朝陽區‘28 geocode(address)
3.利用基於傳輸層上socket實現
1 #!/usr/bin/env python3 2 #-*- coding:utf-8 -*- 3 4 import socket 5 from urllib.parse import quote_plus 6 7 request_text = """ 8 GET /v3/geocode/geo?address={}&key=cb649a25c1f81c1451adbeca73623251 HTTP/1.1\r\n 9 Host: restapi.amap.com:80\r\n10 User-Agent: search4.py\r\n11 Connection: close\r\n12 \r\n13 """14 15 def geocode(address):16 sock = socket.socket()17 sock.connect((‘restapi.amap.com‘, 80))18 request = request_text.format(quote_plus(address))19 sock.sendall(request.encode(‘ascii‘))20 raw_reply = b‘‘21 while True:22 more = sock.recv(4096)23 if not more:24 break25 raw_reply += more26 print(raw_reply.decode(‘utf-8‘))27 28 if __name__==‘__main__‘:29 #address = input("請輸入地址:")30 address = ‘北京市朝陽區‘31 geocode(address)
高德地圖api實現地址和經緯度的轉換(python)