Files
eqn.ios/Sources/Earthquake Network/Controllers/Alerts/PasquakesMapViewController.swift
T

98 lines
3.6 KiB
Swift

//
// PasquakesMapViewController.swift
// Earthquake Network
//
// Created by Andrea Busi on 10/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import Foundation
import MapKit
class PasquakesMapViewController: EQNBaseMapViewController {
/// Pasquakes currently showned on the map
private var filteredPastquakes = [EQNPastquakes]()
override var availableFilters: [EQNFiltroMappa] {
[ .unMese, .unaSettimana, .unGiorno, .unOra ]
}
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
}
// MARK: - Public
override func registerMapAnnotationViews() {
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier)
}
override func loadDataSource() {
guard let list = EQNManager.manager().datiPastQuakes else { return }
// filter report based on selected filter
let filterDate = filter.date
filteredPastquakes = list.filter { $0.date > filterDate }
// create annotations to display on the map
let annotations = filteredPastquakes.compactMap { EQNMapAnnotationPastquake(pastquake: $0) }
// update map and center
updateMap(with: annotations)
}
override func centerMap() {
var centerLocation: CLLocation?
// Se c'è un rilevamento distante dall'utente meno del raggio di notifica,
// allora il centro è su quel rilevamento
if let userPosition = CLLocationManager().location {
let nearestPastquake = filteredPastquakes
.sorted(by: { (pastquake1, pastquake2) -> Bool in
abs(userPosition.distance(from: pastquake1.coordinate)) < abs(userPosition.distance(from: pastquake2.coordinate))
})
.first
// controlliamo che sia inferiore al raggio massimo impostato per le notifiche
if let radiusLow = Double(EQNAllertaSismica.shared().raggioSismiLievi),
let radiusStrong = Double(EQNAllertaSismica.shared().raggioSismiForti),
let nearestPastquake = nearestPastquake {
let radius = max(radiusLow, radiusStrong)
if abs(nearestPastquake.coordinate.distance(from: userPosition)) < radius {
centerLocation = nearestPastquake.coordinate
}
}
}
// altrimenti il centro è sul rilevamento più recente
if centerLocation == nil, let newestPastquake = filteredPastquakes.sorted(by: { $0.date > $1.date }).first {
centerLocation = newestPastquake.coordinate
}
if let centerLocation = centerLocation {
let span = MKCoordinateSpan(latitudeDelta: 8, longitudeDelta: 8)
let region = MKCoordinateRegion(center: centerLocation.coordinate, span: span)
mapView.setCenter(centerLocation.coordinate, animated: false)
mapView.setRegion(region, animated: true)
}
}
// MARK: - Map
override func setupAnnotationView(for annotation: MKAnnotation, on mapView: MKMapView) -> MKAnnotationView? {
guard let annotation = annotation as? EQNMapAnnotationPastquake else {
return nil
}
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView
annotationView.image = annotation.image
annotationView.title = annotation.title
return annotationView
}
}