// // EQNMapAnnotationUserReport.swift // Earthquake Network // // Created by Andrea Busi on 06/03/21. // Copyright © 2021 Earthquake Network. All rights reserved. // import Foundation import MapKit @objc class EQNMapAnnotationUserReport: NSObject, MKAnnotation { @objc var coordinate: CLLocationCoordinate2D @objc var title: String? @objc var subtitle: String? @objc var image: UIImage? @objc var timeDifference: String? var report: EQNSegnalazione? // MARK: - Init convenience init(report: EQNSegnalazione) { let coordinate = report.coordinate.coordinate let magnitude = report.intensity self.init(magnitude: magnitude, coordinate: coordinate) self.report = report self.timeDifference = EQNUtility.formattedTimeDifference(from: report.date) } @objc init( magnitude: Int, coordinate: CLLocationCoordinate2D ) { self.title = Self.title(for: magnitude) self.subtitle = Self.subtitle(for: magnitude) self.coordinate = coordinate self.image = Self.image(for: magnitude) } // MARK: - Helper private static func image(for magnitute: Int) -> UIImage? { guard let color = UIColor(named: "Mercalli \(magnitute)") else { print("[EQNMapAnnotationUserReport] Unable to get a color for magnitude: \(magnitute)") return nil } return UIImage.circle(diameter: EQNCustomAnnotationView.SmallHeight, color: color, borderWidth: 1.0, borderColor: .darkGray) } private static func title(for magnitude: Int) -> String { let grade = magnitude / 10 let roman = grade.toRoman() return String(format: NSLocalizedString("mercalli_intensity", comment: ""), roman) } private static func subtitle(for magnitude: Int) -> String { let grade = magnitude / 10 let roman = grade.toRoman() let string = NSLocalizedString("mercalli_\(roman)", comment: "") // strings are in the format like "II - Appena percepito" // so we are going to remove the "II" from the string let components = string.components(separatedBy: " - ") if components.count > 1 { return components[1] } return string } }