show location name in map in python

To show a location name on a map in Python, you first need to obtain the latitude and longitude coordinates of that location. This can be done using geocoding services provided by various providers such as Google, Bing, Mapbox, etc.

Here's an example using the geopy library to geocode a location:

main.py
from geopy.geocoders import Nominatim

geolocator = Nominatim(user_agent="my-app")

# Replace "New York City" with your desired location
location = geolocator.geocode("New York City")

# Extract the latitude and longitude
latitude = location.latitude
longitude = location.longitude

print(latitude, longitude)
# Output: 40.7127281 -74.0060152
343 chars
14 lines

Once you have the latitude and longitude, you can use the folium library to create a map and add a marker at that location:

main.py
import folium

# Create a map centered at the location
m = folium.Map(location=[latitude, longitude], zoom_start=12)

# Add a marker
folium.Marker([latitude, longitude], popup="New York City").add_to(m)

# Display the map
m
224 chars
11 lines

This will display a map centered at the location with a marker showing the location name. You can replace "New York City" with any location of your choice.

gistlibby LogSnag