Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- import Firebase
- import FirebaseDatabase
- import MapKit
- import CoreLocation
- class RequestVC: UITableViewController, CLLocationManagerDelegate {
- var locationManager = CLLocationManager()
- var sellerUserNames = [String]()
- var requestLocations = [CLLocationCoordinate2D]()
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- if segue.identifier == "backToMain" {
- self.navigationController?.navigationBar.isHidden = true
- }
- }
- override func viewDidLoad() {
- super.viewDidLoad()
- locationManager.delegate = self
- locationManager.desiredAccuracy = kCLLocationAccuracyBest
- locationManager.requestWhenInUseAuthorization()
- locationManager.startUpdatingLocation()
- let databaseRef = FIRDatabase.database().reference()
- databaseRef.child("Sell_Request").observe(FIRDataEventType.childAdded, with: { (FIRDataSnapshot) in
- if let data = FIRDataSnapshot.value as? NSDictionary {
- if let name = data[Constants.NAME] as? String {
- if let lat = data["latitude"] as? Double {
- if let long = data["longitude"] as? Double {
- print("(self.sellerUserNames) Location: Latitude: (lat), Longitude: (long)")
- self.sellerUserNames.append(name)
- self.requestLocations.append(CLLocationCoordinate2D(latitude: lat, longitude: long))
- self.tableView.reloadData()
- }
- }
- }
- }
- })
- }
- func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- }
- override func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
- override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return sellerUserNames.count
- }
- override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
- let location = locationManager.location?.coordinate
- let buyerLocation = CLLocation(latitude: (location?.latitude)!, longitude: (location?.longitude)!)
- let sellerLocation = CLLocation(latitude: requestLocations[indexPath.row].latitude, longitude: requestLocations[indexPath.row].longitude)
- let distance = buyerLocation.distance(from: sellerLocation) / 1000
- let roundedDistance = round(distance * 100) / 100
- let distanceInMiles = roundedDistance * 0.621
- let roundedDistanceInMiles = round(distanceInMiles * 1000) / 1000
- cell.textLabel?.text = sellerUserNames[indexPath.row] + " - (roundedDistanceInMiles) miles away"
- return cell
- }
- /*
- // Override to support conditional editing of the table view.
- override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- // Return false if you do not want the specified item to be editable.
- return true
- }
- */
- /*
- // Override to support editing the table view.
- override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
- if editingStyle == .delete {
- // Delete the row from the data source
- tableView.deleteRows(at: [indexPath], with: .fade)
- } else if editingStyle == .insert {
- // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
- }
- }
- */
- /*
- // Override to support rearranging the table view.
- override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
- }
- */
- /*
- // Override to support conditional rearranging of the table view.
- override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
- // Return false if you do not want the item to be re-orderable.
- return true
- }
- */
- /*
- // MARK: - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
- // Get the new view controller using segue.destinationViewController.
- // Pass the selected object to the new view controller.
- }
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement