Baidu provides the query latitude and longitude of the API is:
Http://api.map.baidu.com/geocoder?address= the address you want to query &output=xml&key= the key you want to enter
A ready-made key is: F247CDB592EB43EBAC6CCD27F796E2D2 returns JSON
We take the longitude and latitude information of Xi ' an as an example:
Address Bar Input:
Http://api.map.baidu.com/geocoder?address= Xian &OUTPUT=JSON&KEY=F247CDB592EB43EBAC6CCD27F796E2D2
Returns the following JSON information:
{"
status": "OK",
"result": {"Location":
{"
LNG": 108.953098,
lat ": 34.2778
},
" Precise ": 0,
" confidence ":" Level
":" \u57ce\u5e02 "
}
return XML
Address Bar Input:
Http://api.map.baidu.com/geocoder?address= Xian &OUTPUT=XML&KEY=F247CDB592EB43EBAC6CCD27F796E2D2
Returns the following XML information:
<GeocoderSearchResponse>
<status>OK</status>
<result>
<location>
<lat>34.2778</lat>
<lng>108.953098</lng>
</location>
< precise>0</precise>
<confidence>12</confidence>
<level> City </level>
</result>
</GeocoderSearchResponse>
parse the returned XML to get the latitude and longitude of the city
Loc_cache = {}
key = ' F247CDB592EB43EBAC6CCD27F796E2D2 '
def getlocation (addr): From
urllib Import Urlopen from
xml.dom.minidom import parsestring
doc = Urlopen (' http://api.map.baidu.com/geocoder?address= %s&output=xml&key=%s '% (addr, key))
dom = parsestring (Doc.read ())
lat = Doc.getelementsbytagname (' Lat ') [0].firstchild.data
LNG = doc.getelementsbytagname (' LNG ') [0].firstchild.data return
map (lat , LNG))
calculating distance between cities according to latitude and longitude
def distance (A1, A2):
Loc1, loc2 = getlocation (A1), getlocation (A2)
Latdiff = loc1[0]-loc2[0]
Lngdiff = Loc1 [1]-loc2[1]
miles = ((69.1*latdiff) **2 + (53.0*lngdiff) **2) **.5 return
miles*1.6092953
if __name__ = = ' __ main__ ':
print getlocation (' city ')
print getlocation (' Xian city ')
print distance (' City ', ' XI ' an city ')
# [ 33.27447, 114.89338]
# [34.2778, 108.953098]
# 518.801728496