本文介绍了Python/Google Maps API-TimeoutError:[Errno 60]从终端调用函数时操作超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从终端调用一个函数,该终端连接到Google Maps API以返回位置的坐标.

I'm calling a function from my terminal, which connects to the Google Maps API to return the coordinates of a place.

但是,我遇到此错误

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-我认为这没问题,因为它以前运行良好.

The code I'm using is as follows geocode.py - I don't think there a problem with this as it ran fine before.

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)

任何想法可能有什么问题吗?

Any ideas what might be wrong?

推荐答案

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

I get (40.7127753, -74.0059728) as output when I run your exact code (with my own key) on RStudio Cloud. So this is likely an API key-related, environment-related or network-related issue.

要缩小问题的范围,建议您在同一平台上进行尝试.这些是我设置的方式:

To narrow down the issue I recommend you try it out on the same platform. These is how I set it up:

geocode.py

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)

main.py

from geocode import getGeocodeLocation
getGeocodeLocation("New York")

还要确保您的API密钥有效,并且您在项目中启用了计费和地理编码API.请参阅Google的入门指南.

Also make sure that your API key is valid and that you have billing and Geocoding API enabled on your project. Refer to Google's get started guide.

希望这对您有所帮助!

这篇关于Python/Google Maps API-TimeoutError:[Errno 60]从终端调用函数时操作超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:19