[P] Improve marker removal architecture to allow for future extensions
This commit is contained in:
@@ -41,8 +41,6 @@ class MapViewApp(App):
|
||||
self.bump_markers: list[MapMarker] = []
|
||||
self.pothole_markers: list[MapMarker] = []
|
||||
|
||||
self.root_window.bind(on_touch_up = self.on_touch_up)
|
||||
|
||||
def on_start(self):
|
||||
"""
|
||||
Встановлює необхідні маркери, викликає функцію для оновлення мапи
|
||||
@@ -106,10 +104,8 @@ class MapViewApp(App):
|
||||
if user_id == config.TRACK_ID:
|
||||
self.mapview.center_on(lat, lon)
|
||||
|
||||
def map_lat_lon_to_pothole(self, lat: float, lon: float) -> MapMarker | None:
|
||||
click_tolerance = self.mapview.zoom * 10
|
||||
flt = filter(lambda marker: abs(lat - marker.lat) + abs(lon - marker.lon) < click_tolerance,
|
||||
self.pothole_markers)
|
||||
def map_lat_lon_to_marker(self, lat: float, lon: float) -> MapMarker | None:
|
||||
flt = filter(lambda marker: lon == marker.lat and lat == marker.lon, self.pothole_markers + self.bump_markers)
|
||||
return next(flt)
|
||||
|
||||
def set_pothole_marker(self, point):
|
||||
@@ -145,16 +141,28 @@ class MapViewApp(App):
|
||||
if lat is None or lon is None:
|
||||
return
|
||||
|
||||
pothole = self.map_lat_lon_to_pothole(lat, lon)
|
||||
if pothole:
|
||||
self.mapview.remove_marker(pothole)
|
||||
self.pothole_markers.pop(self.pothole_markers.index(pothole))
|
||||
clicked_marker_data = self.datasource.map_lat_lon_to_ProcessedAgentData(lat, lon)
|
||||
|
||||
def on_touch_up(self, touch):
|
||||
self.mapview.on_touch_up(touch)
|
||||
if not clicked_marker_data:
|
||||
return
|
||||
|
||||
clicked_marker = self.map_lat_lon_to_marker(clicked_marker_data.latitude, clicked_marker_data.longitude)
|
||||
|
||||
self.mapview.remove_marker(clicked_marker)
|
||||
|
||||
pothole_index = self.pothole_markers.index(clicked_marker)
|
||||
bump_index = self.bump_markers.index(clicked_marker)
|
||||
|
||||
if pothole_marker >= 0:
|
||||
self.pothole_markers.pop(pothole_index)
|
||||
elif bump_index >= 0:
|
||||
self.bump_markers.pop(bump_index)
|
||||
|
||||
def on_touch_down(self, widget, touch):
|
||||
if touch.button == "right":
|
||||
coordinate = self.mapview.get_latlon_at(touch.x, touch.y, self.mapview.zoom)
|
||||
self.delete_pothole_marker(coordinate)
|
||||
return True
|
||||
|
||||
|
||||
def build(self):
|
||||
@@ -168,6 +176,8 @@ class MapViewApp(App):
|
||||
lon=30.5234
|
||||
)
|
||||
|
||||
self.mapview.bind(on_touch_down = self.on_touch_down)
|
||||
|
||||
return self.mapview
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user