Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MapVC.swift
- // AddaIOS
- //
- // Created by Alif on 26/12/2017.
- // Copyright © 2017 Alif. All rights reserved.
- //
- import UIKit
- import MapKit
- class MapVC: UIViewController {
- // MARK: - IBOutlets
- @IBOutlet weak var mapView: MKMapView!
- let locationManager = CLLocationManager()
- // var mapView: MKMapView!
- // var locationManager = CLLocationManager()
- // MARK: - Properties
- var locationPin = MKPointAnnotation()
- var userLocations = [String: UserAnnotation]()
- lazy var currentUserId = PerfectLocalAuth.userid
- lazy var username = PerfectLocalAuth.realname()
- lazy var age = 10 // PerfectLocalAuth
- lazy var gender = "Male" // PerfectLocalAuth
- // RealtimeLocationTracker
- private var realTimeLocationTrackerService = RealTimeLocationTracker.shared
- lazy var currenUserId = PerfectLocalAuth.userid
- override func viewDidLoad() {
- super.viewDidLoad()
- // createMapView()
- }
- deinit {
- realTimeLocationTrackerService.disconnect()
- print("deinit from MapVC")
- }
- // override func viewWillDisappear(_ animated: Bool) {
- // super.viewDidLoad()
- // realTimeLocationTrackerService.disconnect()
- // mapView.removeAnnotation()
- // }
- override func viewWillAppear(_ animated: Bool) {
- realTimeLocationTrackerService.addListener(self)
- realTimeLocationTrackerService.connect()
- }
- override func viewDidAppear(_ animated: Bool) {
- determineCurrentLocation()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- func createMapView() {
- mapView = MKMapView()
- let leftMargin: CGFloat = 0
- let topMargin: CGFloat = 60
- let mapWidth: CGFloat = view.frame.size.width
- let mapHeight: CGFloat = 700
- mapView.frame = CGRect(x: leftMargin, y: topMargin, width: mapWidth, height: mapHeight)
- mapView.mapType = MKMapType.standard
- mapView.showsCompass = true
- mapView.showsScale = true
- mapView.isZoomEnabled = true
- mapView.isScrollEnabled = true
- mapView.center = view.center
- view.addSubview(mapView)
- }
- func determineCurrentLocation() {
- locationManager.delegate = self
- mapView.delegate = self
- mapView.showsUserLocation = true
- if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
- locationManager.startUpdatingLocation()
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- } else {
- locationManager.requestWhenInUseAuthorization()
- }
- }
- func createAlertAndGotoChatViewController(title: String, msg: String) {
- let alert = UIAlertController(title: title, message: msg, preferredStyle: UIAlertControllerStyle.alert)
- alert.addAction(UIAlertAction(title: "StartChat", style: .default, handler: { (action) in
- guard let vc = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "chat") as? PrivateViewController else { return }
- self.present(vc, animated: true, completion: nil)
- }))
- alert.addAction(UIAlertAction(title: "Cancel", style: .default))
- self.present(alert, animated: true, completion: nil)
- }
- }
- // MARK: - Socket functions
- extension MapVC: RealTimeLocationTrackerEventsListener {
- func realTimeLocationTrackerDidConnect(_ realTimeLocationTrackerService: RealTimeLocationTracker) {
- print("realTimeLocationTrackerDidConnect")
- mapView.zoomToUserLocation()
- // let request = LocationRequest(currenUserId: currenUserId, latitude: nil, longitude: nil)
- let request = LocationRequest(currenUserId: currenUserId, username: "", gender: "", age: 0, latitude: "", longitude: "", cmd: "trackRegister")
- self.realTimeLocationTrackerService.sendLocationRequest(request)
- }
- func realTimeLocationTrackerDidDisconnect(_ realTimeLocationTrackerService: RealTimeLocationTracker) {
- print("realTimeLocationTrackerDidDisconnect")
- }
- func realTimeLocationTrackerDidRecieveError(_ realTimeLocationTrackerService: RealTimeLocationTracker, didRecieveError: Error) {
- print("realTimeLocationTrackerDidRecieveError", didRecieveError)
- }
- func realTimeLocationTrackerDidReceiveMessage(_ realTimeLocationTrackerService: RealTimeLocationTracker, didRecieveMessage text: String) {
- print("realTimeLocationTrackerDidReceiveMessage", text)
- guard let userCordinate = ReceiveUserCordinate.from(json: text) else { return }
- let username = userCordinate.username
- let gender = userCordinate.gender
- let age = userCordinate.age
- guard
- let latitude = Double(userCordinate.latitude),
- let longitude = Double(userCordinate.longitude)
- else { return }
- let user = userCordinate.currenUserId
- let uc = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
- if let annotation = userLocations[user] {
- annotation.coordinate = uc
- } else {
- let annotation = UserAnnotation(username: username, gender: gender, age: age, coordinate: uc)
- userLocations[user] = annotation
- mapView.addAnnotations([annotation])
- }
- // Remove Annotony when user goes offline
- // The server should send a different message when a user goes offline (edited)
- // And then you use `if let` to find the annotation of the user, remove it from the dictionary and from the map
- }
- }
- extension MapVC: CLLocationManagerDelegate {
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- }
- func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
- if status == .authorizedAlways {
- mapView.showsUserLocation = true
- mapView.userTrackingMode = .follow
- }
- }
- func mapView(_ mapView: MKMapView, didFailToLocateUserWithError error: Error) {
- print(error)
- }
- }
- extension MapVC: MKMapViewDelegate {
- func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
- let center = userLocation.coordinate
- // Sending my Location
- let request = LocationRequest(
- currenUserId: currenUserId,
- username: username,
- gender: gender,
- age: age,
- latitude: "\(center.latitude)",
- longitude: "\(center.longitude)",
- cmd: ""
- )
- self.realTimeLocationTrackerService.sendLocationRequest(request)
- // Current userlocation coordinate
- let span = MKCoordinateSpan(latitudeDelta: 0.005, longitudeDelta: 0.005)
- let region = MKCoordinateRegion(center: center, span: span)
- mapView.setRegion(region, animated: true)
- }
- func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
- let identifier = "UserAnnotation"
- if annotation is UserAnnotation {
- var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
- if annotationView == nil {
- annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
- annotationView!.canShowCallout = true
- let btn = UIButton(type: .detailDisclosure)
- annotationView!.rightCalloutAccessoryView = btn
- } else {
- annotationView!.annotation = annotation
- }
- return annotationView
- }
- return nil
- }
- func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
- guard let userAnnotation = view.annotation as? UserAnnotation else { return }
- guard let name = userAnnotation.username else { return }
- // let placeInfo = userAnnotation.info
- createAlertAndGotoChatViewController(title: name, msg: userAnnotation.gender)
- }
- }
Add Comment
Please, Sign In to add comment