From e161dfe9d4058cf61cbac644828583ee8ba20b74 Mon Sep 17 00:00:00 2001 From: Kevin Smith Date: Wed, 21 Jan 2026 19:37:51 -0500 Subject: [PATCH] Fix map rendering and add error handling - Fix Zoom_start to zoom_start (case-sensitive parameter) which caused blank map - Add error handling for invalid phone numbers and failed geocoding - Auto-add + prefix for numbers starting with country code digit - Add US as fallback region for phone number parsing - Fix open_map to use absolute file path - Add user feedback for errors instead of silent failures - Add comment clarifying API key must be kept in quotes --- main.py | 79 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 21 deletions(-) diff --git a/main.py b/main.py index 8041fec..ef3f4b8 100644 --- a/main.py +++ b/main.py @@ -11,31 +11,68 @@ root.geometry("385x594+300+200") root.resizable(False,False) root.configure(bg='#96BFFF') -key="get your free api key from openCage website" +key='YOUR_OPENCAGE_API_KEY' # Get your free API key at https://opencagedata.com/ (keep API key in quotes, do not remove quotes) def track(): - enter_nb=entry.get() - number=phonenumbers.parse(enter_nb) - - location=geocoder.description_for_number(number,'en') - country.config(text=location) - - service=carrier.name_for_number(number,'en') - sim.config(text=service) - - query = str(location) - results = OpenCageGeocode(key).geocode(query) - - lat = results[0]['geometry']['lat'] - lng = results[0]['geometry']['lng'] - - myMap = folium.Map(location=[lat, lng], Zoom_start=9) - - folium.Marker([lat, lng], popup=location).add_to(myMap) - myMap.save("myLocation.html") + try: + enter_nb = entry.get().strip() + if not enter_nb: + country.config(text="Enter a number") + return + + # Clean up the input - add + if it starts with a digit (country code) + if enter_nb[0].isdigit(): + enter_nb = '+' + enter_nb + + # Parse phone number with US as fallback region for numbers without country code + try: + number = phonenumbers.parse(enter_nb, None) + except phonenumbers.NumberParseException: + # Try with US as default region + number = phonenumbers.parse(enter_nb, 'US') + + # Get location description + location = geocoder.description_for_number(number, 'en') + if not location: + country.config(text="Location not found") + sim.config(text="Unknown") + return + country.config(text=location) + + # Get carrier/SIM info + service = carrier.name_for_number(number, 'en') + sim.config(text=service if service else "Unknown") + + # Geocode the location to get coordinates + query = str(location) + results = OpenCageGeocode(key).geocode(query) + + if not results: + country.config(text="Could not geocode") + return + + lat = results[0]['geometry']['lat'] + lng = results[0]['geometry']['lng'] + + # Create map with correct parameter name (zoom_start, not Zoom_start) + myMap = folium.Map(location=[lat, lng], zoom_start=9) + folium.Marker([lat, lng], popup=location).add_to(myMap) + myMap.save("myLocation.html") + + except phonenumbers.NumberParseException: + country.config(text="Invalid number format") + sim.config(text="Use +country code") + except Exception as e: + country.config(text="Error occurred") + print(f"Error: {e}") def open_map(): - webbrowser.open("myLocation.html") + import os + file_path = os.path.abspath("myLocation.html") + if os.path.exists(file_path): + webbrowser.open('file://' + file_path) + else: + country.config(text="Track a number first") logo =PhotoImage(file="logo.png") Label(root,image=logo,bg="#96BFFF").place(x=135,y=40)