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.WindowsPresentation
3
{
4
   using System.Collections.Generic;
5
   using System.ComponentModel;
6
   using System.Windows;
7
   using System.Windows.Controls;
8
   using GMap.NET;
9
   using System.Windows.Media;
10
   using System.Diagnostics;
11
   using System.Windows.Shapes;
12
   using System;
13
 
14
   /// <summary>
15
   /// GMap.NET marker
16
   /// </summary>
17
   public class GMapMarker : INotifyPropertyChanged
18
   {
19
      public event PropertyChangedEventHandler PropertyChanged;
20
      protected void OnPropertyChanged(string name)
21
      {
22
         if(PropertyChanged != null)
23
         {
24
            PropertyChanged(this, new PropertyChangedEventArgs(name));
25
         }
26
      }
27
 
28
      protected void OnPropertyChanged(PropertyChangedEventArgs name)
29
      {
30
         if(PropertyChanged != null)
31
         {
32
            PropertyChanged(this, name);
33
         }
34
      }
35
 
36
      UIElement shape;
37
      static readonly PropertyChangedEventArgs Shape_PropertyChangedEventArgs = new PropertyChangedEventArgs("Shape");
38
 
39
      /// <summary>
40
      /// marker visual
41
      /// </summary>
42
      public UIElement Shape
43
      {
44
         get
45
         {
46
            return shape;
47
         }
48
         set
49
         {
50
            if(shape != value)
51
            {
52
               shape = value;
53
               OnPropertyChanged(Shape_PropertyChangedEventArgs);
54
 
55
               UpdateLocalPosition();
56
            }
57
         }
58
      }
59
 
60
      private PointLatLng position;
61
 
62
      /// <summary>
63
      /// coordinate of marker
64
      /// </summary>
65
      public PointLatLng Position
66
      {
67
         get
68
         {
69
            return position;
70
         }
71
         set
72
         {
73
            if(position != value)
74
            {
75
               position = value;
76
               UpdateLocalPosition();
77
            }
78
         }
79
      }
80
 
81
      GMapControl map;
82
 
83
      /// <summary>
84
      /// the map of this marker
85
      /// </summary>
86
      public GMapControl Map
87
      {
88
         get
89
         {
90
            if(Shape != null && map == null)
91
            {
92
               DependencyObject visual = Shape;
93
               while(visual != null && !(visual is GMapControl))
94
               {
95
                  visual = VisualTreeHelper.GetParent(visual);
96
               }
97
 
98
               map = visual as GMapControl;
99
            }
100
 
101
            return map;
102
         }
103
          internal set
104
          {
105
              map = value;
106
          }
107
      }
108
 
109
      /// <summary>
110
      /// custom object
111
      /// </summary>
112
      public object Tag;
113
 
114
      System.Windows.Point offset;
115
      /// <summary>
116
      /// offset of marker
117
      /// </summary>
118
      public System.Windows.Point Offset
119
      {
120
         get
121
         {
122
            return offset;
123
         }
124
         set
125
         {
126
            if(offset != value)
127
            {
128
               offset = value;
129
               UpdateLocalPosition();
130
            }
131
         }
132
      }
133
 
134
      int localPositionX;
135
      static readonly PropertyChangedEventArgs LocalPositionX_PropertyChangedEventArgs = new PropertyChangedEventArgs("LocalPositionX");
136
 
137
      /// <summary>
138
      /// local X position of marker
139
      /// </summary>
140
      public int LocalPositionX
141
      {
142
         get
143
         {
144
            return localPositionX;
145
         }
146
         internal set
147
         {
148
            if(localPositionX != value)
149
            {
150
               localPositionX = value;
151
               OnPropertyChanged(LocalPositionX_PropertyChangedEventArgs);
152
            }
153
         }
154
      }
155
 
156
      int localPositionY;
157
      static readonly PropertyChangedEventArgs LocalPositionY_PropertyChangedEventArgs = new PropertyChangedEventArgs("LocalPositionY");
158
 
159
      /// <summary>
160
      /// local Y position of marker
161
      /// </summary>
162
      public int LocalPositionY
163
      {
164
         get
165
         {
166
            return localPositionY;
167
         }
168
         internal set
169
         {
170
            if(localPositionY != value)
171
            {
172
               localPositionY = value;
173
               OnPropertyChanged(LocalPositionY_PropertyChangedEventArgs);
174
            }
175
         }
176
      }
177
 
178
      int zIndex;
179
      static readonly PropertyChangedEventArgs ZIndex_PropertyChangedEventArgs = new PropertyChangedEventArgs("ZIndex");
180
 
181
      /// <summary>
182
      /// the index of Z, render order
183
      /// </summary>
184
      public int ZIndex
185
      {
186
         get
187
         {
188
            return zIndex;
189
         }
190
         set
191
         {
192
            if(zIndex != value)
193
            {
194
               zIndex = value;
195
               OnPropertyChanged(ZIndex_PropertyChangedEventArgs);
196
            }
197
         }
198
      }
199
 
200
      public GMapMarker(PointLatLng pos)
201
      {
202
         Position = pos;
203
      }
204
 
205
      internal GMapMarker()
206
      {
207
      }
208
 
209
      /// <summary>
210
      /// calls Dispose on shape if it implements IDisposable, sets shape to null and clears route
211
      /// </summary>
212
      public virtual void Clear()
213
      {
214
         var s = (Shape as IDisposable);
215
         if(s != null)
216
         {
217
            s.Dispose();
218
            s = null;
219
         }
220
         Shape = null;
221
      }
222
 
223
      /// <summary>
224
      /// updates marker position, internal access usualy
225
      /// </summary>
226
      void UpdateLocalPosition()
227
      {
228
         if(Map != null)
229
         {
230
            GPoint p = Map.FromLatLngToLocal(Position);
231
            p.Offset(-(long)Map.MapTranslateTransform.X, -(long)Map.MapTranslateTransform.Y);
232
 
233
            LocalPositionX = (int)(p.X + (long)(offset.X));
234
            LocalPositionY = (int)(p.Y + (long)(offset.Y));
235
         }
236
      }
237
 
238
      /// <summary>
239
      /// forces to update local marker  position
240
      /// dot not call it if you don't really need to ;}
241
      /// </summary>
242
      /// <param name="m"></param>
243
      internal void ForceUpdateLocalPosition(GMapControl m)
244
      {
245
         if(m != null)
246
         {
247
            map = m;
248
         }
249
         UpdateLocalPosition();
250
      }
251
   }
252
}