首页 > 编程语言 > 详细

python调用google map api

时间:2017-04-18 15:36:43      阅读:235      评论:0      收藏:0      [点我收藏+]
import urllib
import urllib.request
import json
serviceurl = ‘http://maps.googleapis.com/maps/api/geocode/json?‘
#serviceurl = ‘http://python-data.dr-chuck.net/geojson?‘

while True:
    address = input(‘Enter location:‘)
    if len(address) < 1:break
    url = serviceurl + urllib.parse.urlencode({‘sensor‘:‘false‘,‘address‘:address})
    print(‘Retrieving:‘,url)
    uh = urllib.request.urlopen(url)
    data = uh.read()
    print(‘Retrieved:‘,len(data),‘characters‘)

    print(str(data))
    #接受过来的数据都是字节型数据需要进行decode()操作
    try: js = json.loads(data.decode())
    except: js = None
#try:js = json.loads(str(data))
    #except: js = None
    print(js)
    if ‘status‘ not in js or js[‘status‘] != ‘OK‘:
        print(‘===Failed To Retrieve===‘)
        print(data)
        continue
    print(json.dumps(js,indent=4))

    lat = js[‘results‘][0][‘geometry‘][‘location‘][‘lat‘]
    lng = js[‘results‘][0][‘geometry‘][‘location‘][‘lng‘]
    print(‘lat:‘,lat,‘lng:‘,lng)
    location = js[‘results‘][0][‘formatted_address‘]
    print(location)

python调用google map api

原文:http://www.cnblogs.com/heshangaichirou/p/6728047.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!