【问题标题】:Python / Google Maps API - TimeoutError: [Errno 60] Operation timed out when calling a function from the terminalPython / Google Maps API - TimeoutError:[Errno 60] 从终端调用函数时操作超时
【发布时间】:2019-11-22 23:56:21
【问题描述】:

我正在从我的终端调用一个函数,该函数连接到 Google Maps API 以返回某个地点的坐标。

但是,我收到了这个错误

sock.connect((self.host, self.port))
TimeoutError: [Errno 60] Operation timed out

流程如下:

>>>> python
>>>> from geocode import getGeocodeLocation
>>>> getGeocodeLocation("New York")

错误:

sock.connect((self.host, self.port))
TimeoutError: [Errno 60] Operation timed out

我使用的代码如下 geocode.py - 我认为这没有问题,因为它之前运行良好。

import httplib2
import json

def getGeocodeLocation(inputString):
    # Use Google Maps to convert a location into Latitute/Longitute coordinates

    google_api_key = "my_api_key"
    locationString = inputString.replace(" ", "+")
    url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s'% (locationString, google_api_key))
    h = httplib2.Http()
    result = json.loads(h.request(url,'GET')[1])
    latitude = result['results'][0]['geometry']['location']['lat']
    longitude = result['results'][0]['geometry']['location']['lng']
    return (latitude,longitude)

有什么想法可能是错的吗?

【问题讨论】:

    标签: python google-maps google-api


    【解决方案1】:

    当我在 RStudio Cloud 上运行您的确切代码(使用我自己的密钥)时,我得到 (40.7127753, -74.0059728) 作为输出。所以这很可能是 API 密钥相关、环境相关或网络相关的问题。

    为了缩小问题范围,我建议您在同一平台上尝试。我是这样设置的:

    地理编码.py

    import httplib2
    import json
    
    def getGeocodeLocation(inputString):
        # Use Google Maps to convert a location into Latitute/Longitute coordinates
    
        google_api_key = "MY_API_KEY"
        locationString = inputString.replace(" ", "+")
        url = ('https://maps.googleapis.com/maps/api/geocode/json?address=%s&key=%s'% (locationString, google_api_key))
        h = httplib2.Http()
        result = json.loads(h.request(url,'GET')[1])
        latitude = result['results'][0]['geometry']['location']['lat']
        longitude = result['results'][0]['geometry']['location']['lng']
        return (latitude,longitude)
    

    main.py

    from geocode import getGeocodeLocation
    getGeocodeLocation("New York")
    

    还要确保您的 API 密钥有效并且您的项目启用了计费和地理编码 API。参考谷歌的get started guide

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多