【发布时间】:2016-04-29 22:48:02
【问题描述】:
我想使用 google API 查找两个位置之间的距离。我希望输出看起来像 - “位置 1 和位置 2 之间的距离是 500 英里(此处的距离是示例目的)”,但是由于当前程序显示各种输出(我无法使用),我如何获得所需的输出得到他想要的输出)。你们能告诉我方法吗?或者告诉我具体的程序是什么?
import urllib
import json
serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
while True:
address = raw_input('Enter location: ')
if len(address) < 1 : break
url = serviceurl + urllib.urlencode({'sensor':'false', 'address': address})
print 'Retrieving', url
uh = urllib.urlopen(url)
data = uh.read()
print 'Retrieved',len(data),'characters'
try: js = json.loads(str(data))
except: js = None
if 'status' not in js or js['status'] != 'OK':
print '==== Failure 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 python-2.7 google-api google-geocoding-api