@@ -22,6 +22,7 @@ struct CustomMap: UIViewRepresentable {
2222 @Binding var isPresented : Bool
2323 @StateObject var locationManagerDelegate = LocationManagerDelegate ( )
2424
25+
2526 @State var lineCoordinates : [ CLLocationCoordinate2D ] = [ ]
2627
2728 var contextualInfo : ( ( String ) -> Void ) ?
@@ -41,7 +42,7 @@ struct CustomMap: UIViewRepresentable {
4142
4243 // Updates the UIView with new data
4344 func updateUIView( _ mapView: MKMapView , context: Context ) {
44- mapView. setCenter ( userLocation, animated: true )
45+ // mapView.setCenter(userLocation, animated: true)
4546 context. coordinator. updateUserRegion ( mapView)
4647 manageAnnotations ( mapView, context: context)
4748
@@ -148,7 +149,6 @@ struct CustomMap: UIViewRepresentable {
148149 // Handles selection of annotations
149150 func mapView( _ mapView: MKMapView , didSelect annotation: MKAnnotation ) {
150151 print ( " did select " )
151- print ( annotation)
152152 if let annotation = annotation as? MKClusterAnnotation {
153153 let displayAnnotation = annotation. memberAnnotations. first as! DisplayUnitAnnotation
154154 selectedAnAnnotation ( selectedQuest: displayAnnotation)
@@ -177,7 +177,6 @@ struct CustomMap: UIViewRepresentable {
177177 }
178178 }
179179 self . parent. lineCoordinates = polylineCoords
180- print ( " POLYLINE COORDS ARE ---- \( polylineCoords) " )
181180
182181 parent. inferStreetName ( location: annotationLocation) { streetName in
183182 if let streetName = streetName {
@@ -222,34 +221,29 @@ struct CustomMap: UIViewRepresentable {
222221
223222 // Helper method to manage annotations
224223 private func manageAnnotations( _ mapView: MKMapView , context: Context ) {
225- /// Extracting coordinates of existing annotations
224+
226225 let existingCoordinates = mapView. annotations. compactMap { ( $0 as? DisplayUnitAnnotation ) ? . coordinate }
227- let newAnnotations = items. map { $0. annotation }
228- let newCoordinates = newAnnotations. map { $0. coordinate }
229- /// Checking if the coordinates of existing annotations are different from the coordinates of new annotations
230- if existingCoordinates. count != newCoordinates. count {
231- /// to reset isRegionSet value whenever region changes.
232- context. coordinator. isRegionSet = false
233- /// Removing annotations that are not present in the new set
234- let annotationsToRemove = mapView. annotations. filter {
235- guard let displayUnitAnnotation = $0 as? DisplayUnitAnnotation else { return false }
236- return !newCoordinates. contains ( displayUnitAnnotation. coordinate)
237- }
238- mapView. removeAnnotations ( annotationsToRemove)
239- /// Adding annotations that are present in the new set but not in the existing set
240- let annotationsToAdd = newAnnotations. filter { !existingCoordinates. contains ( $0. coordinate) }
241- mapView. addAnnotations ( annotationsToAdd)
242-
243- /// Updating the region if it hasn't been set yet
244- if !context. coordinator. isRegionSet {
245- /// resetting region only when app is launched/re-launched
246- if existingCoordinates. count == 0 {
247- mapView. setCenter ( userLocation, animated: true )
248- // mapView.setRegion(region, animated: true)
249- }
250- context. coordinator. isRegionSet = true
251- }
226+
227+ /// resetting region only when app is launched/re-launched
228+ if existingCoordinates. count == 0 {
229+ mapView. setCenter ( userLocation, animated: true )
252230 }
231+ context. coordinator. isRegionSet = true
232+
233+
234+ if ( existingCoordinates. isEmpty) {
235+ print ( " Adding annotations completely " )
236+ mapView. addAnnotations ( items. compactMap ( { $0. annotation} ) )
237+ }
238+
239+ let currentAnnotations = Set ( mapView. annotations. compactMap { $0 as? DisplayUnitAnnotation } )
240+ let newAnnotations = Set ( items. map ( { $0. annotation} ) )
241+
242+ let annotationsToRemove = currentAnnotations. subtracting ( newAnnotations)
243+ let annotationsToAdd = newAnnotations. subtracting ( currentAnnotations)
244+
245+ mapView. removeAnnotations ( Array ( annotationsToRemove) )
246+ mapView. addAnnotations ( Array ( annotationsToAdd) )
253247 }
254248
255249 // calculate distance between user current location and selected annotation
0 commit comments