百度地图的命令行API


./addr2geo.sh 广州塔 | xargs ./geo2point.sh | xargs ./point2coord.sh


./coord2point.sh 113.330995, 23.112229 | xargs ./point2addr.sh

caiguanhao c8d6a16d1d add text color and fix typo; 11 anos atrás
.tests.sh c8d6a16d1d add text color and fix typo; 11 anos atrás
.travis.yml 4460e20f8e add tests; update README; 11 anos atrás
LICENSE 03890e07da initial commit; add point2coord.sh; add LICENSE; add README.md; 11 anos atrás
README.md 4460e20f8e add tests; update README; 11 anos atrás
addr2geo.sh adceb4c535 add support for hex code; 11 anos atrás
addr2point.sh adceb4c535 add support for hex code; 11 anos atrás
coord2point.sh 4971e19929 check bc; 11 anos atrás
geo2point.sh 4971e19929 check bc; 11 anos atrás
gmaps2bdmaps.sh 4971e19929 check bc; 11 anos atrás
point2addr.sh 96a2880036 add wget support to point2addr.sh 11 anos atrás
point2coord.sh 4971e19929 check bc; 11 anos atrás

README.md

Baidu Maps Coordinates Utils

Build Status

Use these utils to automatically fetch coordinates of large quantities of addresses in China. These coordinates may be used as the default coordinates of the embeded Baidu Maps in web pages. Note that the way how Baidu Maps represent its coordinates is different from the rest of the world.

One-line command

Address to coordinates

$ bash addr2geo.sh 广州塔 | xargs bash geo2point.sh | xargs bash point2coord.sh
  113.331110, 23.112097

Coordinates to address

$ bash coord2point.sh 113.331110, 23.112097 | xargs bash point2addr.sh
  广东省广州市海珠区艺苑路

Address to GEO string

Returns the GEO string of the first search result for specified Chinese address in Baidu Maps. If no search results returned, try to remove last character and search again until GEO string is found.

$ bash addr2geo.sh 广州塔
  .=UOqMLBsMwqPA;

Address to point

Returns the point directly and fuzzily. Note that the script will sometimes return nothing if address is too short (for example, address without the name of the city). If the address is too long or does not exist, it will return a nearby point.

$ bash addr2point.sh 广州塔
  12616023.37, 2628610.59

GEO string to point

The argument supports GEO string with only one point. For those strings with multiple points, you may either use Baidu's JavaScript functions to convert them or split the string by ';' and treat each part as an argument to this script.

Points in multi-point GEO string share the same GEO type, which is determined by the first character of the string. A possible multi-point GEO string may look like this: .=LmIPNBjMOxcA;=LmIPNBjMOxcA; .

$ bash geo2point.sh ".=UOqMLBsMwqPA;"
  12616098.76, 2628657.08

Point to coordinates

$ bash point2coord.sh 12616098.76, 2628657.08
  113.331110, 23.112097

Coordinates to point

$ bash coord2point.sh 113.331110, 23.112097
  12616098.73, 2628657.09

Point to address

Find the possible address to the point.

$ bash point2addr.sh 12616098.76, 2628657.08
  广东省广州市海珠区艺苑路

Google Maps coordinates to Baidu Maps coordinates

$ bash gmaps2bdmaps.sh 23.10641, 113.32449
  113.3310312352, 23.112174790841

View / Use Baidu's JavaScript

  • Open http://api.map.baidu.com/lbsapi/getpoint/ in Google Chrome.
  • Open Developer Tools panel and select Sources tab.
  • Select getscript JS file and open Pretty Print mode.
  • Find convertMC2LL: function and add a breakpoint inside the function by clicking the line number.
    • This is the function to convert point to coordinates.
  • Search something in the web page to trigger the script.
  • When it reaches to the breakpoint, click O.parseGeo in Call Stack on the right sidebar.
  • The script file opened in new tab contains:
    • Functions to convert GEO string to point:
    • You can open the Console and type Q(".=LmIPNBjMOxcA;=LmIPNBjMOxcA;")

Bugs

  • Back-and-forth conversion between point and coordinates is not always accurate.
  • Baidu Maps may change the coordinates to the address frequently.
  • Conversion from Google Maps coords to Baidu Maps' may not be accurate if the coords are outside of mainland China.

Requirements

Most of Linux/Unix based operating systems have these software pre-installed.

Examples

Run Tests

You can run bash .tests.sh to see if this tool works.

See Also

Developer

  • caiguanhao

原理

利用 cURL 访问百度地图公开的 API ,搜索地址,从结果中获取GEO字符串,如果没有结果,会删除最后一个字符,继续搜索,直至有结果为止。百度把地图上点的坐标“加密”成为GEO字符串,点的坐标又要经过运算才可得到经纬坐标。百度表示坐标的方法和正常的表示方法相反。

我在百度上找不到可以直接转换或者获取坐标的方法。

利用这个脚本可以快速获取大批地址对应的坐标,这些坐标可以作为各网站上嵌入的百度地图的默认坐标。