为什么我的地理位置不起作用?



我的函数的职责是计算从我的公司到任何地址的距离(最后(打印功能(我放了一个地址的例子((。我不知道为什么它不起作用,它说明了回溯错误。

我的函数的第一部分,从地址转换为坐标。 第二部分计算距离。

这是代码

import math
from geopy.geocoders import Nominatim
def Distancia(direccion_domicilio):
geolocator = Nominatim(user_agent="specify_your_app_name_here")
location = geolocator.geocode(direccion_domicilio)
latylon=(location.latitude, location.longitude)

rad=math.pi/180
dif_lat = 20.6072848-(latylon[0])
dif_long = -103.4160099-(latylon[1])
radio=6372.795477598
a=(math.sin(rad*dif_lat/2))**2 + math.cos(rad*location.latitud)*math.cos(rad*20.6072848)*(math.sin(rad*dif_long/2)**2)
distancia=2*radio*math.asin(math.sqrt(a))
return distancia
print(Distancia("Avenida Guadalupe, Real Guadalupe, Jardines de Chapalita, Zapopan, Jalisco, 45030, México"))

这是错误消息

Traceback (most recent call last):
File "C:Python37liburllibrequest.py", line 1317, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "C:Python37libhttpclient.py", line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File "C:Python37libhttpclient.py", line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "C:Python37libhttpclient.py", line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "C:Python37libhttpclient.py", line 1016, in _send_output
self.send(msg)
File "C:Python37libhttpclient.py", line 956, in send
self.connect()
File "C:Python37libhttpclient.py", line 1392, in connect
server_hostname=server_hostname)
File "C:Python37libssl.py", line 412, in wrap_socket
session=session
File "C:Python37libssl.py", line 850, in _create
self.do_handshake()
File "C:Python37libssl.py", line 1108, in do_handshake
self._sslobj.do_handshake()
socket.timeout: _ssl.c:1029: The handshake operation timed out
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:Python37libsite-packagesgeopygeocodersbase.py", line 344, in _call_geocoder
page = requester(req, timeout=timeout, **kwargs)
File "C:Python37liburllibrequest.py", line 525, in open
response = self._open(req, data)
File "C:Python37liburllibrequest.py", line 543, in _open
'_open', req)
File "C:Python37liburllibrequest.py", line 503, in _call_chain
result = func(*args)
File "C:Python37liburllibrequest.py", line 1360, in https_open
context=self._context, check_hostname=self._check_hostname)
File "C:Python37liburllibrequest.py", line 1319, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error _ssl.c:1029: The handshake operation timed out>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:UsersDellDocumentsPython programasEjercicios extrasPROYECTOdistancia.py", line 18, in <module>
print(Distancia("Avenida Guadalupe, Real Guadalupe, Jardines de Chapalita, Zapopan, Jalisco, 45030, México"))
File "C:UsersDellDocumentsPython programasEjercicios extrasPROYECTOdistancia.py", line 6, in Distancia
location = geolocator.geocode(direccion_domicilio)
File "C:Python37libsite-packagesgeopygeocodersosm.py", line 307, in geocode
self._call_geocoder(url, timeout=timeout), exactly_one
File "C:Python37libsite-packagesgeopygeocodersbase.py", line 367, in _call_geocoder
raise GeocoderTimedOut('Service timed out')
geopy.exc.GeocoderTimedOut: Service timed out

您没有执行整个安装过程。回溯会告诉您正在发生的事情。它说SSL证书有问题。geopy库从网络检索数据(在您的情况下为 Nomitatiom(。 看看这个文档:https://geopy.readthedocs.io/en/stable/#geopy.geocoders.options 和名为default_ssl_context的选项。 您应该在代码的开头使用此代码(来自上面的文档(:

  • 要使用 SSL,请使用:
导入拼接单 进口证书 导入地理编码器 ctx = ssl.create_default_context(cafile=certifi.where((( geopy.geocoders.options.default_ssl_context = ctx
  • 禁用 SSL(不推荐,甚至可能不适用于某些地理提供商(:
导入拼接单 导入地理编码器 ctx = ssl.create_default_context(( ctx.check_hostname = 假 ctx.verify_mode = SSL。CERT_NONE geopy.geocoders.options.default_ssl_context = ctx

最新更新