【发布时间】:2016-02-24 06:56:32
【问题描述】:
如何通过 Python 在文件 json 中使用来自 lat/lng 的反向地理编码。之后打印文件 json 位置名称。
import json
Json_data=[{"lat":"3.160","lng":"101.710"},
{"lat":"2.350","lng":"102.030"},
{"lat":"6.120","lng":"102.130"}]
#Json_data=[{"lat":3.160,"lng":101.710},
# {"lat":2.350,"lng":102.030},
# {"lat":6.120,"lng":102.130}]
def revrseGeocode(latlng):
result={}
url ='https://maps.googleapis.com/maps/api/geocode/json?latlng={0} &key={1}'
apikey='AIzaSyC6gSdW2pWz9QgaBY2ZlZcYRTKva9-x74w'
request= url.format(latlng,apikey)
data= json.loads(request)
if len(data['results'])>0:
result=data['results'][0]
return result
for i,row in Json_data:
Json_data[i]= revrseGeocode(Json_data[1][i]+','+Json_data[1][i])
for i,row in Json_data:
if 'address_components' in row['geocode_data']:
for component in row['geocode_data']['address_components']:
if 'country' in component['types']:
Json_data['country'][i]= component['long_name']
for component in row['geocode_data']['address_components']:
if 'locality' in component['types']:
Json_data['city'][i]=component['long_name']
break
elif 'postal_town' in component['types']:
Json_data['city'][i] = component['long_name']
break
elif 'administrative_area_level_2' in component['types']:
Json_data['city'][i]=component['long_name']
break
elif 'administrative_area_level_1' in component['types']:
Json_data['city'][i]= component['long_name']
break
#ERROR:
文件“/home/magic/Desktop/python/test1.py”,第 22 行,在 Json_data[i]= revrseGeocode(Json_data[1][i]+','+Json_data[1][i])
文件“/home/magic/Desktop/python/test1.py”,第 16 行,在 revrseGeocode 数据= json.loads(请求)
文件“/usr/lib/python2.7/json/init.py”,第 338 行,加载中 返回_default_decoder.decode(s)
文件“/usr/lib/python2.7/json/decoder.py”,第 366 行,在解码中 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
文件“/usr/lib/python2.7/json/decoder.py”,第 384 行,在 raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded
【问题讨论】:
-
“反向地理编码”是什么意思?请提供详细信息。至少,说明预期的输出是什么。
-
你在研究这个时发现了什么?
-
您尝试将列表视为字典
标签: python json google-maps