Advertisement
Guest User

Untitled

a guest
Jun 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. //
  2. // Parques.swift
  3. // HelpMeParkIOS
  4. //
  5. // Created by Rafael Silva on 04/06/2019.
  6. // Copyright © 2019 Alvaro. All rights reserved.
  7. //
  8.  
  9. import Foundation
  10. import FirebaseDatabase
  11. import UIKit
  12. import MapKit
  13. import FirebaseAuth
  14.  
  15. class Parques: UIViewController, UITableViewDelegate, UITableViewDataSource {
  16.  
  17.  
  18. var parquesList = [Parque]()
  19.  
  20. var coordenadas = [CLLocation]()
  21.  
  22. var currentLoc = CLLocation()
  23.  
  24. var distancias: [Float] = []
  25.  
  26. var selectedLabel:String?
  27. var utilizador: String = ""
  28. var doubleToString: String = ""
  29.  
  30. var closestLocation: CLLocation?
  31.  
  32. var smallestDistance: CLLocationDistance?
  33.  
  34.  
  35. var dbHandle: DatabaseHandle?
  36.  
  37.  
  38. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  39.  
  40.  
  41. return parquesList.count
  42.  
  43.  
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. @IBOutlet weak var nrlugares: UILabel!
  51.  
  52.  
  53. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  54.  
  55.  
  56. let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ParqueViewCell
  57.  
  58.  
  59. self.parquesList = parquesList.sorted(by: {$0.distancia! < $1.distancia!})
  60. cell.lblName.text = self.parquesList[indexPath.row].nomeParque
  61.  
  62.  
  63. //Label do Nome
  64. cell.lblName.font = UIFont(name: "Arial-BoldMT", size:30);
  65. cell.lblName.textColor = UIColor.white
  66. cell.lblName.sizeToFit()
  67.  
  68.  
  69.  
  70. cell.accessoryType = UITableViewCell.AccessoryType.detailDisclosureButton
  71.  
  72. cell.backgroundView = UIImageView(image: UIImage(named: "table"))
  73.  
  74. cell.tintColor = UIColor.white
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82. return cell
  83.  
  84. }
  85.  
  86.  
  87.  
  88. func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
  89.  
  90. let alert = UIAlertController(title: "Informações", message:"Nome do Parque: " + parquesList[indexPath.row].nomeParque + "\n" + "\n" + " Distancia à sua localizacao: " + String(format:"%.2f",parquesList[indexPath.row].distancia!) + " KM", preferredStyle: UIAlertController.Style.alert)
  91. alert.addAction(UIAlertAction(title: "Fechar", style: UIAlertAction.Style.default, handler: nil))
  92. self.present(alert, animated: true, completion: nil)
  93.  
  94. alert.view.tintColor = UIColor.red;
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  106. print(parquesList[indexPath.row].nomeParque)
  107. self.performSegue(withIdentifier: "detalheParque", sender: indexPath)
  108. }
  109.  
  110.  
  111.  
  112. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  113. if segue.identifier == "detalheParque" {
  114. let idx = sender as! IndexPath
  115. let vcdetalhe = (segue.destination as! DetailParque)
  116. vcdetalhe.parquesList = parquesList[idx.row].nomeParque
  117. }
  118. }
  119.  
  120.  
  121. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  122.  
  123. return 100
  124. }
  125.  
  126.  
  127.  
  128.  
  129. //SET FIREBASE REFERENCE
  130. let ref = Database.database().reference()
  131.  
  132. var databaseHandle:DatabaseHandle?
  133.  
  134.  
  135. @IBOutlet weak var tableView: UITableView!
  136.  
  137. override func viewDidLoad() {
  138.  
  139. print(utilizador)
  140.  
  141. tableView.delegate = self
  142.  
  143.  
  144.  
  145.  
  146. tableView.backgroundView = UIImageView(image: UIImage(named: "table"))
  147.  
  148.  
  149. let uid = (Auth.auth().currentUser?.uid)!
  150.  
  151. dbHandle = ref.child("Users").child(uid).child("latestLoc").observe(.value, with: {
  152. (snapshot) in
  153.  
  154.  
  155. let currentLat = snapshot.childSnapshot(forPath:"latitude").value as? String
  156.  
  157. let currentLng = snapshot.childSnapshot(forPath:"longitude").value as? String
  158.  
  159. self.currentLoc = CLLocation(latitude: Double(currentLat!) as! CLLocationDegrees, longitude: Double(currentLng!) as! CLLocationDegrees)
  160. print(self.currentLoc)
  161. self.getParques()
  162.  
  163. })
  164.  
  165.  
  166. }
  167.  
  168. func getParques() {
  169. self.parquesList = []
  170. ref.child("Parques").observe(.childAdded, with: {
  171. snapshot in
  172.  
  173. print(snapshot)
  174.  
  175. let nome = (snapshot.value as? NSDictionary)!["nome"] as! String
  176.  
  177. let auxLat = (snapshot.value as? NSDictionary)!["Latitude"] as! String
  178.  
  179. let auxLong = (snapshot.value as? NSDictionary)!["Longitude"] as! String
  180.  
  181. let latitude = Double(auxLat)
  182. let longitude = Double(auxLong)
  183. let location = CLLocation(latitude: latitude!, longitude: longitude!)
  184. let distance = self.currentLoc.distance(from: location)
  185. print(nome, distance)
  186. self.parquesList.append(Parque(nome: nome, distancia: Double((distance) / 1000)))
  187. print(self.parquesList)
  188. self.coordenadas.append(CLLocation(latitude: Double(auxLat) as! CLLocationDegrees, longitude: Double(auxLong) as! CLLocationDegrees))
  189.  
  190. self.tableView.reloadData()
  191.  
  192. print(self.parquesList)
  193.  
  194.  
  195. })
  196. }
  197.  
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement