From 45f19a753302cc071c28cd92fab941cfdd0d1091 Mon Sep 17 00:00:00 2001 From: Andrea Busi Date: Tue, 23 Mar 2021 19:09:57 +0100 Subject: [PATCH] feat: Add circles in map detail --- ...ismicNetworksMapDetailViewController.swift | 45 ++++++++++++++++++- Sources/Earthquake Network/Models/EQNSisma.m | 16 +++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift index b5b2f2f..608347b 100644 --- a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift +++ b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift @@ -60,6 +60,8 @@ class SeismicNetworksMapDetailViewController: EQNBaseMapViewController { private let seismic: EQNSisma private var allSeismics: [EQNSisma] + /// Contains circles drawed on the map + private var mapCircles = [MKCircle]() // MARK: - Init @@ -88,6 +90,15 @@ class SeismicNetworksMapDetailViewController: EQNBaseMapViewController { let annotations = allSeismics.map { EQNMapAnnotationSeismic(seismic: $0) } updateMap(with: annotations) + + // if the given seismic is still in the data source, show circles + // otherwise just remove any other circles already on the map + if allSeismics.contains(seismic) { + addCircles(for: seismic.coordinate) + } else { + addCircles(for: nil) + } + loadFiltersRecap() } @@ -125,7 +136,28 @@ class SeismicNetworksMapDetailViewController: EQNBaseMapViewController { seismicsFilterLabel.text = recap } - + private func addCircles(for location: CLLocation?) { + // remove any previous circles + mapView.removeOverlays(mapCircles) + mapCircles.removeAll() + + // aggiungiamo 3 cerchi concentrici per il punto specificato (se disponibile) + // li inseriamo a a 50, 100 e 200 km. + guard let location = location else { return } + + let circles = [ + MKCircle(center: location.coordinate, radius: 25_000), + MKCircle(center: location.coordinate, radius: 100_000), + MKCircle(center: location.coordinate, radius: 200_000) + ] + + // !!note: is important to assign here the circles + // otherwise `addOverlays` will not work + mapCircles = circles + + // creo nuovi cerchi + mapView.addOverlays(circles) + } // MARK: - Actions @@ -154,6 +186,17 @@ class SeismicNetworksMapDetailViewController: EQNBaseMapViewController { return annotationView } + + func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { + guard let circleOverlay = overlay as? MKCircle else { + return MKOverlayRenderer(overlay: overlay) + } + + let circle = MKCircleRenderer(overlay: circleOverlay) + circle.strokeColor = AppTheme.Colors.darkGray + circle.lineWidth = 1.0 + return circle + } } extension SeismicNetworksMapDetailViewController: SeismicFiltersViewControllerDelegate { diff --git a/Sources/Earthquake Network/Models/EQNSisma.m b/Sources/Earthquake Network/Models/EQNSisma.m index 0ee6bda..d1d78ff 100644 --- a/Sources/Earthquake Network/Models/EQNSisma.m +++ b/Sources/Earthquake Network/Models/EQNSisma.m @@ -115,5 +115,21 @@ return self; } +#pragma mark - Equatable + +- (BOOL)isEqual:(id)other +{ + if ([other isKindOfClass:[EQNSisma class]]) { + EQNSisma *otherSisma = (EQNSisma *)other; + return [otherSisma.coordinate isEqual:self.coordinate] && otherSisma.intensity.doubleValue == self.intensity.doubleValue; + } + return NO; +} + +- (NSUInteger)hash +{ + return [self.coordinate hash]; +} + @end