使用JSQLocationMediaItem发送位置

问题描述:

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

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) 

但是,当我点击发送位置按钮,我只是得到一个图像气泡与纺车,它永远做。 enter image description here

+0

嘿我试图使用这段代码,但确切的是:self.latestLocation = locations [locations.count-1]你能解释它吗? –

添加解决方案在你的代码

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) 

你完成了位置对象创建后设置的位置。

+0

嘿,我想使用这段代码,但究竟是什么:self.latestLocation = locations [locations.count-1]你能解释它吗? –

+0

@GhiggzPikkoro self.latestLocation = locations [locations.count-1]用于从CLLocationManager委托方法中提取最后一个位置。 –

+0

好的,我以另一种方式做了,它也可以工作,但我想问你,如果你知道用户如何用地图点击地图上的泡泡,它可以在所有视图中显示地图?这是可能的吗?是你做的吗 ? –