python利用百度map webapi获得经纬度
程序员文章站
2022-03-30 11:54:49
1 # -*- coding: utf-8 -*- 2 import urllib 3 import urllib2 4 import json 5 6 def getloca(loca): 7 url='http://api.map.baidu.com/geocoder/v2/?address=' ......
1 # -*- coding: utf-8 -*- 2 import urllib 3 import urllib2 4 import json 5 6 def getloca(loca): 7 url='http://api.map.baidu.com/geocoder/v2/?address='+loca+\ 8 '&output=json&ak=grfpde3wyt8zy6oonb26pcxpeblxgkuo&callback=showlocation'#从百度api调用 9 rawdata=urllib2.urlopen(url) 10 readit=rawdata.read() 11 r=readit[27:-1]#输出的格式为str,由于开头有函数名showlocation&&showlocation 不将其剔除无法转换为dict类型 12 reloca=json.loads(r) 13 return reloca['result']['location']['lng'],reloca['result']['location']['lng']#返回元组 14 15 getloca('somewhere')