美文网首页
Identify geographical location o

Identify geographical location o

作者: 广氏菠萝猫 | 来源:发表于2017-10-19 12:06 被阅读0次
    # -*- coding: utf-8 -*-
    import urllib.parse
    import hashlib
    import requests
    import json
    
    '''
    Baidu Map API http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding
    '''
    def get_baidu_api_url(baidu_api_ak, baidu_api_sk, lat, lng):
        baseurl = 'http://api.map.baidu.com'
        svcurl = '/geocoder/v2/?output=json&pois=0&ak='
        query = '&location='
    
        # Calculation follows Baidu Map API standard: http://lbsyun.baidu.com/index.php?title=lbscloud/api/appendix
        queryStr = svcurl + baidu_api_ak + query + str(lat) + "," + str(lng)
        encodedStr = urllib.parse.quote(queryStr, safe="/:=&?#+!$,;'@()*[]")
        rawStr = encodedStr + baidu_api_sk
        baidu_api_sn = hashlib.md5(urllib.parse.quote_plus(rawStr).encode("utf-8")).hexdigest()
    
        url = baseurl + queryStr + "&sn=" + baidu_api_sn
    
        return url
    
    
    if __name__ == '__main__':
    
        # sample: 39.983424, 116.322987 ## Beijing
    
        # sample: 40.758078395, 73.97712620722223  ## Europe
    
        # sample: 22.80646666666667, 113.27083611111111  ## Shunde, Foshan, Guangdong Province, PRC
    
        # sample: 22.65424722222222, 114.7337888888889  ## Huidong, Huizhou, Guangdong Province, PRC
    
        url = get_baidu_api_url('your_baidu_api_ak', 'your_baidu_api_sk', 22.80646666666667, 113.27083611111111)
    
        req = requests.get(url)
        content = req.text
        print(content)
        hjson = json.loads(content)
    
        status = hjson["status"]
        if status == 0:
            location = hjson["result"]["formatted_address"]  # province, city, street
            print(location)
            info = hjson["result"]["sematic_description"]  # address detail
            print(info)
        else:
            print(hjson["message"])
    

    相关文章

      网友评论

          本文标题:Identify geographical location o

          本文链接:https://www.haomeiwen.com/subject/zhjuuxtx.html