2017-09-06 8 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

Ok 다른 방법으로도 작업 할 수 있습니다. 그러나 사용자가지도로 풍선을 탭하면 모든보기에서지도를 표시 할 수있는 방법을 알고 있는지 묻고 싶습니다. 그것은 당신에 따라 가능합니까? 네가 했니 ? –