send location using JSQLocationMediaItem

不打扰是莪最后的温柔 提交于 2019-12-24 00:13:15

问题


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.


回答1:


Adding the solution in your code

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.



来源:https://stackoverflow.com/questions/46080206/send-location-using-jsqlocationmediaitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!