我正在使用JSQMessagesViewController在我的应用中实现聊天。我希望能够将我正在聊天的用户发送到我的位置。这就是我所做的。
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)
答案 0 :(得分:2)
在代码中添加解决方案
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)
您必须在完成位置对象创建后设置位置。