ThinkGeo.com    |     Documentation    |     Premium Support

Add polygon to wpf map

 Hi,


I try to add polygon to the map , without success.


I use the following code:   what wrong?


 



private void InitializeOverlays()


        {


.......



 if (Map.Overlays.GetKeys().Contains(lAYERS_OVERLAY) == false)


            {


                InMemoryFeatureLayer polygonrlayer = new InMemoryFeatureLayer();


                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.CreateSimpleAreaStyle(GeoColor.FromArgb(150, GeoColor.StandardColors.Red));


                polygonrlayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;


 


                LayerOverlay layerOverlay = new LayerOverlay();


                layerOverlay.Layers.Add(POLYGON_LAYER, polygonrlayer);


 


                Map.Overlays.Add(lAYERS_OVERLAY, layerOverlay);


            }



 


}


 



   private void AddPolygons( )


        {


            InMemoryFeatureLayer polygonLayer = (InMemoryFeatureLayer)Map.FindFeatureLayer(POLYGON_LAYER);


           polygonLayer.Open();


           polygonLayer.EditTools.BeginTransaction();


                    Feature feature = new Feature("POLYGON((14.863037109375 47.722412109375,14.80810546875 47.634521484375,14.928955078125 47.27197265625,15.09375 47.22802734375,15.236572265625 47.348876953125,15.324462890625 47.524658203125,15.324462890625 47.8212890625,15.15966796875 47.9091796875,14.863037109375 47.722412109375))");


                var valid = feature.IsValid();


             polygonLayer.EditTools.Add(feature);


                 }


         


            polygonLayer.EditTools.CommitTransaction();


            Map.CurrentExtent = Map.Overlays[lAYERS_OVERLAY].GetBoundingBox();


           Map.Refresh(Map.Overlays[lAYERS_OVERLAY]);


           polygonLayer.Close();


            


        }


 


Thanks alot!




 



Hi, 
  
 It’s work now .but I need to add the layer every time I add the polygon (  Map.Overlays.Add(layerOverlay); ) 
  
 Why?

Hi Yael, 
  
 Could you post your latest code or your sample project which works? That will help and speed up to figure out what’s going on. 
  
 Thanks, 
 Lee

Hi, 
 this is the codeto init the  (after add some other overlays) 
 
 private void InitializeOverlays()
        {
        //…init other overlays

            //Overlay for polygons
            if (Map.Overlays.GetKeys().Contains(lAYERS_OVERLAY) == false)
            {
                InMemoryFeatureLayer polygonrlayer = new InMemoryFeatureLayer();
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(100, GeoColor.StandardColors.RoyalBlue);
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.StandardColors.Blue;
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.FromArgb(200, GeoColor.StandardColors.Red), 5);
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.SymbolPen = new GeoPen(GeoColor.FromArgb(255, GeoColor.StandardColors.Green), 8);
                polygonrlayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
              
                LayerOverlay layerOverlay = new LayerOverlay();
                layerOverlay.Layers.Add(POLYGON_LAYER, polygonrlayer);
                Map.Overlays.Add(lAYERS_OVERLAY, layerOverlay);
            }
}
 
 
 this is the code of init the polygons (It’s called several times during the application running) 
 
private void AddPolygons(List<Location> listLocations)
        {
  
            InMemoryFeatureLayer polygonLayer = (InMemoryFeatureLayer)Map.FindFeatureLayer(POLYGON_LAYER);
               

            foreach (var location in listLocations)
            { 
                var polygonShape =  CreatelPolygon(locationr);  
                if (polygonShape.Validate(ShapeValidationMode.Simple).IsValid)
                    polygonLayer.InternalFeatures.Add(new Feature(polygonShape)); 

            } 
               
           
            var layerOverlay = (LayerOverlay)Map.Overlays[lAYERS_OVERLAY];
             Map.Overlays.Add(layerOverlay);
            // if I delete the line above and use the following line it don’t work
          //  layerOverlay.Refresh(); 
}
 
 
  
 Thanks!

yael, 



Thanks for your code, I have tested it, and looks like it works fine, is there something I missed? Please check my test code below:



private void WpfMap_Loaded(object sender, RoutedEventArgs e)
        {
            Map1.MapUnit = GeographyUnit.DecimalDegree;

            if (Map1.Overlays.GetKeys().Contains("lAYERS_OVERLAY") == false)
            {
                InMemoryFeatureLayer polygonrlayer = new InMemoryFeatureLayer();
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.FillSolidBrush.Color = GeoColor.FromArgb(100, GeoColor.StandardColors.RoyalBlue);
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle.OutlinePen.Color = GeoColor.StandardColors.Blue;
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultLineStyle.OuterPen = new GeoPen(GeoColor.FromArgb(200, GeoColor.StandardColors.Red), 5);
                polygonrlayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle.SymbolPen = new GeoPen(GeoColor.FromArgb(255, GeoColor.StandardColors.Green), 8);
                polygonrlayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
                polygonrlayer.InternalFeatures.Add(new Feature("POLYGON((14.863037109375 47.722412109375,14.80810546875 47.634521484375,14.928955078125 47.27197265625,15.09375 47.22802734375,15.236572265625 47.348876953125,15.324462890625 47.524658203125,15.324462890625 47.8212890625,15.15966796875 47.9091796875,14.863037109375 47.722412109375))"));
                LayerOverlay layerOverlay = new LayerOverlay();
                layerOverlay.Layers.Add("POLYGON_LAYER", polygonrlayer);
                Map1.Overlays.Add("lAYERS_OVERLAY", layerOverlay);
            }
            Map1.CurrentExtent = Map1.Overlays[0].GetBoundingBox();
            Map1.Refresh();
        }
        private void AddPolygons()
        {

            InMemoryFeatureLayer polygonLayer = (InMemoryFeatureLayer)Map1.FindFeatureLayer("POLYGON_LAYER");

            //foreach (var location in listLocations)
            //{
                //var polygonShape = CreatelPolygon(locationr);
                //if (polygonShape.Validate(ShapeValidationMode.Simple).IsValid)
                polygonLayer.InternalFeatures.Add(new Feature("POLYGON((15.863037109375 47.722412109375,15.80810546875 47.634521484375,14.928955078125 47.27197265625,15.09375 47.22802734375,15.236572265625 47.348876953125,15.324462890625 47.524658203125,15.324462890625 47.8212890625,15.15966796875 47.9091796875,15.863037109375 47.722412109375))"));
                polygonLayer.InternalFeatures.Add(new Feature("POLYGON((12.863037109375 47.722412109375,15.80810546875 47.634521484375,14.928955078125 47.27197265625,15.09375 47.22802734375,15.236572265625 47.348876953125,15.324462890625 47.524658203125,15.324462890625 47.8212890625,15.15966796875 47.9091796875,12.863037109375 47.722412109375))"));
                polygonLayer.InternalFeatures.Add(new Feature("POLYGON((18.863037109375 47.722412109375,15.80810546875 47.634521484375,14.928955078125 47.27197265625,15.09375 47.22802734375,15.236572265625 47.348876953125,15.324462890625 47.524658203125,15.324462890625 47.8212890625,15.15966796875 47.9091796875,18.863037109375 47.722412109375))"));
            //}
            var layerOverlay = (LayerOverlay)Map1.Overlays["lAYERS_OVERLAY"];
            layerOverlay.Refresh();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            AddPolygons();
        }



Any more questions please feel free to let me know. 



Thanks, 



Gary