此地理编码代码在本地工作,但不在服务器中工作,它以 的形式获取地址并从函数self.request.get('address')
返回值。ndb.GeoPt()
get_lat_long(address)
那么,我怎么会得到错误:
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~map-jobs/2.371278632227250958/main.py", line 453, in post
profile.geopoint = get_lat_long(self.request.get('address'))
File "/base/data/home/apps/s~map-jobs/2.371278632227250958/main.py", line 17, in get_lat_long
lat = data['results'][0]['geometry']['location']['lat']
IndexError: list index out of range
使用此代码:
def get_lat_long(address):
address = cgi.escape(address.replace(" ","+"))
sensor = "true"
url = "http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor="+ sensor
result = urlfetch.fetch(url)
data = json.loads(result.content)
lat = data['results'][0]['geometry']['location']['lat']
long = data['results'][0]['geometry']['location']['lng']
return ndb.GeoPt(str(lat) +","+ str(long))
在 google-app-engine 服务器中写这个的正确方法是什么?