GMapOverlay.cs :  » GIS » GMap.NET » GMap » NET » WindowsForms » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GIS » GMap.NET 
GMap.NET » GMap » NET » WindowsForms » GMapOverlay.cs

namespace GMap.NET.WindowsForms{
   using System.Drawing;
   using System.Drawing.Drawing2D;
   using System.Windows.Forms;
   using GMap.NET.ObjectModel;

   /// <summary>
   /// GMap.NET overlay
   /// </summary>
   public class GMapOverlay
   {
      /// <summary>
      /// is overlay visible
      /// </summary>
      public bool IsVisibile = true;

      /// <summary>
      /// overlay Id
      /// </summary>
      public string Id;

      /// <summary>
      /// list of markers, should be thread safe
      /// </summary>
      public readonly ObservableCollectionThreadSafe<GMapMarker> Markers = new ObservableCollectionThreadSafe<GMapMarker>();

      /// <summary>
      /// list of routes, should be thread safe
      /// </summary>
      public readonly ObservableCollectionThreadSafe<GMapRoute> Routes = new ObservableCollectionThreadSafe<GMapRoute>();

      /// <summary>
      /// list of polygons, should be thread safe
      /// </summary>
      public readonly ObservableCollectionThreadSafe<GMapPolygon> Polygons = new ObservableCollectionThreadSafe<GMapPolygon>();

      internal GMapControl Control;

      public GMapOverlay(GMapControl control, string id)
      {
         Control = control;
         Id = id;
         Markers.CollectionChanged += new NotifyCollectionChangedEventHandler(Markers_CollectionChanged);
         Routes.CollectionChanged += new NotifyCollectionChangedEventHandler(Routes_CollectionChanged);
         Polygons.CollectionChanged += new NotifyCollectionChangedEventHandler(Polygons_CollectionChanged);
      }

      void Polygons_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
         if(e.NewItems != null)
         {
            if(Control != null)
            {
               foreach(GMapPolygon obj in e.NewItems)
               {
                  Control.UpdatePolygonLocalPosition(obj);
               }
            }
         }

         if(Control != null)
         {
            if(!Control.HoldInvalidation)
            {
               Control.Core_OnNeedInvalidation();
            }
         }
      }

      void Routes_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
         if(e.NewItems != null)
         {
            if(Control != null)
            {
               foreach(GMapRoute obj in e.NewItems)
               {
                  Control.UpdateRouteLocalPosition(obj);
               }
            }
         }

         if(Control != null)
         {
            if(!Control.HoldInvalidation)
            {
               Control.Core_OnNeedInvalidation();
            }
         }
      }

      void Markers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
      {
         if(e.NewItems != null)
         {
            if(Control != null)
            {
               foreach(GMapMarker obj in e.NewItems)
               {
                  if(obj != null)
                  {
                     obj.Overlay = this;
                     obj.ForceUpdateLocalPosition();
                  }
               }
            }
         }

         if(Control != null)
         {
            if(e.Action == NotifyCollectionChangedAction.Remove || e.Action == NotifyCollectionChangedAction.Reset)
            {
#if !PocketPC
               if(Control.IsMouseOverMarker)
               {
                  Control.IsMouseOverMarker = false;
                  Control.Cursor = Cursors.Default;
               }
#endif
            }

            if(!Control.HoldInvalidation)
            {
               Control.Core_OnNeedInvalidation();
            }
         }
      }

      /// <summary>
      /// draw routes, override to draw custom
      /// </summary>
      /// <param name="g"></param>
      protected virtual void DrawRoutes(Graphics g)
      {
#if !PocketPC
         foreach(GMapRoute r in Routes)
         {
            using(GraphicsPath rp = new GraphicsPath())
            {
               for(int i = 0; i < r.LocalPoints.Count; i++)
               {
                  GMap.NET.Point p2 = r.LocalPoints[i];

                  if(i == 0)
                  {
                     rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
                  }
                  else
                  {
                     System.Drawing.PointF p = rp.GetLastPoint();
                     rp.AddLine(p.X, p.Y, p2.X, p2.Y);
                  }
               }

               if(rp.PointCount > 0)
               {
                  g.DrawPath(r.Stroke, rp);
               }
            }
         }
#else
         foreach(GMapRoute r in Routes)
         {
            Point[] pnts = new Point[r.LocalPoints.Count];
            for(int i = 0; i < r.LocalPoints.Count; i++)
            {
               Point p2 = new Point(r.LocalPoints[i].X, r.LocalPoints[i].Y);
               pnts[pnts.Length - 1 - i] = p2;
            }

            if(pnts.Length > 0)
            {
               g.DrawLines(r.Stroke, pnts);
            }
         }
#endif
      }

      /// <summary>
      /// draw polygons, override to draw custom
      /// </summary>
      /// <param name="g"></param>
      protected virtual void DrawPolygons(Graphics g)
      {
#if !PocketPC
         foreach(GMapPolygon r in Polygons)
         {
            using(GraphicsPath rp = new GraphicsPath())
            {
               for(int i = 0; i < r.LocalPoints.Count; i++)
               {
                  GMap.NET.Point p2 = r.LocalPoints[i];

                  if(i == 0)
                  {
                     rp.AddLine(p2.X, p2.Y, p2.X, p2.Y);
                  }
                  else
                  {
                     System.Drawing.PointF p = rp.GetLastPoint();
                     rp.AddLine(p.X, p.Y, p2.X, p2.Y);
                  }
               }

               if(rp.PointCount > 0)
               {
                  rp.CloseFigure();

                  g.FillPath(r.Fill, rp);

                  g.DrawPath(r.Stroke, rp);
               }
            }
         }
#else
         foreach(GMapPolygon r in Polygons)
         {
            Point[] pnts = new Point[r.LocalPoints.Count];
            for(int i = 0; i < r.LocalPoints.Count; i++)
            {
               Point p2 = new Point(r.LocalPoints[i].X, r.LocalPoints[i].Y);
               pnts[pnts.Length - 1 - i] = p2;
            }

            if(pnts.Length > 0)
            {
               g.FillPolygon(r.Fill, pnts);
               g.DrawPolygon(r.Stroke, pnts);
            }
         }
#endif
      }

      /// <summary>
      /// renders objects and routes
      /// </summary>
      /// <param name="g"></param>
      public virtual void Render(Graphics g)
      {
         if(Control != null)
         {
            if(Control.RoutesEnabled)
            {
               DrawRoutes(g);
            }

            if(Control.PolygonsEnabled)
            {
               DrawPolygons(g);
            }

            if(Control.MarkersEnabled)
            {
               // markers
               foreach(GMapMarker m in Markers)
               {
                  if(m.Visible && Control.Core.CurrentRegion.Contains(m.LocalPosition.X, m.LocalPosition.Y))
                  {
                     m.OnRender(g);
                  }
               }

               // tooltips above
               foreach(GMapMarker m in Markers)
               {
                  if(m.ToolTip != null && m.Visible && Control.Core.CurrentRegion.Contains(m.LocalPosition.X, m.LocalPosition.Y))
                  {
                     if(!string.IsNullOrEmpty(m.ToolTipText) && (m.ToolTipMode == MarkerTooltipMode.Always || (m.ToolTipMode == MarkerTooltipMode.OnMouseOver && m.IsMouseOver)))
                     {
                        m.ToolTip.Draw(g);
                     }
                  }
               }
            }
         }
      }
   }
}
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.