ios - 显示用户周围的多个驱动程序注释(使用数据库)

标签 ios swift firebase firebase-realtime-database mapkit

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

simulator photo

我已经弄清楚如何通过数据库显示用户附近的驱动程序列表,现在我希望 map 显示用户附近的驱动程序。

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 数据库。

最佳答案

您可以使用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)
            }
        }

    }

关于ios - 显示用户周围的多个驱动程序注释(使用数据库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51904682/

相关文章:

ios - NSRegularExpression 找不到捕获组匹配项

iOS Swift - UITableView didselectrowatindexpath 崩溃

android - 带命名空间的 Firebase 远程配置

objective-c - NSMutableAttributedString 在更改字体时崩溃?

javascript - react-native-maps:UrlTile 和 Polyline - zIndex/Overlaying 不工作

ios - 快速、objective-c 协议(protocol)实现

ios - 如何使用具有不同对齐方式的 subview 创建堆栈 View ?

ios - 如何从堆栈中删除旧的 ViewController

android - 当我的 Android 应用程序处于前台时,我想隐藏其他应用程序的所有通知。这可能吗?如何?

firebase - Firebase 中的匿名用户