一、創建應用:
首先進入百度地圖開發平台官網:http://lbsyun.baidu.com/index.php?title=首頁
注冊賬號以及創建應用,這里不做詳細說明。
二、js中簡單調用百度接口:
官方參考文檔:http://lbsyun.baidu.com/index.php?title=jspopular/guide/helloworld

<!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Hello, World</title> <style type="text/css"> html{height:100%} body{height:100%;margin:0px;padding:0px} #container{height:100%} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰"> //v2.0版本的引用方式:src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰" </script> </head> <body> <div id="container"></div> <script type="text/javascript"> var map = new BMap.Map("container"); // 創建地圖實例 var point = new BMap.Point(116.404, 39.915); // 創建點坐標 map.centerAndZoom(point, 15); // 初始化地圖,設置中心點坐標和地圖級別 </script> </body> </html>
三、實際應用:
通過從數據庫中獲取單位的所在地區的原始數據,進行簡單的整理之后加入URL地址中
URL進行如下所示的拼接,可以進行地址解析,獲取地區的百度經緯度坐標
url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address=地區,Ak=你的ak)
URL進行如下所示的拼接,可以進行逆地址解析,獲取百度經緯度坐標的結構化地址
url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak='+你的ak+'&output=json&coordtype=wgs84ll&location='+str(緯度)+','+str(經度)
其中轉換類Geocoder:用於坐標與地址間的相互轉換
下面是項目代碼:

import requests import json import pymysql ak='你的ak' def mysql_(): conn = pymysql.connect(host='127.0.0.1', user='數據庫賬號', passwd='數據庫密碼', db='數據庫', port=3306, charset='utf8', cursorclass=pymysql.cursors.DictCursor) cur = conn.cursor() return conn,cur def selectdiyu(): conn, cur = mysql_() sql = "select id,complete from chengguo" cur.execute(sql) diyus=cur.fetchall() return diyus def updatediyu(diyu, adcode, id): data = [] data.append((diyu, adcode, id)) conn, cur = mysql_() sql = "update chengguo set diyu = %s,adcode = %s where id = %s" cur.executemany(sql,data) conn.commit() def updatecode(adcode, id): data = [] data.append((adcode, id)) conn, cur = mysql_() sql = "update chengguo set adcode = %s where id = %s" cur.executemany(sql,data) conn.commit() def getPosition(url): res = requests.get(url) json_data = json.loads(res.text) if json_data['status'] == 0: lat = json_data['result']['location']['lat'] # 緯度 lng = json_data['result']['location']['lng'] # 經度 else: print("Error output!") return json_data['status'] return lat, lng def getlocation(lat, lng, id): url = 'http://api.map.baidu.com/reverse_geocoding/v3/?ak='+ak+'&output=json&coordtype=wgs84ll&location='+str(lat)+','+str(lng) result = requests.get(url) url_text = result.content.decode() text = json.loads(url_text) address = text.get('result').get('addressComponent') province = address.get('province') city = address.get('city') district = address.get('district') adcode = address.get('adcode') if province == city: diyu = province + district else: diyu = province + city + district try: # updatediyu(diyu, adcode, id) updatecode(adcode, id) print('更新成功',id,diyu,adcode) except: print('更新失敗') if __name__ == "__main__": diyus = selectdiyu() for diyu in diyus: add = diyu['complete'].replace('等','').split(' ')[0] add = add.split('、')[0].partition('項目負責人')[0] add_url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address=add,Ak=ak) try: lat, lng = getPosition(add_url) getlocation(lat, lng, diyu['id']) except: add_url = 'http://api.map.baidu.com/geocoding/v3/?address={Address}&output=json&ak={Ak}'.format(Address='天津市'+add,Ak=ak) lat, lng = getPosition(add_url) getlocation(lat, lng, diyu['id'])