show the way based on map in python

For showing direction based on a map in Python, you can use several libraries such as folium, geopy, and gmaps. Here is an example using geopy and folium:

main.py
from geopy.geocoders import Nominatim
import folium

# Create geolocator object
geolocator = Nominatim(user_agent="my-app")

# Get the location for the starting point
start_location = geolocator.geocode("New York City")

# Get the location for the destination point
end_location = geolocator.geocode("Los Angeles")

# Create a map object centered on the starting point
map = folium.Map(location=[start_location.latitude, start_location.longitude], zoom_start=5)

# Add a marker for the starting point
folium.Marker([start_location.latitude, start_location.longitude], popup="Starting point").add_to(map)

# Add a marker for the destination point
folium.Marker([end_location.latitude, end_location.longitude], popup="Destination point").add_to(map)

# Get the route between the starting and destination point
route = geolocator.route(start_location, end_location)

# Add the route to the map as a PolyLine
folium.PolyLine(locations=[(point.latitude, point.longitude) for point in route], color='blue').add_to(map)

# Display the map
map
1036 chars
30 lines

In this example, we use Nominatim from geopy to get the geolocation of the starting and destination points. Then, we use folium to create a map object and add markers for the two points. Finally, we use geolocator.route to get the route between the two points and add it to the map as a PolyLine. The result is a map showing the route between New York City and Los Angeles.

gistlibby LogSnag