Created
March 8, 2015 12:52
-
-
Save ccwang002/59b7c4b6bdd80a6af9c6 to your computer and use it in GitHub Desktop.
Baidu map API usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
import requests | |
class BaiduMap: | |
def __init__(self, key=''): | |
"""Helper class for Baidu map API | |
Parameters | |
---------- | |
key: str | |
API key | |
""" | |
if not key: | |
raise ValueError('Baidu map API key required') | |
# set fixed parameters | |
self._params = { | |
'ak': key, # baidu api key | |
'output': 'json', # always use json | |
} | |
def get(self, params): | |
full_params = {} | |
full_params.update(self._params) | |
full_params.update(params) | |
r = requests.get( | |
'http://api.map.baidu.com/geocoder/v2/', | |
params=full_params | |
) | |
return r.json() | |
def resolve_address(self, lat, lng, show_nearby=False): | |
"""Resolve the postal address by geo location | |
Parameters | |
---------- | |
lat: float | |
Latitude | |
lng: float | |
Longitude | |
show_nearby: bool, False | |
Whether to show nearby postal addresses within 100m | |
""" | |
params = { | |
'location': '{!s},{!s}'.format(lat, lng), | |
'pois': 1 if show_nearby else 0, | |
} | |
json = self.get(params) | |
return json['result'] | |
def resolve_location(self, address, city=None): | |
"""Resolve the geo location by postal address | |
Parameters | |
---------- | |
address: str | |
postal address | |
city: str | |
city name | |
""" | |
params = { | |
'address': address, | |
'city': city, | |
} | |
json = self.get(params) | |
return json['result'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import requests" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"class BaiduMap:\n", | |
" def __init__(self, key=''):\n", | |
" \"\"\"Helper class for Baidu map API\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" key: str\n", | |
" API key\n", | |
" \"\"\"\n", | |
" if not key:\n", | |
" raise ValueError('Baidu map API key required')\n", | |
"\n", | |
" # set fixed parameters\n", | |
" self._params = {\n", | |
" 'ak': key, # baidu api key\n", | |
" 'output': 'json', # always use json\n", | |
" }\n", | |
" \n", | |
" def get(self, params):\n", | |
" full_params = {}\n", | |
" full_params.update(self._params)\n", | |
" full_params.update(params)\n", | |
" r = requests.get(\n", | |
" 'http://api.map.baidu.com/geocoder/v2/',\n", | |
" params=full_params\n", | |
" )\n", | |
" return r.json()\n", | |
" \n", | |
" def resolve_address(self, lat, lng, show_nearby=False):\n", | |
" \"\"\"Resolve the postal address by geo location\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" lat: float\n", | |
" Latitude\n", | |
" lng: float\n", | |
" Longitude\n", | |
" show_nearby: bool, False\n", | |
" Whether to show nearby postal addresses within 100m\n", | |
" \"\"\"\n", | |
" params = {\n", | |
" 'location': '{!s},{!s}'.format(lat, lng),\n", | |
" 'pois': 1 if show_nearby else 0,\n", | |
" }\n", | |
" json = self.get(params)\n", | |
" return json['result']\n", | |
"\n", | |
" def resolve_location(self, address, city=None):\n", | |
" \"\"\"Resolve the geo location by postal address\n", | |
" \n", | |
" Parameters\n", | |
" ----------\n", | |
" address: str\n", | |
" postal address\n", | |
" city: str\n", | |
" city name\n", | |
" \"\"\"\n", | |
" params = {\n", | |
" 'address': address,\n", | |
" 'city': city,\n", | |
" }\n", | |
" json = self.get(params)\n", | |
" return json['result']" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"bmap = BaiduMap(key='') # put your API key here" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'result': {'confidence': 80,\n", | |
" 'level': '商务大厦',\n", | |
" 'location': {'lat': 40.056876296398, 'lng': 116.30783584945},\n", | |
" 'precise': 1},\n", | |
" 'status': 0}" | |
] | |
}, | |
"execution_count": 4, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"bmap.get({\n", | |
" 'address': '百度大厦',\n", | |
" 'city': '北京市',\n", | |
"})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"{'result': {'addressComponent': {'city': '北京市',\n", | |
" 'country': '中国',\n", | |
" 'country_code': 0,\n", | |
" 'direction': '附近',\n", | |
" 'distance': '7',\n", | |
" 'district': '海淀区',\n", | |
" 'province': '北京市',\n", | |
" 'street': '中关村大街',\n", | |
" 'street_number': '27号1101-08室'},\n", | |
" 'business': '中关村,人民大学,苏州街',\n", | |
" 'cityCode': 131,\n", | |
" 'formatted_address': '北京市海淀区中关村大街27号1101-08室',\n", | |
" 'location': {'lat': 39.983424051248, 'lng': 116.32298703399},\n", | |
" 'poiRegions': [],\n", | |
" 'sematic_description': '中关村大厦附近25米'},\n", | |
" 'status': 0}" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"bmap.get({\n", | |
" 'location': '39.983424,116.322987',\n", | |
" 'pois': 0,\n", | |
"})" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"json = bmap.locate(\n", | |
" lat=39.983424, lng=116.322987,\n", | |
" show_nearby=False\n", | |
")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Joint address: 北京市海淀区中关村大街27号1101-08室\n", | |
"Address Components: {'country_code': 0, 'street': '中关村大街', 'province': '北京市', 'district': '海淀区', 'distance': '7', 'direction': '附近', 'country': '中国', 'street_number': '27号1101-08室', 'city': '北京市'}\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Joint address:', json['formatted_address'])\n", | |
"print('Address Components:', json['addressComponent'])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"json = bmap.resolve(address='百度大厦')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Location lat: 40.0569, lng: 116.3078\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Location lat: {lat:.4f}, lng: {lng:.4f}'.format(**json['location']))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"json = bmap.resolve(address='百度大厦', city='北京市')" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Location lat: 40.0569, lng: 116.3078\n" | |
] | |
} | |
], | |
"source": [ | |
"print('Location lat: {lat:.4f}, lng: {lng:.4f}'.format(**json['location']))" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment