SuperMap.Connector.Control.Forms.MapControl.GraphicsLayers_CollectionChanged C# (CSharp) Method

GraphicsLayers_CollectionChanged() private method

private GraphicsLayers_CollectionChanged ( object sender, NotifyCollectionChangedEventArgs e ) : void
sender object
e NotifyCollectionChangedEventArgs
return void
        private void GraphicsLayers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add && e.NewItems != null)
            {
                foreach (GraphicsLayer item in e.NewItems)
                {
                    GMapOverlay overlay = new GMapOverlay(item.ID);
                    if (_layerMapOverlay.ContainsKey(item.ID))
                    {
                        this.GraphicsLayers.Remove(_layerMapOverlay[item.ID].GraphicsLayer);
                    }
                    if (!_layerMapOverlay.ContainsKey(item.ID))
                    {
                        _layerMapOverlay.Add(item.ID, new GraphicsLayerMapOverlay()
                        {
                            GraphicsLayer = item,
                            GMapOverlay = overlay
                        });
                        this.gMapControl1.Overlays.Add(overlay);
                        item.PolygonsChanged += new NotifyCollectionChangedEventHandler(item_PolygonChanged);
                        item.LineCollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(item_LineCollectionChanged);
                        item.MarkCollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(item_MarkCollectionChanged);
                        if (item.Polygons != null && item.Polygons.Count > 0)
                        {
                            if (!_polygonMapGMapPolygon.ContainsKey(item.ID))
                            {
                                _polygonMapGMapPolygon.Add(item.ID, new Dictionary<string, PolygonMapGMapPolygon>());
                            }
                            foreach (Polygon polygonItem in item.Polygons)
                            {
                                if (polygonItem != null)
                                {
                                    GMapPolygon gMapPolygon = new GMapPolygon(Helper.Point2Ds2PointLatLngs(polygonItem.Point2Ds), polygonItem.ID);
                                    gMapPolygon.IsVisible = true;
                                    gMapPolygon.IsHitTestVisible = true;
                                    gMapPolygon.Fill = new SolidBrush(polygonItem.FillColor);
                                    gMapPolygon.Stroke = new Pen(polygonItem.StrokeColor, (float)polygonItem.StrokeWeight);
                                    overlay.Polygons.Add(gMapPolygon);
                                    _polygonMapGMapPolygon[item.ID].Add(polygonItem.ID, new PolygonMapGMapPolygon(polygonItem, gMapPolygon));
                                }
                            }
                        }
                        if (item.Lines != null && item.Lines.Count > 0)
                        {
                            this.AddLine(item, overlay, item.Lines);
                        }
                        if (item.Markers != null && item.Markers.Count > 0)
                        {
                            this.AddMarker(item, overlay, item.Markers);
                        }
                    }
                }
            }
            if (e.Action == NotifyCollectionChangedAction.Remove && e.OldItems != null)
            {
                foreach (GraphicsLayer item in e.OldItems)
                {
                    if (_layerMapOverlay.ContainsKey(item.ID))
                    {
                        item.PolygonsChanged -= item_PolygonChanged;
                        item.LineCollectionChanged -= item_LineCollectionChanged;
                        item.MarkCollectionChanged -= item_MarkCollectionChanged;
                        this.gMapControl1.Overlays.Remove(_layerMapOverlay[item.ID].GMapOverlay);
                        _layerMapOverlay.Remove(item.ID);
                        _polygonMapGMapPolygon.Remove(item.ID);
                        _lineMapGMapRoute.Remove(item.ID);
                        _markerMapGMapMarker.Remove(item.ID);
                    }
                }
            }
            if (e.Action == NotifyCollectionChangedAction.Reset)
            {
                foreach (KeyValuePair<string, GraphicsLayerMapOverlay> pair in _layerMapOverlay)
                {
                    pair.Value.GraphicsLayer.PolygonsChanged -= item_PolygonChanged;
                    pair.Value.GraphicsLayer.MarkCollectionChanged -= item_MarkCollectionChanged;
                    pair.Value.GraphicsLayer.LineCollectionChanged -= item_LineCollectionChanged;
                }

                this.gMapControl1.Overlays.Clear();
                foreach (GMapOverlay item in this.gMapControl1.Overlays)
                {
                    if (item != null)
                    {
                        item.Markers.Clear();
                        item.Polygons.Clear();
                        item.Routes.Clear();
                    }
                }

                this._layerMapOverlay.Clear();
                _polygonMapGMapPolygon.Clear();
                _lineMapGMapRoute.Clear();
                _markerMapGMapMarker.Clear();
                this.Refresh();
            }
        }