高德地圖api實現地址和經緯度的轉換(python)

來源:互聯網
上載者:User

標籤: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)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.