Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2287 - 1

2
namespace GMap.NET
3
{
4
   using System;
5
   using System.Collections.Generic;
6
   using System.Runtime.Serialization;
7
   using GMap.NET.MapProviders;
8
 
9
   /// <summary>
10
   /// represents route of map
11
   /// </summary>
12
   [Serializable]
13
#if !PocketPC
14
   public class MapRoute : ISerializable, IDeserializationCallback
15
#else
16
   public class MapRoute
17
#endif
18
   {
19
      /// <summary>
20
      /// points of route
21
      /// </summary>
22
      public readonly List<PointLatLng> Points = new List<PointLatLng>();
23
 
24
      /// <summary>
25
      /// route info
26
      /// </summary>
27
      public string Name;
28
 
29
      /// <summary>
30
      /// custom object
31
      /// </summary>
32
      public object Tag;
33
 
34
      /// <summary>
35
      /// route start point
36
      /// </summary>
37
      public PointLatLng? From
38
      {
39
         get
40
         {
41
            if(Points.Count > 0)
42
            {
43
               return Points[0];
44
            }
45
 
46
            return null;
47
         }
48
      }
49
 
50
      /// <summary>
51
      /// route end point
52
      /// </summary>
53
      public PointLatLng? To
54
      {
55
         get
56
         {
57
            if(Points.Count > 1)
58
            {
59
               return Points[Points.Count - 1];
60
            }
61
 
62
            return null;
63
         }
64
      }
65
 
66
      public MapRoute(string name)
67
      {
68
         Name = name;
69
      }
70
 
71
      public MapRoute(IEnumerable<PointLatLng> points, string name)
72
      {
73
         Points.AddRange(points);
74
         Name = name;
75
      }
76
 
77
      /// <summary>
78
      /// route distance (in km)
79
      /// </summary>
80
      public double Distance
81
      {
82
         get
83
         {
84
            double distance = 0.0;
85
 
86
            if(From.HasValue && To.HasValue)
87
            {
88
               for(int i = 1; i < Points.Count; i++)
89
               {
90
                  distance += GMapProviders.EmptyProvider.Projection.GetDistance(Points[i - 1], Points[i]);
91
               }
92
            }
93
 
94
            return distance;
95
         }
96
      }    
97
 
98
      /// <summary>
99
      /// clears points and sets tag and name to null
100
      /// </summary>
101
      public void Clear()
102
      {
103
         Points.Clear();
104
         Tag = null;
105
         Name = null;
106
      }
107
 
108
#if !PocketPC
109
      #region ISerializable Members
110
 
111
      // Temp store for de-serialization.
112
      private PointLatLng[] deserializedPoints;
113
 
114
      /// <summary>
115
      /// Populates a <see cref="T:System.Runtime.Serialization.SerializationInfo"/> with the data needed to serialize the target object.
116
      /// </summary>
117
      /// <param name="info">The <see cref="T:System.Runtime.Serialization.SerializationInfo"/> to populate with data.</param>
118
      /// <param name="context">The destination (see <see cref="T:System.Runtime.Serialization.StreamingContext"/>) for this serialization.</param>
119
      /// <exception cref="T:System.Security.SecurityException">
120
      /// The caller does not have the required permission.
121
      /// </exception>
122
      public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
123
      {
124
         info.AddValue("Name", this.Name);
125
         info.AddValue("Tag", this.Tag);
126
         info.AddValue("Points", this.Points.ToArray());
127
      }
128
 
129
      /// <summary>
130
      /// Initializes a new instance of the <see cref="MapRoute"/> class.
131
      /// </summary>
132
      /// <param name="info">The info.</param>
133
      /// <param name="context">The context.</param>
134
      protected MapRoute(SerializationInfo info, StreamingContext context)
135
      {
136
         this.Name = info.GetString("Name");
137
         this.Tag = Extensions.GetValue<object>(info, "Tag", null);
138
         this.deserializedPoints = Extensions.GetValue<PointLatLng[]>(info, "Points");
139
         this.Points = new List<PointLatLng>();
140
      }
141
 
142
      #endregion
143
 
144
      #region IDeserializationCallback Members
145
 
146
      /// <summary>
147
      /// Runs when the entire object graph has been de-serialized.
148
      /// </summary>
149
      /// <param name="sender">The object that initiated the callback. The functionality for this parameter is not currently implemented.</param>
150
      public virtual void OnDeserialization(object sender)
151
      {
152
         // Accounts for the de-serialization being breadth first rather than depth first.
153
         Points.AddRange(deserializedPoints);
154
         Points.TrimExcess();
155
      }
156
 
157
      #endregion
158
#endif
159
   }
160
}