Created
November 13, 2018 20:27
-
-
Save FabiolaRamirez/8ff0d73a277a4d07ee87e9a10b1a7593 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 lastSelectedAnnotationView: MKAnnotationView? | |
| 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) | |
| if exist(coordinates: self.coordinates as! [CLLocationCoordinate2D], elementLatitude: location.coordinate.latitude, elementLongitude: location.coordinate.longitude) { | |
| self.coordinates.append(nil) | |
| } else { | |
| self.coordinates.append(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() | |
| } | |
| } | |
| } | |
| func exist(coordinates: [CLLocationCoordinate2D], elementLatitude: Double, elementLongitude: Double) -> Bool { | |
| for coordinate in coordinates { | |
| if coordinate.latitude == elementLatitude && coordinate.longitude == elementLongitude { | |
| print("elementLatitude: \(elementLatitude)") | |
| print("elementLongitude: \(elementLongitude)") | |
| return true | |
| } | |
| } | |
| return false | |
| } | |
| let completionOperation = BlockOperation { | |
| DispatchQueue.main.async { | |
| self.drawPoints() | |
| if self.fromMainScreen { | |
| var currentPoint: CLLocationCoordinate2D? | |
| switch self.alertType { | |
| case .ssnTracePremium: | |
| if let address = CurrentAlert.shared.currentSSNTraceAlertPoint?.address { | |
| currentPoint = CurrentAlert.shared.locations[address] | |
| } | |
| case .sexOffendersPremium: | |
| if let address = CurrentAlert.shared.currentSexOffenderAlertPoint?.address { | |
| currentPoint = CurrentAlert.shared.locations[address] | |
| } | |
| case .changeOfAddressPremium: | |
| if let address = CurrentAlert.shared.currentChangeOfAddressPoint?.address { | |
| currentPoint = CurrentAlert.shared.locations[address] | |
| } | |
| } | |
| if let currentPoint = currentPoint { | |
| 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: false) | |
| } | |
| } else { | |
| self.showAnnotations(self.annotations, animated: false) | |
| } | |
| } | |
| } | |
| 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) | |
| } | |
| } | |
| } | |
| func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { | |
| if let annotation = view.annotation as? AlertAnnotationView { | |
| alertPointDelegate?.selectAlertPoint(mapPoint: annotation.mapPoint!, alertType: self.alertType) | |
| } | |
| view.image = UIImage(named: "pin_selected") | |
| lastSelectedAnnotationView?.image = UIImage(named: "pin_unselected") | |
| lastSelectedAnnotationView = view | |
| } | |
| func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { | |
| let annotationView = MKAnnotationView() | |
| if let annotation = annotation as? AlertAnnotationView { | |
| if annotation.position == 0 { | |
| lastSelectedAnnotationView = annotationView | |
| } | |
| 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 = "" | |
| } | |
| annotationView.image = image | |
| let numberLabel = UILabel(frame: CGRect(x: 0, y: 2, width: 34, height: 23)) | |
| 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" | |
| } | |
| var current: MapPoint? | |
| switch alertType { | |
| case .ssnTracePremium: | |
| current = CurrentAlert.shared.currentSSNTraceAlertPoint | |
| case .sexOffendersPremium: | |
| current = CurrentAlert.shared.currentSexOffenderAlertPoint | |
| case .changeOfAddressPremium: | |
| current = CurrentAlert.shared.currentChangeOfAddressPoint | |
| } | |
| if let current = current, annotation.mapPoint! == current { | |
| return "pin_selected" | |
| } | |
| return "pin_unselected" | |
| } | |
| } | |
| class AlertAnnotationView: MKPointAnnotation { | |
| var mapPoint: MapPoint? | |
| var position: Int = 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment