【问题标题】:why does my geolocation it is not working?为什么我的地理位置不起作用?
【发布时间】:2018-10-15 20:54:32
【问题描述】:

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

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

这是密码

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

【问题讨论】:

    标签: python python-3.x python-3.7 geopy nominatim


    【解决方案1】:

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

    • 使用 SSL 使用:
    导入 ssl 进口证明 导入 geopy.geocoders ctx = ssl.create_default_context(cafile=certifi.where()) geopy.geocoders.options.default_ssl_context = ctx
    • 禁用 SSL(不推荐,甚至可能不适用于某些地理提供商):
    导入 ssl 导入 geopy.geocoders ctx = ssl.create_default_context() ctx.check_hostname = 假 ctx.verify_mode = ssl.CERT_NONE geopy.geocoders.options.default_ssl_context = ctx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 2015-01-31
      • 1970-01-01
      • 2012-02-24
      • 2013-06-29
      • 2021-10-21
      • 2013-05-27
      相关资源
      最近更新 更多