Travel Planner with Map Integration

import tkinter as tk

from tkinter import messagebox

import requests

import folium

from geopy.geocoders import Nominatim

import webbrowser


# === API KEY ===

WEATHER_API_KEY = "YOUR_OPENWEATHER_API_KEY"


# === Core Functions ===


def get_coordinates(place):

    geolocator = Nominatim(user_agent="travel_planner")

    location = geolocator.geocode(place)

    if location:

        return (location.latitude, location.longitude)

    return None


def get_weather(city):

    url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={WEATHER_API_KEY}&units=metric"

    response = requests.get(url)

    data = response.json()

    if data.get("main"):

        temp = data["main"]["temp"]

        weather = data["weather"][0]["description"]

        return f"{temp}°C, {weather}"

    return "Not found"


def estimate_cost(destinations):

    return len(destinations) * 1500  # ₹1500 per destination (sample logic)


def show_map(destinations):

    if not destinations:

        messagebox.showerror("Error", "No destinations added!")

        return


    m = folium.Map(location=[20.5937, 78.9629], zoom_start=5)


    for city in destinations:

        coord = get_coordinates(city)

        if coord:

            weather = get_weather(city)

            folium.Marker(coord, tooltip=f"{city}: {weather}").add_to(m)

        else:

            messagebox.showwarning("Warning", f"Could not locate {city}")


    # Optional: Add route lines

    coords = [get_coordinates(city) for city in destinations if get_coordinates(city)]

    if len(coords) > 1:

        folium.PolyLine(coords, color="blue", weight=2.5).add_to(m)


    m.save("travel_map.html")

    webbrowser.open("travel_map.html")


# === GUI ===


destinations = []


def add_destination():

    city = city_entry.get()

    if city:

        destinations.append(city)

        city_listbox.insert(tk.END, city)

        city_entry.delete(0, tk.END)


def clear_destinations():

    destinations.clear()

    city_listbox.delete(0, tk.END)


def plan_trip():

    if not destinations:

        messagebox.showwarning("Empty", "Add destinations first.")

        return

    cost = estimate_cost(destinations)

    show_map(destinations)

    messagebox.showinfo("Trip Estimate", f"🗺️ Trip planned for {len(destinations)} places.\nEstimated Cost: ₹{cost}")


root = tk.Tk()

root.title("🧳 Travel Planner")

root.geometry("400x450")


tk.Label(root, text="Enter Destination City").pack(pady=5)

city_entry = tk.Entry(root, width=30)

city_entry.pack(pady=5)


tk.Button(root, text="➕ Add", command=add_destination).pack()

city_listbox = tk.Listbox(root, width=40)

city_listbox.pack(pady=10)


tk.Button(root, text="🗺️ Plan Trip", command=plan_trip).pack(pady=10)

tk.Button(root, text="❌ Clear All", command=clear_destinations).pack()


root.mainloop()


No comments: