高德地图api实现地址和经纬度的转换(python)

利用高德地图web服务api实现地理/逆地址编码

api使用具体方法请查看官方文档

文档网址:http://lbs.amap.com/api/webservice/guide/api/georegeo/

1.利用python第三方库requests实现

Requests库文档地址:http://www.python-requests.org/en/master/

 #!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
利用高德地图api实现地址和经纬度的转换
'''
import requests def geocode(address):
parameters = {'address': address, 'key': 'cb649a25c1f81c1451adbeca73623251'}
base = 'http://restapi.amap.com/v3/geocode/geo'
response = requests.get(base, parameters)
answer = response.json()
print(address + "的经纬度:", answer['geocodes'][0]['location']) if __name__=='__main__':
#address = input("请输入地址:")
address = '北京市海淀区'
geocode(address)

2.基于http协议利用标准库http.client实现

 #!/usr/bin/env python3
#-*- coding:utf-8 -*-
'''
利用高德地图api实现地址和经纬度的转换
''' import http.client
import json
from urllib.parse import quote_plus base = '/v3/geocode/geo'
key = 'cb649a25c1f81c1451adbeca73623251' def geocode(address):
path = '{}?address={}&key={}'.format(base, quote_plus(address), key)
#print(path)
connection = http.client.HTTPConnection('restapi.amap.com',80)
connection.request('GET', path)
rawreply = connection.getresponse().read()
#print(rawreply)
reply = json.loads(rawreply.decode('utf-8'))
print(address + '的经纬度:',reply['geocodes'][0]['location']) if __name__=='__main__':
#address = input("请输入你的地址:")
address = '北京市朝阳区'
geocode(address)

3.利用基于传输层上socket实现

 #!/usr/bin/env python3
#-*- coding:utf-8 -*- import socket
from urllib.parse import quote_plus request_text = """\
GET /v3/geocode/geo?address={}&key=cb649a25c1f81c1451adbeca73623251 HTTP/1.1\r\n\
Host: restapi.amap.com:80\r\n\
User-Agent: search4.py\r\n\
Connection: close\r\n\
\r\n\
""" def geocode(address):
sock = socket.socket()
sock.connect(('restapi.amap.com', 80))
request = request_text.format(quote_plus(address))
sock.sendall(request.encode('ascii'))
raw_reply = b''
while True:
more = sock.recv(4096)
if not more:
break
raw_reply += more
print(raw_reply.decode('utf-8')) if __name__=='__main__':
#address = input("请输入地址:")
address = '北京市朝阳区'
geocode(address)
上一篇:如何在网页中提取Email地址


下一篇:Angularjs参考框架地址