问题
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:
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.
回答1:
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];
}
来源:https://stackoverflow.com/questions/42314143/show-locationmediaitem-in-jsqmessagesviewcontroller