このジオコーディング コードはローカルで機能しますが、サーバーでは機能しません。アドレスを次の形式で取得し、関数から値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 サーバーに書き込む正しい方法は何ですか?