@@ -38,22 +38,20 @@ class MapState: ObservableObject {
3838 @Published var showDFRS : Bool ?
3939
4040 @Published var searchResults : [ MKMapItem ] ?
41- }
42-
43- struct MarlinMap : UIViewRepresentable {
44- @Environment ( \. colorScheme) var colorScheme
4541
4642 @AppStorage ( " mapType " ) var mapType : Int = Int ( MKMapType . standard. rawValue)
4743 @AppStorage ( " showGARS " ) var showGARS : Bool = false
4844 @AppStorage ( " showMGRS " ) var showMGRS : Bool = false
4945 @AppStorage ( " showMapScale " ) var showMapScale = false
50-
46+ }
47+
48+ struct MarlinMap : UIViewRepresentable {
5149 @ObservedObject var mapState : MapState
5250
5351 var mixins : [ MapMixin ] ?
5452 var name : String
5553
56- init ( name: String , mixins: [ MapMixin ] ? = [ ] , mapState: MapState ? = nil , annotationToShrink : EnlargableAnnotation ? = nil , focusedAnnotation : AnnotationWithView ? = nil ) {
54+ init ( name: String , mixins: [ MapMixin ] ? = [ ] , mapState: MapState ? = nil ) {
5755 self . name = name
5856 if let mapState = mapState {
5957 self . mapState = mapState
@@ -86,12 +84,9 @@ struct MarlinMap: UIViewRepresentable {
8684 mapView. isPitchEnabled = false
8785 mapView. showsCompass = false
8886 mapView. tintColor = UIColor ( Color . primaryColorVariant)
87+ mapView. isAccessibilityElement = true
88+ mapView. accessibilityLabel = name
8989
90- if showMapScale {
91- let scale = MKScaleView ( mapView: mapView)
92- scale. scaleVisibility = . visible // always visible
93- mapView. addSubview ( scale)
94- }
9590 context. coordinator. mapView = mapView
9691
9792 mapView. register ( EnlargedAnnotationView . self, forAnnotationViewWithReuseIdentifier: EnlargedAnnotationView . ReuseID)
@@ -108,6 +103,27 @@ struct MarlinMap: UIViewRepresentable {
108103 func updateUIView( _ mapView: MKMapView , context: Context ) {
109104 print ( " Update ui view " )
110105 context. coordinator. mapView = mapView
106+
107+ let scale = context. coordinator. mapScale ?? mapView. subviews. first { view in
108+ return ( view as? MKScaleView ) != nil
109+ }
110+
111+ if mapState. showMapScale {
112+ if scale == nil {
113+ let scale = MKScaleView ( mapView: mapView)
114+ scale. scaleVisibility = . visible // always visible
115+ scale. isAccessibilityElement = true
116+ scale. accessibilityLabel = " Map Scale "
117+ mapView. addSubview ( scale)
118+ context. coordinator. mapScale = scale
119+ } else if let scale = scale {
120+ mapView. addSubview ( scale)
121+ }
122+ } else if let scale = scale {
123+ scale. removeFromSuperview ( )
124+ }
125+
126+ // TODO: I think this is not used anymore
111127 context. coordinator. updateDataSources ( fetchRequests: mapState. fetchRequests)
112128
113129 if let center = mapState. center, center. center. latitude != context. coordinator. setCenter? . latitude, center. center. longitude != context. coordinator. setCenter? . longitude {
@@ -147,22 +163,22 @@ struct MarlinMap: UIViewRepresentable {
147163 mapView. removeOverlays ( overlaysToRemove)
148164 mapView. addOverlays ( overlaysToAdd)
149165
150- if mapType == ExtraMapTypes . osm. rawValue {
166+ if mapState . mapType == ExtraMapTypes . osm. rawValue {
151167 if context. coordinator. osmOverlay == nil {
152168 context. coordinator. osmOverlay = MKTileOverlay ( urlTemplate: " https://osm.gs.mil/tiles/default/{z}/{x}/{y}.png " )
153169 context. coordinator. osmOverlay? . tileSize = CGSize ( width: 512 , height: 512 )
154170 context. coordinator. osmOverlay? . canReplaceMapContent = true
155171 }
156172 mapView. removeOverlay ( context. coordinator. osmOverlay!)
157173 mapView. insertOverlay ( context. coordinator. osmOverlay!, at: 0 , level: . aboveRoads)
158- } else if let mkmapType = MKMapType ( rawValue: UInt ( mapType) ) {
174+ } else if let mkmapType = MKMapType ( rawValue: UInt ( mapState . mapType) ) {
159175 mapView. mapType = mkmapType
160176 if let osmOverlay = context. coordinator. osmOverlay {
161177 mapView. removeOverlay ( osmOverlay)
162178 }
163179 }
164180
165- if showGARS {
181+ if mapState . showGARS {
166182 if context. coordinator. garsOverlay == nil {
167183 context. coordinator. garsOverlay = GARSTileOverlay ( 512 , 512 )
168184 }
@@ -173,7 +189,7 @@ struct MarlinMap: UIViewRepresentable {
173189 }
174190 }
175191
176- if showMGRS {
192+ if mapState . showMGRS {
177193 if context. coordinator. mgrsOverlay == nil {
178194 context. coordinator. mgrsOverlay = MGRSTileOverlay ( 512 , 512 )
179195 }
@@ -185,16 +201,6 @@ struct MarlinMap: UIViewRepresentable {
185201 }
186202 }
187203
188- func MKMapRectForCoordinateRegion( region: MKCoordinateRegion ) -> MKMapRect {
189- let topLeft = CLLocationCoordinate2D ( latitude: region. center. latitude + ( region. span. latitudeDelta/ 2 ) , longitude: region. center. longitude - ( region. span. longitudeDelta/ 2 ) )
190- let bottomRight = CLLocationCoordinate2D ( latitude: region. center. latitude - ( region. span. latitudeDelta/ 2 ) , longitude: region. center. longitude + ( region. span. longitudeDelta/ 2 ) )
191-
192- let a = MKMapPoint ( topLeft)
193- let b = MKMapPoint ( bottomRight)
194-
195- return MKMapRect ( origin: MKMapPoint ( x: min ( a. x, b. x) , y: min ( a. y, b. y) ) , size: MKMapSize ( width: abs ( a. x- b. x) , height: abs ( a. y- b. y) ) )
196- }
197-
198204 func makeCoordinator( ) -> MarlinMapCoordinator {
199205 return MarlinMapCoordinator ( self )
200206 }
@@ -208,13 +214,14 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
208214 var mgrsOverlay : MGRSTileOverlay ?
209215
210216 var mapView : MKMapView ?
217+ var mapScale : MKScaleView ?
211218 var marlinMap : MarlinMap
212219 var focusedAnnotation : EnlargableAnnotation ?
213220 var focusMapOnItemSink : AnyCancellable ?
214221
215222 var setCenter : CLLocationCoordinate2D ?
216223 var trackingModeSet : MKUserTrackingMode ?
217-
224+ // TODO: I think this is not used anymore
218225 var fetchedResultsControllers : [ String : NSFetchedResultsController < NSFetchRequestResult > ] = [ : ]
219226
220227 init ( _ marlinMap: MarlinMap ) {
@@ -228,7 +235,7 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
228235 self ? . focusItem ( notification: $0)
229236 } )
230237 }
231-
238+ // TODO: I think this is not used anymore
232239 func updateDataSources( fetchRequests: [ String : NSFetchRequest < NSFetchRequestResult > ] ) {
233240 for (key, fetchRequest) in fetchRequests {
234241 if let controller = fetchedResultsControllers [ key] {
@@ -246,7 +253,7 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
246253 }
247254 }
248255 }
249-
256+ // TODO: I think this is not used anymore
250257 func initiateFetchResultsController( fetchedResultsController: NSFetchedResultsController < NSFetchRequestResult > ? ) {
251258 fetchedResultsController? . delegate = self
252259 do {
@@ -264,12 +271,12 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
264271 func addAnnotation( annotation: MKAnnotation ) {
265272 mapView? . addAnnotation ( annotation)
266273 }
267-
274+ // TODO: I think this is not used anymore
268275 func updateAnnotation( annotation: MKAnnotation ) {
269276 mapView? . removeAnnotation ( annotation)
270277 mapView? . addAnnotation ( annotation)
271278 }
272-
279+ // TODO: I think this is not used anymore
273280 func deleteAnnotation( annotation: MKAnnotation ) {
274281 var mapAnnotation : MKAnnotation ?
275282 if let asam = annotation as? Asam {
@@ -308,6 +315,7 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
308315 UIView . animate ( withDuration: 0.5 , delay: 0.0 , options: . curveEaseInOut, animations: {
309316 focusedAnnotation. shrinkAnnotation ( )
310317 } ) { complete in
318+ print ( " xxx remove annotation " )
311319 self . mapView? . removeAnnotation ( focusedAnnotation)
312320 }
313321 self . focusedAnnotation = nil
@@ -328,16 +336,6 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
328336 mapView? . addAnnotation ( ea)
329337 }
330338
331- func MKMapRectForCoordinateRegion( region: MKCoordinateRegion ) -> MKMapRect {
332- let topLeft = CLLocationCoordinate2D ( latitude: region. center. latitude + ( region. span. latitudeDelta/ 2 ) , longitude: region. center. longitude - ( region. span. longitudeDelta/ 2 ) )
333- let bottomRight = CLLocationCoordinate2D ( latitude: region. center. latitude - ( region. span. latitudeDelta/ 2 ) , longitude: region. center. longitude + ( region. span. longitudeDelta/ 2 ) )
334-
335- let a = MKMapPoint ( topLeft)
336- let b = MKMapPoint ( bottomRight)
337-
338- return MKMapRect ( origin: MKMapPoint ( x: min ( a. x, b. x) , y: min ( a. y, b. y) ) , size: MKMapSize ( width: abs ( a. x- b. x) , height: abs ( a. y- b. y) ) )
339- }
340-
341339 @objc func singleTapGensture( tapGestureRecognizer: UITapGestureRecognizer ) {
342340 guard let mapGesture = tapGestureRecognizer as? MapSingleTap , let mapView = mapGesture. mapView else {
343341 return
@@ -487,14 +485,14 @@ class MarlinMapCoordinator: NSObject, MKMapViewDelegate, UIGestureRecognizerDele
487485 }
488486
489487}
490-
488+ // TODO: I think this is not used anymore
491489extension MarlinMapCoordinator : NSFetchedResultsControllerDelegate {
492490 func controller( _ controller: NSFetchedResultsController < NSFetchRequestResult > , didChange anObject: Any , at indexPath: IndexPath ? , for type: NSFetchedResultsChangeType , newIndexPath: IndexPath ? ) {
493491 guard let annotation = anObject as? MKAnnotation else {
494492 return
495493 }
496494 switch ( type) {
497-
495+
498496 case . insert:
499497 self . addAnnotation ( annotation: annotation)
500498 case . delete:
0 commit comments