private void OnActiveViewEventsAfterDraw(ESRI.ArcGIS.Display.IDisplay display, ESRI.ArcGIS.Carto.esriViewDrawPhase phase)
        {
            ESRI.ArcGIS.Carto.esriViewDrawPhase m_phase = phase;

            //if the drawing pahse geography, find all feature layer and selected feature and draw them on screen if they are polygons. Please note don't call   display::StartDrawing as it is already started by the system.
            if (m_phase == ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeography)
            {
                IMap m_Map = m_MxDoc.FocusMap;
                ESRI.ArcGIS.esriSystem.UID m_UID = new ESRI.ArcGIS.esriSystem.UID();
                m_UID.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
                IEnumLayer m_EnumLayer = m_Map.Layers[m_UID];
                ILayer     m_Layer     = m_EnumLayer.Next();

                //if you want to change the selection color you can change it here.
                ISimpleFillSymbol m_FillSymbol = new SimpleFillSymbol();
                m_Rgb              = new RgbColor();
                m_Rgb.Red          = 255;
                m_FillSymbol.Color = m_Rgb;
                display.SetSymbol(m_FillSymbol as ISymbol);

                do
                {
                    if (m_Layer is IFeatureLayer)
                    {
                        if (m_Layer != null)
                        {
                            IFeatureSelection m_FeatureSelection = (IFeatureSelection)m_Layer;
                            ISelectionSet     m_SelSet           = m_FeatureSelection.SelectionSet;
                            IFeatureCursor    m_FeatCur;
                            ICursor           m_Cursor;
                            m_SelSet.Search(null, false, out m_Cursor);

                            m_FeatCur = (IFeatureCursor)m_Cursor;
                            IFeature m_Feature;

                            m_Feature = m_FeatCur.NextFeature();

                            do
                            {
                                if (m_Feature != null)
                                {
                                    if (m_Feature.Shape is IPolygon)
                                    {
                                        display.DrawPolygon(m_Feature.Shape);
                                    }
                                }
                                m_Feature = m_FeatCur.NextFeature();
                            } while (m_Feature != null);
                        }
                    }
                    m_Layer = m_EnumLayer.Next();
                } while (m_Layer != null);
            }
            #endregion
        }
 private void OnActiveViewEventsViewRefreshed(ESRI.ArcGIS.Carto.IActiveView view, ESRI.ArcGIS.Carto.esriViewDrawPhase phase, System.Object data, ESRI.ArcGIS.Geometry.IEnvelope envelope)
 {
     if (!_iscloned)
     {
         return;
     }
     if (!_isthisadmin)
     {
         return;
     }
     try
     {
         //first check if the map extent has changed
         if (ExtentHasChanged())
         {
             //this flag is to avoid an infinite loop; any change to the map will cause this 'view refresh'
             //event to be fired again
             if (_firstRefresh)
             {
                 _firstRefresh = false;
                 using (StreamWriter sw = new StreamWriter(_extentfile))
                 {
                     sw.WriteLine(_currentExtent.XMin.ToString());
                     sw.WriteLine(_currentExtent.XMax.ToString());
                     sw.WriteLine(_currentExtent.YMin.ToString());
                     sw.WriteLine(_currentExtent.YMax.ToString());
                     sw.WriteLine(Math.Round(_map.MapScale).ToString());
                 }
             }
         }
     }
     catch (Exception ex)
     {
         string message = ex.Message;
     }
 }
        private void OnActiveViewEventsViewRefreshed(ESRI.ArcGIS.Carto.IActiveView view, ESRI.ArcGIS.Carto.esriViewDrawPhase phase, System.Object data, ESRI.ArcGIS.Geometry.IEnvelope envelope)
        {
            IPoint point           = new Point();
            IPoint lowerLeftPoint  = new Point();
            IPoint upperRightPoint = new Point();


            double latXmin, latXmax, longYmin, longYmax, diagonal;

            lowerLeftPoint.X = view.Extent.XMin;
            lowerLeftPoint.Y = view.Extent.YMin;

            upperRightPoint.X = view.Extent.XMax;
            upperRightPoint.Y = view.Extent.YMax;

            PointToLatLong(lowerLeftPoint, out latXmin, out longYmin);
            PointToLatLong(upperRightPoint, out latXmax, out longYmax);

            diagonal = Distance(latXmin, longYmin, latXmax, longYmax, 'K') * 1000; // "1KM * 1000
            diagonal = Math.Round(diagonal, 2);

            _altitude = Convert.ToString(0.5 * Math.Sqrt(3) * diagonal, CultureInfo.InvariantCulture);

            point.X = (view.Extent.XMax + view.Extent.XMin) / 2;
            point.Y = (view.Extent.YMax + view.Extent.YMin) / 2;

            Double lat;
            Double lon;

            PointToLatLong(point, out lat, out lon);

            lat = Math.Round(lat, 5);
            lon = Math.Round(lon, 5);

            _latitude  = Convert.ToString(lat, CultureInfo.InvariantCulture);
            _longitude = Convert.ToString(lon, CultureInfo.InvariantCulture);

            CreateTrackingKML();
        }