2017-09-06 55 views
1

我正在使用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]你能解釋它嗎? –

回答

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

好的,我以另一種方式做了,它也可以工作,但我想問你,如果你知道用戶如何用地圖點擊地圖上的泡泡,它可以在所有視圖中顯示地圖?這是可能的嗎?是你做的嗎 ? –