2

I have just tried implementing the LocationMediaItem in my Xamarin.iOS app using JSQMessagesViewController. Everything worked out fine, only problem is that the UICollectionView cell which is supposed to show the location is stuck on loading forever and the map is never actually being displayed.

Here's some code how I make the locationMediaItem in C#:

var locationMediaItem = new LocationMediaItem(new CLLocation(latitude, longitude));

    if (ChatDB.Messages[indexPath.Row].user_id == SenderId)
    {
        locationMediaItem.AppliesMediaViewMaskAsOutgoing = true;
        return JSQMessagesViewController.Message.Create(ChatDB.Messages[indexPath.Row].user_id, User.Instance.name, locationMediaItem);
    }

Here is an image of what I mean:

Here

So the JSQMessagesViewController knows I want it to display a map view, but it never stops loading and I can't figure out why.

Hope somebody can help out.

Stefan Stefanov
  • 829
  • 7
  • 23
Florensvb
  • 209
  • 1
  • 10

1 Answers1

4

Was having the same issue as you and was able to make it work.

The trick was not to set the location right on the LocationMediaItem constructor, leave it null then use the method SetLocation which receives the location and a handle as parameters.

var itemLoationItem = new LocationMediaItem ();
itemLoationItem.AppliesMediaViewMaskAsOutgoing = true;
Messages.Add (Message.Create (SenderId, SenderDisplayName, itemLoationItem));
itemLoationItem.SetLocation (new CLLocation (lat, long), HandleLocationMediaItemCompletionBlock);

In the handle just reload the data

void HandleLocationMediaItemCompletionBlock ()
{
    this.CollectionView.ReloadData ();
}

Note: Avoid creating the Message object in GetMessageData as this method is called every time the Collectionview is reloaded. You should handle the Message creation when receiving the message or when sending it and add it to the collection of messages then in this method you just return the object from the collection.

public override IMessageData GetMessageData (MessagesCollectionView collectionView, NSIndexPath indexPath)
{
    return Messages [indexPath.Row];
}

Image showing location

pinedax
  • 9,246
  • 2
  • 23
  • 30
  • Dude! Thank you so much for your answer, I've been waiting for someone for so long haha. I have just tried your suggestion but unfortunately with no results. This is how I adapted my code: `locationMediaItem.SetLocation(new CLLocation(latitude, longitude), delegate { collectionView.ReloadData(); });` I added a breaking point at `collection.reload` and it's actually being called, but the map does not appear... I have yet to implement your suggestion, not to create a new JSQMessage every time. I will definitely do that, but i doubt it can be the source of my problem? – Florensvb Feb 20 '17 at 10:39
  • Actually, once I implemented all changes as you proposed- it worked! Thanks again – Florensvb Feb 20 '17 at 13:04