Hi,
I have a question about how adding a layer dynamically a layer (shapefile, jpegfile, etc …)
Opening the software, I have for example a map which has 4 overlays and each overlay has 3 layers.
The users click on a button “add”, this button calls the windows explorer and the user choose a shapefile that he wants to see on the map.
I want to add this layer in an overlay which already exists.
And my question is how to add dyamically this shapefile ?
Currently, the only way that I found is to remove the overlay and add the overlay with these 4 layers (3 + one shapefile ) .
Is there on other way to do that ? …
The code is :
(Retrieving layers in the overlay. ==> nb of layers is 3)
Collection<Layer>layers=MapVm.ServiceMapOverlaysConfiguration[MapVm.ServiceMapActif.GetIdServiceMap()].Overlays[ConstantsMap.NOMOVERLAYSTATIC+"_KEY"].Layers;
(Creation of the new layer and add in the collection of the layers ==> nb of layers is 4)
layers.Add(s.CreateLayer(projLayer.SRID,OutilsOvLyr.ProjectionAffichage));
(deleting the overlay)
MapVm.MapActif.Overlays.Remove(ConstantsMap.NOMOVERLAYSTATIC + “_KEY”);
(re-creation of the overlay)
LayerOverlay ovLy = s.CreateOverlay();
addition of the 4 layers in the overlay
foreach (Layer l in layers)
{
ovLy.Layers.Add(l);
}
(addition of the overlay)
MapVm.MapActif.Overlays.Add(ovLy);
MapVm.MapActif.Refresh();
I thought about an other solution is to add a new overlay with one layer … but the user can add a lot of layers.
And for better performance, it’s better to have few overlays which contains a lot of layers than a lot of overlays which contains few layers … ;-(
Thanks a lot for your help.
Regards.
Steph.
Add dynamically shapeFile
Hi Steph,
We don’t need to create a new layeroverlay, but insert the new layer into the layeroverlay directly and then refresh the layeroverlay should make the new layer drawn. some codes like :
private
void
btnAddlayer_Click_1(
object
sender, RoutedEventArgs e)
{
LayerOverlay staticOverlay = wpfMap1.Overlays[
“WorldOverlay”
]
as
LayerOverlay;
ShapeFileFeatureLayer worldLayer =
new
ShapeFileFeatureLayer(@
"…\SampleData\Data\WorldCapitals.shp"
);
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.Capital1;
staticOverlay.Layers.Add(worldLayer);
wpfMap1.Refresh(staticOverlay);
}
Hope it helps.
Thanks,
Troy
Hi Troy,
Indeed, it is much simpler … ;-)
Thanks.
Regards.
Steph.
Steph,
Good to hear it helps.
Thanks,
Troy