Skip to main content
 首页 » 编程设计

python之为什么我的地理定位不起作用

2024年12月31日7开发

我的功能的职责是计算从我的公司到任何地址的距离(在最后(打印功能)我放了一个地址示例))。我不知道为什么它不起作用,它说了一些关于回溯错误的事情。

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

这是代码

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:\Python37\lib\urllib\request.py", line 1317, in do_open 
    encode_chunked=req.has_header('Transfer-encoding')) 
  File "C:\Python37\lib\http\client.py", line 1229, in request 
    self._send_request(method, url, body, headers, encode_chunked) 
  File "C:\Python37\lib\http\client.py", line 1275, in _send_request 
    self.endheaders(body, encode_chunked=encode_chunked) 
  File "C:\Python37\lib\http\client.py", line 1224, in endheaders 
    self._send_output(message_body, encode_chunked=encode_chunked) 
  File "C:\Python37\lib\http\client.py", line 1016, in _send_output 
    self.send(msg) 
  File "C:\Python37\lib\http\client.py", line 956, in send 
    self.connect() 
  File "C:\Python37\lib\http\client.py", line 1392, in connect 
    server_hostname=server_hostname) 
  File "C:\Python37\lib\ssl.py", line 412, in wrap_socket 
    session=session 
  File "C:\Python37\lib\ssl.py", line 850, in _create 
    self.do_handshake() 
  File "C:\Python37\lib\ssl.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:\Python37\lib\site-packages\geopy\geocoders\base.py", line 344, in _call_geocoder 
    page = requester(req, timeout=timeout, **kwargs) 
  File "C:\Python37\lib\urllib\request.py", line 525, in open 
    response = self._open(req, data) 
  File "C:\Python37\lib\urllib\request.py", line 543, in _open 
    '_open', req) 
  File "C:\Python37\lib\urllib\request.py", line 503, in _call_chain 
    result = func(*args) 
  File "C:\Python37\lib\urllib\request.py", line 1360, in https_open 
    context=self._context, check_hostname=self._check_hostname) 
  File "C:\Python37\lib\urllib\request.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:\Users\Dell\Documents\Python programas\Ejercicios extras\PROYECTOdistancia.py", line 18, in <module> 
    print(Distancia("Avenida Guadalupe, Real Guadalupe, Jardines de Chapalita, Zapopan, Jalisco, 45030, México")) 
  File "C:\Users\Dell\Documents\Python programas\Ejercicios extras\PROYECTOdistancia.py", line 6, in Distancia 
    location = geolocator.geocode(direccion_domicilio) 
  File "C:\Python37\lib\site-packages\geopy\geocoders\osm.py", line 307, in geocode 
    self._call_geocoder(url, timeout=timeout), exactly_one 
  File "C:\Python37\lib\site-packages\geopy\geocoders\base.py", line 367, in _call_geocoder 
    raise GeocoderTimedOut('Service timed out') 
geopy.exc.GeocoderTimedOut: Service timed out 

请您参考如下方法:

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

  • 使用 SSL 使用:
    import ssl 
    import certifi 
    import geopy.geocoders 
    ctx = ssl.create_default_context(cafile=certifi.where()) 
    geopy.geocoders.options.default_ssl_context = ctx 
  • 禁用 SSL(不推荐,甚至可能对某些地理提供商不起作用):
    import ssl 
    import geopy.geocoders 
    ctx = ssl.create_default_context() 
    ctx.check_hostname = False 
    ctx.verify_mode = ssl.CERT_NONE 
    geopy.geocoders.options.default_ssl_context = ctx