Created
October 30, 2018 16:02
-
-
Save FabiolaRamirez/ed6da17b490a8d2f4b6d30e182693bdc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // MapView.swift | |
| // finance | |
| // | |
| // Created by Fabiola Ramirez on 10/18/18. | |
| // Copyright © 2018 creditsesame. All rights reserved. | |
| // | |
| import UIKit | |
| import MapKit | |
| protocol AlertPointDelegate { | |
| func selectAlertPoint(mapPoint: MapPoint, alertType: AlertLocationType) | |
| } | |
| class MapView: MKMapView, MKMapViewDelegate { | |
| var alertPointDelegate: AlertPointDelegate? | |
| let localSearchRequest = MKLocalSearchRequest() | |
| var points: [MapPoint] = [] { | |
| didSet { | |
| getCoordinates() | |
| } | |
| } | |
| var coordinates: [CLLocationCoordinate2D?] = [] | |
| var alertType: AlertLocationType = .ssnTracePremium | |
| var isDetail = false | |
| var fromMainScreen = false | |
| override init(frame: CGRect) { | |
| super.init(frame: frame) | |
| self.delegate = self | |
| } | |
| required init?(coder aDecoder: NSCoder) { | |
| super.init(coder: aDecoder) | |
| self.delegate = self | |
| } | |
| func getCoordinates() { | |
| coordinates = [] | |
| let queue = OperationQueue() | |
| let operation1 = BlockOperation { | |
| let geoCoder = CLGeocoder() | |
| for mapPoint in self.points { | |
| if let location = CurrentAlert.shared.locations[mapPoint.address] { | |
| self.coordinates.append(location) | |
| // dispatchGroup.leave() | |
| } else { | |
| let dispatchGroup = DispatchGroup() | |
| dispatchGroup.enter() | |
| geoCoder.geocodeAddressString(mapPoint.address) { (placemarks, error) in | |
| if let placemarks = placemarks, let location = placemarks.first?.location { | |
| CurrentAlert.shared.locations[mapPoint.address] = CLLocationCoordinate2D(latitude: location.coordinate.latitude , longitude: location.coordinate.longitude) | |
| self.coordinates.append(CLLocationCoordinate2D(latitude: location.coordinate.latitude , longitude: location.coordinate.longitude)) | |
| } else { | |
| self.coordinates.append(nil) | |
| } | |
| dispatchGroup.leave() | |
| } | |
| dispatchGroup.wait() | |
| } | |
| } | |
| } | |
| let completionOperation = BlockOperation { | |
| self.drawPoints() | |
| if self.fromMainScreen { | |
| switch self.alertType { | |
| case .ssnTracePremium: | |
| if let currentPoint = CurrentAlert.shared.locations[(CurrentAlert.shared.currentSSNTraceAlertPoint?.address)!] { | |
| let userCenter = CLLocationCoordinate2D(latitude: currentPoint.latitude , longitude: currentPoint.longitude) | |
| let region = MKCoordinateRegion(center: userCenter, span: MKCoordinateSpan(latitudeDelta: 0.012, longitudeDelta: 0.012)) | |
| self.setRegion(region, animated: true) | |
| } | |
| case .sexOffendersPremium: | |
| if let currentPoint = CurrentAlert.shared.locations[(CurrentAlert.shared.currentSexOffenderAlertPoint?.address)!] { | |
| let userCenter = CLLocationCoordinate2D(latitude: currentPoint.latitude, longitude: currentPoint.longitude) | |
| let region = MKCoordinateRegion(center: userCenter, span: MKCoordinateSpan(latitudeDelta: 0.012, longitudeDelta: 0.012)) | |
| self.setRegion(region, animated: true) | |
| } | |
| default: | |
| self.showAnnotations(self.annotations, animated: true) | |
| } | |
| } else { | |
| self.showAnnotations(self.annotations, animated: true) | |
| } | |
| } | |
| completionOperation.addDependency(operation1) | |
| queue.addOperations([operation1, completionOperation], waitUntilFinished: false) | |
| } | |
| func drawPoints() { | |
| self.removeAnnotations(self.annotations) | |
| for (index, mapPoint) in points.enumerated() { | |
| if let coordinate = coordinates[index] { | |
| let alertAnnotationView = AlertAnnotationView() | |
| alertAnnotationView.coordinate = coordinate | |
| alertAnnotationView.mapPoint = mapPoint | |
| alertAnnotationView.position = index | |
| self.addAnnotation(alertAnnotationView) | |
| } | |
| } | |
| /*if let coordinate = CurrentAlert.shared.locations[(CurrentAlert.shared.currentSSNTraceAlertPoint?.address)!] { | |
| let alertAnnotationView = AlertAnnotationView() | |
| alertAnnotationView.coordinate = coordinate | |
| alertAnnotationView.mapPoint = CurrentAlert.shared.currentSSNTraceAlertPoint | |
| alertAnnotationView.position = 0 | |
| self.addAnnotation(alertAnnotationView) | |
| }*/ | |
| } | |
| func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { | |
| if let annotation = view.annotation as? AlertAnnotationView { | |
| //change pin color | |
| alertPointDelegate?.selectAlertPoint(mapPoint: annotation.mapPoint!, alertType: self.alertType) | |
| /*let selectedAnnotation = annotation | |
| var image: UIImage? = nil | |
| image = UIImage(named: "pin_selected") | |
| let resizedImage = UIGraphicsGetImageFromCurrentImageContext() | |
| annotationView.image = resizedImage*/ | |
| /*let annotationView = MKAnnotationView() | |
| var image: UIImage? = nil | |
| image = UIImage(named: "pin_selected") | |
| let resizedImage = UIGraphicsGetImageFromCurrentImageContext() | |
| annotationView.image = resizedImage*/ | |
| } | |
| } | |
| func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { | |
| let annotationView = MKAnnotationView() | |
| if let annotation = annotation as? AlertAnnotationView { | |
| annotationView.canShowCallout = false | |
| var image: UIImage? = nil | |
| var label: String = "" | |
| if let _ = annotation.mapPoint?.alert { | |
| image = UIImage(named: iconForAlertType(alertType, annotation: annotation)) | |
| label = isDetail ? "" : String(annotation.position + 1) | |
| } else { | |
| image = UIImage(named: "pin") | |
| label = "" | |
| } | |
| let size = CGSize(width: 34, height: 34) | |
| UIGraphicsBeginImageContext(size) | |
| image?.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) | |
| let resizedImage = UIGraphicsGetImageFromCurrentImageContext() | |
| annotationView.image = resizedImage | |
| let numberLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 34, height: 30)) | |
| numberLabel.textAlignment = .center | |
| numberLabel.textColor = UIColor.white | |
| numberLabel.font = UIFont.latoMedium(13) | |
| numberLabel.text = label | |
| annotationView.addSubview(numberLabel) | |
| annotationView.centerOffset = CGPoint(x: 0, y: -17) | |
| } | |
| return annotationView | |
| } | |
| func iconForAlertType(_ alertType: AlertLocationType, annotation: AlertAnnotationView) -> String { | |
| if isDetail { | |
| return "pin" | |
| } | |
| switch alertType { | |
| case .ssnTracePremium: | |
| if let current = CurrentAlert.shared.currentSSNTraceAlertPoint, annotation.mapPoint! == current { | |
| return "pin_selected" | |
| } | |
| case .sexOffendersPremium: | |
| if let current = CurrentAlert.shared.currentSexOffenderAlertPoint, annotation.mapPoint! == current { | |
| return "pin_selected" | |
| } | |
| default: | |
| break | |
| } | |
| return "pin_unselected" | |
| } | |
| } | |
| class AlertAnnotationView: MKPointAnnotation { | |
| var mapPoint: MapPoint? | |
| var position: Int = 0 | |
| var image: UIImage? | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment