首页 > 解决方案 > 在用户周围显示多个驱动程序注释(使用数据库)

问题描述

我是编码新手。有没有办法在用户周围显示多个驱动程序。

模拟器照片.

我已经想出了如何通过数据库显示用户附近的司机列表,现在我希望地图显示用户附近的司机。

import UIKit
import Firebase
import FirebaseAuth
import FirebaseDatabase
import MapKit

class EmployeeTableViewController: UITableViewController, CLLocationManagerDelegate {

    @IBOutlet weak var jobsAvailableMap: MKMapView!

    var jobRequests : [DataSnapshot] = []
    var locationManager = CLLocationManager()
    var employeeLocation = CLLocationCoordinate2D()


    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()

        Database.database().reference().child("JobRequests").observe(.childAdded) { (snapshot) in
            if let jobRequestDictionary = snapshot.value as? [String:AnyObject] {
                if let employeeLat = jobRequestDictionary["employeeLat"] as? Double {

                } else {
                    self.jobRequests.append(snapshot)
                    self.tableView.reloadData()
                }
            }

        }

        Timer.scheduledTimer(withTimeInterval: 3, repeats: true) { (timer) in
            self.tableView.reloadData()
        }

    }


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let coord = manager.location?.coordinate {
            employeeLocation = coord
        }
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return jobRequests.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "jobRequestCell", for: indexPath)

        let snapshot = jobRequests[indexPath.row]

        if let jobRequestDictionary = snapshot.value as? [String:AnyObject] {
            if let email = jobRequestDictionary["email"] as? String {
                if let lat = jobRequestDictionary["lat"] as? Double {
                    if let lon = jobRequestDictionary["lon"] as? Double {

                        let employeeCLLocation = CLLocation(latitude: employeeLocation.latitude, longitude: employeeLocation.longitude)
                        let employerCLLocation = CLLocation(latitude: lat, longitude: lon)
                        let distance = employeeCLLocation.distance(from: employerCLLocation) / 1000
                        let roundedDistance = round(distance * 100) / 100

                        cell.textLabel?.text = "\(email) - \(roundedDistance)km away"

                    }
                }


            }
        }

        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let snapshot = jobRequests[indexPath.row]
       performSegue(withIdentifier: "acceptSegue", sender: snapshot)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let acceptVC = segue.destination as? AcceptJobViewController {
            if let snapshot = sender as? DataSnapshot {
                if let jobRequestDictionary = snapshot.value as? [String:AnyObject] {
                    if let email = jobRequestDictionary["email"] as? String {
                        if let lat = jobRequestDictionary["lat"] as? Double {
                            if let lon = jobRequestDictionary["lon"] as? Double {
                                acceptVC.requestEmail = email
                                let location = CLLocationCoordinate2D(latitude: lat, longitude: lon)
                                acceptVC.requestLocation = location
                                acceptVC.employeeLocation = employeeLocation
                            }
                        }
                    }
                }
            }
        }
    }

    @IBAction func logoutTapped(_ sender: Any) {
        try? Auth.auth().signOut()
        navigationController?.dismiss(animated: true, completion: nil)

    }

}

我正在尝试在线查找教程,但大多数都没有连接到 Firebase 数据库。

标签: iosswiftfirebasefirebase-realtime-databasemapkit

解决方案


你可以使用addAnnotation方法。

例如(不保证您可以构建以下代码)

func addDriverAnnotation(snapshot: DataSnapshot){
    if let jobRequestDictionary = snapshot.value as? [String:AnyObject] {
        if let email = jobRequestDictionary["email"] as? String {
            if let lat = jobRequestDictionary["lat"] as? Double {
                if let lon = jobRequestDictionary["lon"] as? Double {

                    let annotation = MKAnnotation()
                    annotation.coordinate = CLLocationCoordinate2DMake(lat, lon)
                    annotation.title = "title"
                    annotation.subtitle = "subtitle"
                    self.jobsAvailableMap.addAnnotation(annotation)
                }
            }
        }
    }
}

您需要在方法中调用此Database.database()...方法。

Database.database().reference().child("JobRequests").observe(.childAdded) { (snapshot) in
        if let jobRequestDictionary = snapshot.value as? [String:AnyObject] {
            if let employeeLat = jobRequestDictionary["employeeLat"] as? Double {

            } else {
                self.jobRequests.append(snapshot)
                self.tableView.reloadData()
                self.addDriverAnnotation(snapshot: snapshot)
            }
        }

    }

推荐阅读