使用JSQLocationMediaItem发送位置 [英] send location using JSQLocationMediaItem

查看:93
本文介绍了使用JSQLocationMediaItem发送位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSQMessagesViewController在我的应用程序中实现聊天。我希望能够向我发送与我的位置聊天的用户。这就是我所做的。

I am using JSQMessagesViewController to implement chat in my app. I want to be able to send the user that I am chatting with my location. This is what I did.

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        self.latestLocation = locations[locations.count-1]

    }

  let sendLocation = UIAlertAction(title: "Send Location", style: .default, handler: { (action) -> Void in



            let loc: JSQLocationMediaItem = JSQLocationMediaItem(location: self.latestLocation)

            loc.appliesMediaViewMaskAsOutgoing = true

            let locmessage: JSQMessage = JSQMessage(senderId: self.senderId, senderDisplayName: self.senderDisplayName, date: NSDate() as Date!, media: loc)

            self.messages.append(locmessage)

            self.finishSendingMessage(animated: true)
            self.collectionView.reloadData()

            print("Location button tapped")
        })

        let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
            print("Cancel button tapped")
        })

        alertController.addAction(sendLocation)

        self.navigationController!.present(alertController, animated: true, completion: nil)

但是当我单击发送位置按钮时,我只是得到一个带有旋转轮的图像气泡,它永远持续下去。

But when I click on the send location button, I just get an image bubble with a spinning wheel and it does on forever.

推荐答案

在代码中添加解决方案

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    self.latestLocation = locations[locations.count-1]

}

let sendLocation = UIAlertAction(title: "Send Location", style: .default, handler: { (action) -> Void in



        let loc: JSQLocationMediaItem = JSQLocationMediaItem()
        loc.setLocation(self.latestLocation) { // Added completion handler for updating the map after getting the location.

        loc.appliesMediaViewMaskAsOutgoing = true

        let locmessage: JSQMessage = JSQMessage(senderId: self.senderId, senderDisplayName: self.senderDisplayName, date: NSDate() as Date!, media: loc)

        self.messages.append(locmessage)

        self.finishSendingMessage(animated: true)
        self.collectionView.reloadData()

        print("Location button tapped")
    })
 }

    let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action) -> Void in
        print("Cancel button tapped")
    })

    alertController.addAction(sendLocation)

    self.navigationController!.present(alertController, animated: true, completion: nil)

您必须在完成位置对象创建后设置位置

You have to set the location after completing the location object creation.

这篇关于使用JSQLocationMediaItem发送位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆