ZoomAndCenterPolygons and ZoomAndCenterAll

Topics: Feature Requests, Windows Forms
Jun 28, 2012 at 3:56 PM

Hi radioman,

I noticed that the GMapControl doesn't have a way to zoom on visible polygons or on all objects of a overlay.

So I coded these methods myself, and I'd like to suggest the use of then in the nexts builds.

Here is my code on GMaps.NET.WindowsForms.GMapControl.cs:

      /// <summary>
      /// gets rectangle with all objects inside
      /// </summary>
      /// <param name="overlayId">overlay id or null to check all</param>
      /// <returns></returns>
      public RectLatLng? GetRectOfAllPolygons( string overlayId )
      {
          RectLatLng? ret = null;

          double left = double.MaxValue;
          double top = double.MinValue;
          double right = double.MinValue;
          double bottom = double.MaxValue;

          foreach ( GMapOverlay o in Overlays )
          {
              if ( overlayId == null || o.Id == overlayId )
              {
                  if ( o.IsVisibile && o.Polygons.Count > 0 )
                  {
                      foreach ( GMapPolygon poly in o.Polygons )
                      {
                          if ( poly.IsVisible && poly.From.HasValue && poly.To.HasValue )
                          {
                              foreach ( PointLatLng p in poly.Points )
                              {
                                  // left
                                  if ( p.Lng < left )
                                  {
                                      left = p.Lng;
                                  }

                                  // top
                                  if ( p.Lat > top )
                                  {
                                      top = p.Lat;
                                  }

                                  // right
                                  if ( p.Lng > right )
                                  {
                                      right = p.Lng;
                                  }

                                  // bottom
                                  if ( p.Lat < bottom )
                                  {
                                      bottom = p.Lat;
                                  }
                              }
                          }
                      }
                  }
              }
          }

          if ( left != double.MaxValue && right != double.MinValue && top != double.MinValue && bottom != double.MaxValue )
          {
              ret = RectLatLng.FromLTRB( left, top, right, bottom );
          }

          return ret;
      }

      /// <summary>
      /// zooms and centers all polygons
      /// </summary>
      /// <param name="overlayId">overlay id or null to check all</param>
      /// <returns></returns>
      public bool ZoomAndCenterPolygons( string overlayId )
      {
          RectLatLng? rect = GetRectOfAllPolygons( overlayId );
          if ( rect.HasValue )
          {
              return SetZoomToFitRect( rect.Value );
          }

          return false;
      }

      /// <summary>
      /// zooms and centers all objects
      /// </summary>
      /// <param name="overlayId">overlay id or null to check all</param>
      /// <returns></returns>
      public bool ZoomAndCenterAll( string overlayId )
      {
          //grab the max of each object
          RectLatLng? markers = GetRectOfAllMarkers( overlayId );
          RectLatLng? routes = GetRectOfAllRoutes( overlayId );
          RectLatLng? polygons = GetRectOfAllPolygons( overlayId );

          List<RectLatLng> all = new List<RectLatLng>();

          //adds to the list only those with some return
          if ( markers.HasValue ) all.Add( markers.Value );
          if ( routes.HasValue ) all.Add( routes.Value );
          if ( polygons.HasValue ) all.Add( polygons.Value );

          double left = double.MaxValue;
          double top = double.MinValue;
          double right = double.MinValue;
          double bottom = double.MaxValue;

          //loop through the list to build a new rect
          foreach ( RectLatLng r in all )
          {
              // left
              if ( r.Left < left )
              {
                  left = r.Left;
              }

              // top
              if ( r.Top > top )
              {
                  top = r.Top;
              }

              // right
              if ( r.Right > right )
              {
                  right = r.Right;
              }

              // bottom
              if ( r.Bottom < bottom )
              {
                  bottom = r.Bottom;
              }
          }

          if ( left != double.MaxValue && right != double.MinValue && top != double.MinValue && bottom != double.MaxValue )
          {
              RectLatLng rect = RectLatLng.FromLTRB( left, top, right, bottom );

              SetZoomToFitRect( rect );

              return true;
          }
          else
          {
              return false;
          }
      }

I hope my code proves itself useful.

Regards,

Cleverson Nascimento

Jul 8, 2012 at 2:30 PM

nice, why not add ZoomAndCenterPolygon with a polygon name?

      /// <summary>
      /// gets rectangle of a polygon
      /// </summary>
      /// <param name="overlayId">overlay id or null to check all</param>
      /// <returns></returns>
      public RectLatLng? GetRectOfPolygon(string polygonName)
      {
          RectLatLng? ret = null;

          double left = double.MaxValue;
          double top = double.MinValue;
          double right = double.MinValue;
          double bottom = double.MaxValue;

          foreach (GMapOverlay o in Overlays)
          {
                  if (o.IsVisibile && o.Polygons.Count > 0)
                  {
                      foreach (GMapPolygon poly in o.Polygons)
                      {
                          if (poly.IsVisible && poly.From.HasValue && poly.To.HasValue && poly.Name.Equals(polygonName))
                          {
                              foreach (PointLatLng p in poly.Points)
                              {
                                  // left
                                  if (p.Lng < left)
                                  {
                                      left = p.Lng;
                                  }

                                  // top
                                  if (p.Lat > top)
                                  {
                                      top = p.Lat;
                                  }

                                  // right
                                  if (p.Lng > right)
                                  {
                                      right = p.Lng;
                                  }

                                  // bottom
                                  if (p.Lat < bottom)
                                  {
                                      bottom = p.Lat;
                                  }
                              }
                          }
                      }
              }
          }

          if (left != double.MaxValue && right != double.MinValue && top != double.MinValue && bottom != double.MaxValue)
          {
              ret = RectLatLng.FromLTRB(left, top, right, bottom);
          }

          return ret;
      }

 

      /// <summary>
      /// zooms and centers polygon
      /// </summary>
      /// <param name="overlayId">overlay id or null to check all</param>
      /// <returns></returns>
      public bool ZoomAndCenterPolygon(string polygonName)
      {
          RectLatLng? rect = GetRectOfPolygon(polygonName);
          if (rect.HasValue)
          {
              return SetZoomToFitRect(rect.Value);
          }

          return false;
      }