使用Python GAE进行Google Maps Business Geocoding

时间:2013-10-30 21:21:37

标签: python google-app-engine google-maps-api-3 server-side google-geocoder

在Google API控制台中,API访问会为serverbrowserAndroidiOS生成密钥。

  1. 从Google App Engine Python生成地理编码需要哪种类型的api_key?
  2. 如果答案是服务器,我是否需要找出GAE的服务器IP地址,它们是什么?我得到了74.125.129.141,这可能会改变。 (或者会吗?)
  3. 如果你有一个Python GAE服务器,你能帮我找一个生成代码的例子吗?我不确定我做对了吗?
  4. 我有此代码,显示data中的result.content。我的目的是使用ndb.GeoPt(lat,lng)

    将地理编码保存到数据库
    def get_geo(address):
      browser_key = "browser_key"
      server_key = "server_key"
      address = cgi.escape(address.replace(" ","+"))
      sensor = "true"
      url = "http://maps.googleapis.com/maps/api/geocode/json?key="+ server_key +"&address=" + address + "&sensor="+ sensor
      result = urlfetch.fetch(url)
      data = json.loads(result.content) # This is the error
      geo = data
      #lat = data['results'][0]['geometry']['location']['lat']
      #long = data['results'][0]['geometry']['location']['lng']
      #return ndb.GeoPt(str(lat) +","+ str(long))
      return geo
    

    使用上面的示例时出现此错误:

      File "C:\xampp\htdocs\mapjobs\main.py", line 18, in get_geo
        data = json.loads(result.content)
      File "C:\Python27\lib\json\__init__.py", line 310, in loads
        return _default_decoder.decode(s)
      File "C:\Python27\lib\json\decoder.py", line 346, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "C:\Python27\lib\json\decoder.py", line 364, in raw_decode
        raise ValueError("No JSON object could be decoded")
    ValueError: No JSON object could be decoded
    

1 个答案:

答案 0 :(得分:0)

它应包含browser_key,网址应为:

url = "http://maps.googleapis.com/maps/api/geocode/json?key="+ browser_key +"&address=" + address + "&sensor="+ sensor