Search code examples
pythongeopy

Geopy outputs NoneType


I wanted to get the latitude and longitude of an adress with geopy. My code is

geolocator = Nominatim(user_agent="EXAMPLE")
location = geolocator.geocode("Wismarsche Straße 393-397 19049 Schwerin")
lat = str(location.latitude)
lon = str(location.longitude)
latLon = str(lat) + ";" + str(lon) + ";"

and it outputs NoneType.

Does anyone know why?


Solution

  • The documentation states that the geocode method returns None when no results are found. In your case, removing 19049 from your query returns a result:

    location = geolocator.geocode("Wismarsche Straße 393-397 Schwerin")
    lat = str(location.latitude)
    lon = str(location.longitude)
    latLon = str(lat) + ";" + str(lon) + ";"
    
    print(f'Latitude: {lat}, longitude: {lon}, latLon: {latLon}')
    

    Ouput:

    Latitude: 53.6519479, longitude: 11.4073671, latLon: 53.6519479;11.4073671;