namespace GMap.NET { using System; using System.Collections.Generic; using System.Runtime.Serialization; using GMap.NET.MapProviders; /// /// represents route of map /// [Serializable] #if !PocketPC public class MapRoute : ISerializable, IDeserializationCallback #else public class MapRoute #endif { /// /// points of route /// public readonly List Points = new List(); /// /// route info /// public string Name; /// /// custom object /// public object Tag; /// /// route start point /// public PointLatLng? From { get { if(Points.Count > 0) { return Points[0]; } return null; } } /// /// route end point /// public PointLatLng? To { get { if(Points.Count > 1) { return Points[Points.Count - 1]; } return null; } } public MapRoute(string name) { Name = name; } public MapRoute(IEnumerable points, string name) { Points.AddRange(points); Name = name; } /// /// route distance (in km) /// public double Distance { get { double distance = 0.0; if(From.HasValue && To.HasValue) { for(int i = 1; i < Points.Count; i++) { distance += GMapProviders.EmptyProvider.Projection.GetDistance(Points[i - 1], Points[i]); } } return distance; } } /// /// clears points and sets tag and name to null /// public void Clear() { Points.Clear(); Tag = null; Name = null; } #if !PocketPC #region ISerializable Members // Temp store for de-serialization. private PointLatLng[] deserializedPoints; /// /// Populates a with the data needed to serialize the target object. /// /// The to populate with data. /// The destination (see ) for this serialization. /// /// The caller does not have the required permission. /// public virtual void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("Name", this.Name); info.AddValue("Tag", this.Tag); info.AddValue("Points", this.Points.ToArray()); } /// /// Initializes a new instance of the class. /// /// The info. /// The context. protected MapRoute(SerializationInfo info, StreamingContext context) { this.Name = info.GetString("Name"); this.Tag = Extensions.GetValue(info, "Tag", null); this.deserializedPoints = Extensions.GetValue(info, "Points"); this.Points = new List(); } #endregion #region IDeserializationCallback Members /// /// Runs when the entire object graph has been de-serialized. /// /// The object that initiated the callback. The functionality for this parameter is not currently implemented. public virtual void OnDeserialization(object sender) { // Accounts for the de-serialization being breadth first rather than depth first. Points.AddRange(deserializedPoints); Points.TrimExcess(); } #endregion #endif } }