using System; using System.Collections.Generic; using LeanCloud.Storage; namespace LeanCloud.Realtime { /// /// 位置消息 /// public class LCIMLocationMessage : LCIMTextMessage { /// /// 位置 /// public LCGeoPoint Location { get; set; } internal LCIMLocationMessage() { } public LCIMLocationMessage(LCGeoPoint locaction) : base(null) { Location = locaction; } internal override Dictionary Encode() { if (Location == null) { throw new ArgumentNullException(nameof(Location)); } Dictionary data = base.Encode(); Dictionary locationData = new Dictionary { { MessageDataLongitudeKey, Location.Longitude }, { MessageDataLatitudeKey, Location.Latitude } }; data[MessageLocationKey] = locationData; return data; } internal override void Decode(Dictionary msgData) { base.Decode(msgData); if (msgData.TryGetValue(MessageLocationKey, out object val)) { Dictionary locationData = val as Dictionary; if (locationData == null) { return; } double latitude = 0, longitude = 0; if (locationData.TryGetValue(MessageDataLatitudeKey, out object lat) && double.TryParse(lat as string, out double la)) { latitude = la; } if (locationData.TryGetValue(MessageDataLongitudeKey, out object lon) && double.TryParse(lon as string, out double lo)) { longitude = lo; } Location = new LCGeoPoint(latitude, longitude); } } public override int MessageType => LocationMessageType; } }