2

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. enter image description here

juelizabeth
  • 485
  • 1
  • 8
  • 31
  • Hey I'm trying to use this code but what is exactly that : self.latestLocation = locations[locations.count-1] Can You explain it? – Ghiggz Pikkoro Jan 06 '18 at 14:59

1 Answers1

2

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.

Sachin Nautiyal
  • 568
  • 4
  • 15
  • Hey I'm trying to use this code but what is exactly that : self.latestLocation = locations[locations.count-1] Can You explain it? – Ghiggz Pikkoro Jan 06 '18 at 14:59
  • @GhiggzPikkoro self.latestLocation = locations[locations.count-1] is for picking up the last location from the CLLocationManager delegate method. – Sachin Nautiyal Jan 10 '18 at 15:18
  • Ok I did by another way and it can work too, but I want to ask you if you know how when user tap on the bubble with the map it can show the map in the all view? Is it possible according to you? Did you do that ? – Ghiggz Pikkoro Jan 10 '18 at 15:25