/baidu-maps-services-python

An Unofficial Python Client Library for Baidu Maps Web Services APIs.

Primary LanguagePythonMIT LicenseMIT

Python Client for Baidu Maps Web Services

image

Descriptions

This is an unofficial Python Client Library for Baidu Maps Web Services APIs:

The author prepared the library just for occasional requirement from one of his friends. If high performance required, Baidu's official JavaScript API may be the best choice. Moreover, thank Google and their Python Client library for Google Maps API Web Services for setting a good model to the author.

Attentions

The library keeps almost the same inputs/outputs as official apis provides, except that:

  • only parse json outputs just now, NO support for xml.
  • default return is a simpler version of raw API callback. set raw=True for complete raw json callback.
  • always use <lng, lat>, NOT <lat, lng> whenever you need.

Occationally, I met Geoconv API at the very beginning which fed on <lng, lat> coordinates order. Took it for granted, nothing surprise. Next, I wrapt Place API, it required <lat, lng>, so I added transform processing, keeping pace with Geoconv API wrapper... How funny it is! All of raw apis, except Geoconv, supported <lat, lng> coordinates order!

Installation

$ git clone git@github.com:elisong/baidu-maps-services-python.git
$ cd baidu-maps-services-python
$ python setup.py install

Note the library was built under Ubuntu 14.04 LTS & Python 2.7.6.

Simple Case

>>> import baidumaps
>>> bdmaps = baidumaps.Client(ak='<Your Baidu Auth Key>')
>>> bdmaps.place_search(query='银行', region='北京')
[{"name": "**工商银行(中关村支行)",
  "location": {"lng": 116.312202,
               "lat": 40.052384},
  "address": "信息路2-1号国际创业园1号楼1-4F",
  "uid": "50f7d1461e9309c472210b6c"},
 {...},
 {...}]

Usage

Before using Baidu Maps APIs services, read at least these official pages:

Initialize Client

>>> import baidumaps
>>> bdmaps = baidumaps.Client(ak='<Your Baidu Auth Key>',
                              domain='http://api.map.baidu.com',
                              output='json')

Choose API

APIs Base URL function here
Place API http://api.map.baidu.com/place/v2/search place_search
http://api.map.baidu.com/place/v2/detail place_detail
http://api.map.baidu.com/place/v2/eventsearch place_eventsearch
http://api.map.baidu.com/place/v2/eventdetail place_eventdetail
Place Suggestion API http://api.map.baidu.com/place/v2/suggestion place_suggest
Geocoding API http://api.map.baidu.com/geocoder/v2/ geocode
Direction API http://api.map.baidu.com/direction/v1 direct
Route Matrix API http://api.map.baidu.com/direction/v1/routematrix route_matrix
IP Location API http://api.map.baidu.com/location/ip ip_locate
Geoconv API http://api.map.baidu.com/geoconv/v1 geoconv

A base url usually conbined by domain(域名)+ server_name(服务名)+version(服务版本号)+subserver_name(子服务名). Take http://api.map.baidu.com/place/v2/search for example:

place_search()

>>> bdmaps.place_search(query='银行', region='北京')
>>> bdmaps.place_search(query='银行', bounds='116.404,39.915,116.414,39.975')
>>> bdmaps.place_search(query='银行', location='116.404,39.915')

For argument bounds, the followings are permitted:

  • '116.404,39.915,116.414,39.975'
  • '116.404,39.915;116.414,39.975'
  • '116.404,39.915|116.414,39.975'
  • [[116.404, 39.915], [116.414, 39.975]]

As for location, likewise.

place_detail()

>>> bdmaps.place_detail(uid='8ee4560cf91d160e6cc02cd7')
>>> bdmaps.place_detail(uids='8ee4560cf91d160e6cc02cd7;5ffb1816cf771a226f476058')

place_eventsearch()

>>> bdmaps.place_eventsearch(query='美食', event='groupon', region='北京',
                             bounds='116.404,39.915,116.414,39.975')

place_eventdetail()

>>> bdmaps.place_eventdetail(uid='8ee4560cf91d160e6cc02cd7')

No support for 2 uids or more in one request.

place_suggest()

>>> bdmaps.place_suggest(query='***', region='北京')

geocode()

>>> bdmaps.geocode(address='百度大厦')
>>> bdmaps.geocode(location='116.43213,38.76623')

direct()

>>> bdmaps.direct(origin='清华大学', destination='北京大学',
                  origin_region='北京', destination_region='北京')
>>> bdmaps.direct(origin='清华大学', destination='北京大学',
                  region='北京', mode='transit')
  • When setting mode=None or mode='driving',origin_region, then destination_region is necessary;
  • When setting mode='walking' or mode='transit', then region is necessary;

ip_locate()

>>> bdmaps.ip_locate()
>>> bdmaps.ip_locate(ip='202.198.16.3')

route_matrix()

>>> bdmaps.route_matrix(origins='114.21892734521,29.575429778924',
                        destinations='115.21892734521,29.575429778924')
>>> bdmaps.route_matrix(origins='***|鸟巢',
                        destinations='北京大学|东方明珠')

geoconv()

>>> bdmaps.geoconv('114.21892734521,29.575429778924')
>>> bdmaps.geoconv([[114.21892734521, 29.575429778924],
                    [114.21892734521, 29.575429778924]])