Guest User

Untitled

a guest
Mar 14th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.32 KB | None | 0 0
  1. //
  2. // MapVC.swift
  3. // AddaIOS
  4. //
  5. // Created by Alif on 26/12/2017.
  6. // Copyright © 2017 Alif. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10. import MapKit
  11.  
  12. class MapVC: UIViewController {
  13.  
  14. // MARK: - IBOutlets
  15. @IBOutlet weak var mapView: MKMapView!
  16. let locationManager = CLLocationManager()
  17.  
  18. // var mapView: MKMapView!
  19. // var locationManager = CLLocationManager()
  20.  
  21. // MARK: - Properties
  22. var locationPin = MKPointAnnotation()
  23. var userLocations = [String: UserAnnotation]()
  24. lazy var currentUserId = PerfectLocalAuth.userid
  25. lazy var username = PerfectLocalAuth.realname()
  26. lazy var age = 10 // PerfectLocalAuth
  27. lazy var gender = "Male" // PerfectLocalAuth
  28.  
  29. // RealtimeLocationTracker
  30. private var realTimeLocationTrackerService = RealTimeLocationTracker.shared
  31.  
  32. lazy var currenUserId = PerfectLocalAuth.userid
  33.  
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. // createMapView()
  37. }
  38.  
  39. deinit {
  40. realTimeLocationTrackerService.disconnect()
  41. print("deinit from MapVC")
  42. }
  43.  
  44. // override func viewWillDisappear(_ animated: Bool) {
  45. // super.viewDidLoad()
  46. // realTimeLocationTrackerService.disconnect()
  47. // mapView.removeAnnotation()
  48. // }
  49.  
  50. override func viewWillAppear(_ animated: Bool) {
  51. realTimeLocationTrackerService.addListener(self)
  52. realTimeLocationTrackerService.connect()
  53. }
  54.  
  55. override func viewDidAppear(_ animated: Bool) {
  56. determineCurrentLocation()
  57. }
  58.  
  59. override func didReceiveMemoryWarning() {
  60. super.didReceiveMemoryWarning()
  61. // Dispose of any resources that can be recreated.
  62. }
  63.  
  64. func createMapView() {
  65. mapView = MKMapView()
  66.  
  67. let leftMargin: CGFloat = 0
  68. let topMargin: CGFloat = 60
  69. let mapWidth: CGFloat = view.frame.size.width
  70. let mapHeight: CGFloat = 700
  71.  
  72. mapView.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)
  73.  
  74. mapView.mapType = MKMapType.standard
  75. mapView.showsCompass = true
  76. mapView.showsScale = true
  77. mapView.isZoomEnabled = true
  78. mapView.isScrollEnabled = true
  79. mapView.center = view.center
  80. view.addSubview(mapView)
  81.  
  82. }
  83.  
  84. func determineCurrentLocation() {
  85. locationManager.delegate = self
  86. mapView.delegate = self
  87. mapView.showsUserLocation = true
  88.  
  89. if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
  90. locationManager.startUpdatingLocation()
  91. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  92.  
  93. } else {
  94.  
  95. locationManager.requestWhenInUseAuthorization()
  96. }
  97. }
  98.  
  99. func createAlertAndGotoChatViewController(title: String, msg: String) {
  100. let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
  101.  
  102. alert.addAction(UIAlertAction(title: "StartChat", style: .default, handler: { (action) in
  103. guard let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "chat") as? PrivateViewController else { return }
  104. self.present(vc, animated: true, completion: nil)
  105. }))
  106.  
  107. alert.addAction(UIAlertAction(title: "Cancel", style: .default))
  108.  
  109. self.present(alert, animated: true, completion: nil)
  110. }
  111.  
  112. }
  113.  
  114. // MARK: - Socket functions
  115.  
  116. extension MapVC: RealTimeLocationTrackerEventsListener {
  117.  
  118. func realTimeLocationTrackerDidConnect(_ realTimeLocationTrackerService: RealTimeLocationTracker) {
  119. print("realTimeLocationTrackerDidConnect")
  120.  
  121. mapView.zoomToUserLocation()
  122.  
  123. // let request = LocationRequest(currenUserId: currenUserId, latitude: nil, longitude: nil)
  124. let request = LocationRequest(currenUserId: currenUserId, username: "", gender: "", age: 0, latitude: "", longitude: "", cmd: "trackRegister")
  125. self.realTimeLocationTrackerService.sendLocationRequest(request)
  126.  
  127. }
  128.  
  129. func realTimeLocationTrackerDidDisconnect(_ realTimeLocationTrackerService: RealTimeLocationTracker) {
  130. print("realTimeLocationTrackerDidDisconnect")
  131. }
  132.  
  133. func realTimeLocationTrackerDidRecieveError(_ realTimeLocationTrackerService: RealTimeLocationTracker, didRecieveError: Error) {
  134. print("realTimeLocationTrackerDidRecieveError", didRecieveError)
  135. }
  136.  
  137. func realTimeLocationTrackerDidReceiveMessage(_ realTimeLocationTrackerService: RealTimeLocationTracker, didRecieveMessage text: String) {
  138. print("realTimeLocationTrackerDidReceiveMessage", text)
  139.  
  140. guard let userCordinate = ReceiveUserCordinate.from(json: text) else { return }
  141.  
  142. let username = userCordinate.username
  143. let gender = userCordinate.gender
  144. let age = userCordinate.age
  145.  
  146. guard
  147. let latitude = Double(userCordinate.latitude),
  148. let longitude = Double(userCordinate.longitude)
  149. else { return }
  150.  
  151. let user = userCordinate.currenUserId
  152. let uc = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
  153.  
  154. if let annotation = userLocations[user] {
  155. annotation.coordinate = uc
  156. } else {
  157. let annotation = UserAnnotation(username: username, gender: gender, age: age, coordinate: uc)
  158. userLocations[user] = annotation
  159. mapView.addAnnotations([annotation])
  160. }
  161.  
  162. // Remove Annotony when user goes offline
  163. // The server should send a different message when a user goes offline (edited)
  164. // And then you use `if let` to find the annotation of the user, remove it from the dictionary and from the map
  165.  
  166. }
  167.  
  168. }
  169.  
  170. extension MapVC: CLLocationManagerDelegate {
  171. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  172. }
  173.  
  174. func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  175. if status == .authorizedAlways {
  176. mapView.showsUserLocation = true
  177. mapView.userTrackingMode = .follow
  178. }
  179. }
  180.  
  181. func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) {
  182. print(error)
  183. }
  184.  
  185. }
  186.  
  187. extension MapVC: MKMapViewDelegate {
  188. func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
  189.  
  190. let center = userLocation.coordinate
  191.  
  192. // Sending my Location
  193.  
  194. let request = LocationRequest(
  195. currenUserId: currenUserId,
  196. username: username,
  197. gender: gender,
  198. age: age,
  199. latitude: "\(center.latitude)",
  200. longitude: "\(center.longitude)",
  201. cmd: ""
  202. )
  203.  
  204. self.realTimeLocationTrackerService.sendLocationRequest(request)
  205.  
  206. // Current userlocation coordinate
  207. let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
  208. let region = MKCoordinateRegion(center: center, span: span)
  209. mapView.setRegion(region, animated: true)
  210. }
  211.  
  212. func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
  213. let identifier = "UserAnnotation"
  214.  
  215. if annotation is UserAnnotation {
  216. var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
  217.  
  218. if annotationView == nil {
  219. annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
  220. annotationView!.canShowCallout = true
  221.  
  222. let btn = UIButton(type: .detailDisclosure)
  223. annotationView!.rightCalloutAccessoryView = btn
  224.  
  225. } else {
  226. annotationView!.annotation = annotation
  227. }
  228.  
  229. return annotationView
  230. }
  231.  
  232. return nil
  233. }
  234.  
  235. func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
  236. guard let userAnnotation = view.annotation as? UserAnnotation else { return }
  237. guard let name = userAnnotation.username else { return }
  238.  
  239. // let placeInfo = userAnnotation.info
  240.  
  241. createAlertAndGotoChatViewController(title: name, msg: userAnnotation.gender)
  242. }
  243. }
Add Comment
Please, Sign In to add comment