Search code examples
pythonfolium

Python Folium expected to values for location


I was trying to create a program that traces ip adresses and plots them on a map but got the error: ValueError: Expected two (lat, lon) values for location, instead got: []. i am using notepad to create the file and cmd to run it.

import geocoder
import folium

ip = input('Enter IP to trace: ')
g = geocoder.ip(ip)
location = g.latlng


locationmap1 = folium.Map(location=location, zoom_start=12)

folium.CircleMarker(location=location, radius=50).add_to(locationmap1)
locationmap1.save('locationmap1.html')
locationmap1.show_in_browser()

Solution

  • The following code snippet runs. The bug you are running into is because when you run geocoder.id on an invalid IP, g.latlng returns an empty array. My suggestion would be to check that once the location is available, ensure that it is valid by checking that it is valid.

    import geocoder
    import folium
    
    ip = "12.215.42.19"
    g = geocoder.ip(ip)
    location = g.latlng
    
    print(location) // [32.7831, -96.8067]
    
    locationmap1 = folium.Map(location=location, zoom_start=12)
    
    folium.CircleMarker(location=location, radius=50).add_to(locationmap1)