Advertisement
Guest User

Untitled

a guest
Feb 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import UIKit
  2. import Firebase
  3. import FirebaseDatabase
  4. import MapKit
  5. import CoreLocation
  6.  
  7. class RequestVC: UITableViewController, CLLocationManagerDelegate {
  8.  
  9. var locationManager = CLLocationManager()
  10. var sellerUserNames = [String]()
  11. var requestLocations = [CLLocationCoordinate2D]()
  12.  
  13. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  14. if segue.identifier == "backToMain" {
  15. self.navigationController?.navigationBar.isHidden = true
  16. }
  17. }
  18.  
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21.  
  22. locationManager.delegate = self
  23. locationManager.desiredAccuracy = kCLLocationAccuracyBest
  24. locationManager.requestWhenInUseAuthorization()
  25. locationManager.startUpdatingLocation()
  26.  
  27. let databaseRef = FIRDatabase.database().reference()
  28. databaseRef.child("Sell_Request").observe(FIRDataEventType.childAdded, with: { (FIRDataSnapshot) in
  29.  
  30. if let data = FIRDataSnapshot.value as? NSDictionary {
  31. if let name = data[Constants.NAME] as? String {
  32. if let lat = data["latitude"] as? Double {
  33. if let long = data["longitude"] as? Double {
  34.  
  35. print("(self.sellerUserNames) Location: Latitude: (lat), Longitude: (long)")
  36.  
  37. self.sellerUserNames.append(name)
  38. self.requestLocations.append(CLLocationCoordinate2D(latitude: lat, longitude: long))
  39.  
  40. self.tableView.reloadData()
  41.  
  42. }
  43. }
  44. }
  45. }
  46. })
  47. }
  48.  
  49.  
  50. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  51. }
  52.  
  53.  
  54. override func didReceiveMemoryWarning() {
  55. super.didReceiveMemoryWarning()
  56. }
  57.  
  58. override func numberOfSections(in tableView: UITableView) -> Int {
  59. return 1
  60. }
  61.  
  62. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  63. return sellerUserNames.count
  64. }
  65.  
  66.  
  67. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  68.  
  69. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
  70. let location = locationManager.location?.coordinate
  71.  
  72. let buyerLocation = CLLocation(latitude: (location?.latitude)!, longitude: (location?.longitude)!)
  73. let sellerLocation = CLLocation(latitude: requestLocations[indexPath.row].latitude, longitude: requestLocations[indexPath.row].longitude)
  74.  
  75. let distance = buyerLocation.distance(from: sellerLocation) / 1000
  76.  
  77. let roundedDistance = round(distance * 100) / 100
  78.  
  79. let distanceInMiles = roundedDistance * 0.621
  80. let roundedDistanceInMiles = round(distanceInMiles * 1000) / 1000
  81.  
  82.  
  83. cell.textLabel?.text = sellerUserNames[indexPath.row] + " - (roundedDistanceInMiles) miles away"
  84.  
  85. return cell
  86. }
  87.  
  88.  
  89. /*
  90. // Override to support conditional editing of the table view.
  91. override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  92. // Return false if you do not want the specified item to be editable.
  93. return true
  94. }
  95. */
  96.  
  97. /*
  98. // Override to support editing the table view.
  99. override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
  100. if editingStyle == .delete {
  101. // Delete the row from the data source
  102. tableView.deleteRows(at: [indexPath], with: .fade)
  103. } else if editingStyle == .insert {
  104. // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  105. }
  106. }
  107. */
  108.  
  109. /*
  110. // Override to support rearranging the table view.
  111. override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
  112.  
  113. }
  114. */
  115.  
  116. /*
  117. // Override to support conditional rearranging of the table view.
  118. override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
  119. // Return false if you do not want the item to be re-orderable.
  120. return true
  121. }
  122. */
  123.  
  124. /*
  125. // MARK: - Navigation
  126.  
  127. // In a storyboard-based application, you will often want to do a little preparation before navigation
  128. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  129. // Get the new view controller using segue.destinationViewController.
  130. // Pass the selected object to the new view controller.
  131. }
  132. */
  133.  
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement