标签:
API 地址:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding
一个简单的API,可以通过地址(地名)获得经纬度,或者通过经纬度获取周边的位置信息。
ak是百度开发者APIKEY,注册地址:http://developer.baidu.com/
#!/usr/bin/env python3 import requests import json def locatebyAddr(address, city=None): ‘‘‘ 根据地址确定经纬度,城市为可选项 ‘‘‘ items = {‘output‘: ‘json‘, ‘ak‘: ‘A9f77xxxxxxxxxxxxxxxxxxxxxxxxxx‘, ‘address‘: address} if city: items[‘city‘] = city r = requests.get(‘http://api.map.baidu.com/geocoder/v2/‘, params=items) dictResult = json.loads(r.text) return dictResult[‘result‘][‘location‘] if not dictResult[‘status‘] else None def locatebyLatLon(lat, lon, pois=0): ‘‘‘ 根据经纬度确定地址 ‘‘‘ items = {‘location‘: str(lat) + ‘,‘ + str(lon), ‘ak‘: ‘A9f77xxxxxxxxxxxxxxxxxxxxxxxxxx‘, ‘output‘: ‘json‘} if pois: items[‘pois‘] = 1 r = requests.get(‘http://api.map.baidu.com/geocoder/v2/‘, params=items) dictResult = json.loads(r.text) return dictResult[‘result‘] if not dictResult[‘status‘] else None def main(): address = input(‘输入地址: ‘) city = input(‘输入城市:(可选)‘) result = locatebyAddr(address, city) print(result) if __name__ == ‘__main__‘: main()
运行:
标签:
原文地址:http://www.cnblogs.com/aeropig/p/py_baidumap_location.html