1
关于百度地图开放平台
平台提供了多种服务功能,并提供了服务指南和文档,具体可见http://lbsyun.baidu.com/index.php?title=webapi,初次使用时要先点击左侧的“获取密钥”申请自己的ak,每次调用时ak为必填项,且每日有限额。
2
Python调用代码根据地理位置获得经纬度
通过GET请求进行调用,根据选定的输出格式进行结果解析。
#根据地址查找经纬度(百度坐标)def getlnglat(address): url = 'http://api.map.baidu.com/geocoding/v3/' output = 'json' ak = 'GgwK8BGCL80Iu3CrXIwpOG2eGAKs' #生成完整url uri = url + '?' + 'address=杭州' + address + '地铁站&output=' + output + '&ak=' + ak html = requests.get(uri)#获取网页内容 bs_getDetail = BeautifulSoup(html.text,'lxml') #解析网页 #找到存放经纬度数据的文字,转换为字典,取得相应经纬度 lat = eval(bs_getDetail.p.text)["result"]["location"]["lat"] lng = eval(bs_getDetail.p.text)["result"]["location"]["lng"] lng_lat = [lng, lat] return lng_lat
调用结果:
showLocation&&showLocation({"status":0,"result":{"location":{"lng":116.3084202915042,"lat":40.05703033345938},"precise":1,"confidence":80,"comprehension":100,"level":"门址"}})处理后结果:
根据经纬度获得地理位置
#根据经纬度查找地址(百度坐标)def getlocation(lng,lat): url = 'http://api.map.baidu.com/reverse_geocoding/v3/?location=' output = 'json' ak = 'GgwK8BGCL80Iu3CrXIwpOG2eGAKs' #生成完整url uri = url + address + '&output=' + output + '&ak=' + ak + '&location=' + str(lat) + ',' + str(lng) html = requests.get(uri)#获取网页内容 bs_getDetail = BeautifulSoup(html.text,'lxml') #解析网页 #找到存放经纬度数据的文字,转换为字典,取得相应经纬度 location = eval(bs_getDetail.p.text)["result"]["formatted_address"] return location
调用结果:{"status":0,"result":{"location":{"lng":120.30092835093503,"lat":30.42990828603195},"formatted_address":"浙江省杭州市余杭区沿山路11号","business":"临平,东湖","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"浙江省","city":"杭州市","city_level":2,"district":"余杭区","town":"","town_code":"","adcode":"330110","street":"沿山路","street_number":"11号","direction":"西北","distance":"72"},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":179}}
处理后结果: