Python based on the Baidu API to obtain latitude and longitude, according to the latitude and longitude of the distance between cities __python

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.