ArcGIS Pro SDK add FeatureLayer to map

3447
5
05-18-2020 10:34 AM
FilipKuzman
New Contributor III

I know this is a very simple question, however, I find it difficult to find an answer. How do I add an existing layer to my map using ArcGIS Pro SDK? I saw several examples where the layer is added while being created (for example

LayerFactory.Instance.CreateFeatureLayer(new Uri(strUri), map);

), however, how this is being done using the existing FeatureLayer?

Thanks

0 Kudos
5 Replies
Amadeus111
Occasional Contributor II

 int indexNumber = 0;
 shp_path = "C:\path to your shapefile"
 var mapView = MapView.Active;

 if (mapView != null)//Check if there is a map
 {
    System.Uri shp_uri = new System.Uri(@shp_path); //creating uri for the shpfile
    QueuedTask.Run(() =>
    {
       Layer shp_layer = LayerFactory.Instance.CreateLayer(
         shp_uri, mapView.Map, indexNumber, "YourlayerName");
       var selectedLayer = mapView.GetSelectedLayers()[0] as FeatureLayer
      //Do something with selected layer
    });
 }


serbanmarin1
New Contributor II

If you already have a reference to your Layer, try using

LayerFactory.Instance.CopyLayer(sourceLayer, map);

FilipKuzman
New Contributor III

Thank you guys, 

I have succeeded with Serban's answer. That is what I was looking for.

Have a great day!

0 Kudos
DavidWilton
Occasional Contributor

If you have a featureclass:

 

var layerParams = new FeatureLayerCreationParams(fc);
var layer = LayerFactory.Instance.CreateLayer<FeatureLayer>(layerParams, MapView.Active.Map);
CarlosPiccirillo2
New Contributor II

Does this automatically add the copied layer to the current map? Accrding to ChatGPT, it does not but every suggestion it has is wrong, i.e., map.AddLayer(copiedLayer); as Class Map does not have a AddLayer method. Same goes for MapView.Add. etc. 

0 Kudos