File size: 2,106 Bytes
7b3c9be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
from typing import Optional

import folium
import geopy


class MapGenerator:
    def __init__(self, timeout: int = 10):
        self.geocoder = geopy.Nominatim(user_agent="TheAmateur", timeout=timeout)
        self.line_coordinates = []

    def get_map(self, location1: str, location2: Optional[str] = None, error: Optional[float] = None, direction: Optional[str] = None):
        try:
            l1 = self.geocoder.geocode(location1)
            if not l1:
                print(f"Warning: Location not found: {location1}")
                return None

            if location2:
                l2 = self.geocoder.geocode(location2)
                if not l2:
                    print(f"Warning: Location not found: {location2}")
                    return None

                map_obj = folium.Map(location=[l2.latitude, l2.longitude], zoom_start=7)
                folium.Marker([l1.latitude, l1.longitude], popup='Loc 1', icon=folium.Icon(icon="cloud")).add_to(map_obj)
                folium.Marker([l2.latitude, l2.longitude], popup='Loc 2', icon=folium.Icon(color="green")).add_to(map_obj)

                if error and direction:
                    self.line_coordinates.extend([[l1.latitude, l1.longitude], [l2.latitude, l2.longitude]])
                    text = f"{error}km in {direction}"
                    folium.PolyLine(
                        locations=self.line_coordinates,
                        color='blue',
                        weight=5,
                        opacity=0.8,
                        tooltip=text
                    ).add_to(map_obj)
            else:
                map_obj = folium.Map(location=[l1.latitude, l1.longitude], zoom_start=10)
                folium.Marker([l1.latitude, l1.longitude], popup='Loc', icon=folium.Icon(icon="cloud")).add_to(map_obj)

            return map_obj
        except geopy.exc.GeocoderUnavailable as e:
            print(f"Error: Geocoding service is unavailable. {e}")
            return None
        except Exception as e:
            print(f"An unexpected error occurred in get_map(): {e}")
            return None