我们如何在 Swift 中使用 firebase 发送位置信息

How we can send the location messages using firebase in Swift

我想使用 firebase 发送位置消息。就像当用户点击按钮时,它会显示警报你想发送哪条消息,比如音频,视频,location.When 用户 select 给定选项的位置它会调用 locationViewController 并将位置存储在 firebase.I 已经简单地加载了带有标记点的 google 地图,并定义了它自己的坐标,而不是动态的。当用户点击选择按钮时在 chatViewController 中定义的 InputActionSheet 函数调用此函数调用,并在另一个将调用位置功能的函数调用中调用 messages.But 我很困惑当用户在 [ 上的任何位置录音时我们如何动态获取坐标=26=] 地图,当用户点击时它也会显示标记。 SimpleViewController class 用于检查 google 地图的功能,它工作正常:

class ViewController: UIViewController {
    @IBOutlet weak var mapView: GMSMapView!
    let manager = CLLocationManager()
    let karachi = CLLocationCoordinate2D(latitude: 24.882752, longitude: 67.149848)
    let tandoAdam = CLLocationCoordinate2D(latitude: 25.76284, longitude: 68.66087)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupMap(title: "Karachi", subtitle: "Shah Faisal", karachi)
        self.setupMap(title: "TDM", subtitle: "AK H", tandoAdam)
        self.mapView.mapStyle(name:"darkTheme", type:"json")
    }
    
    func setupMap(title:String,subtitle:String,_ coordinate:CLLocationCoordinate2D){
        manager.delegate = self
        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()
        let camera = GMSCameraPosition.camera(withLatitude: coordinate.latitude, longitude:  coordinate.longitude, zoom: 6.0)
        mapView.camera = camera
        mapView.camera = camera
        self.addMarker(title: title, subtitle: subtitle, coordinate:coordinate)
    }
    
    func addMarker(title:String,subtitle:String,coordinate:CLLocationCoordinate2D){
        let marker = GMSMarker()
        marker.icon = #imageLiteral(resourceName: "DiceFive")
        marker.position = coordinate
        marker.title = title
        marker.snippet = subtitle
        marker.map = mapView
    }

    
}
extension UIViewController : CLLocationManagerDelegate {
    public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard locations.first != nil else{
            return
        }
    }

}

在聊天中显示 InputActionSheet 函数 class:

private func presentInputActionSheet() {
        let actionSheet = UIAlertController(title: "Attach Media",
                                            message: "What would you like to attach?",
                                            preferredStyle: .actionSheet)
        actionSheet.addAction(UIAlertAction(title: "Photo", style: .default, handler: { [weak self] _ in
            self?.presentPhotoInputActionsheet()
        }))
        actionSheet.addAction(UIAlertAction(title: "Video", style: .default, handler: { [weak self]  _ in
            self?.presentVideoInputActionsheet()
        }))
        actionSheet.addAction(UIAlertAction(title: "Audio", style: .default, handler: {  _ in

        }))
        actionSheet.addAction(UIAlertAction(title: "Location", style: .default, handler: { [weak self]  _ in
            self?.presentLocationPicker()
        }))
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        present(actionSheet, animated: true)
    }

聊天中的 presentLocationPicker 功能 class:

private func presentLocationPicker() {
        let vc = ViewController(coordinates: nil)
        vc.title = "Pick Location"
        vc.navigationItem.largeTitleDisplayMode = .never
        let location = Location(location: CLLocation(latitude: latitude, longitude: longitude),
                                 size: .zero)
        let message = Message(sender: selfSender,messageId: messageId,sentDate: Date(),kind: .location(location))
            DatabaseManager.shared.sendMessage(to: conversationId, otherUserEmail: strongSelf.otherUserEmail, name: name, newMessage: message, completion: { success in
                if success {
                    print("sent location message")
                }
                else {
                    print("failed to send location message")
                }
            })
        }
        navigationController?.pushViewController(vc, animated: true)
    }

试试这个

 let message = Message(sender: selfSender,messageId: messageId,sentDate: Date(),kind: .location(location))
            DatabaseManager.shared.sendMessage(to: conversationId, otherUserEmail: strongSelf.otherUserEmail, name: name, newMessage: message, completion: { success in
                if success {
                    print("sent location message")
                }
                else {
                    print("failed to send location message")
                }
                navigationController?.pushViewController(vc, animated: true)
            })

        }