Select to view content in your preferred language

ArcGIS Pro SDK add FeatureLayer to map

4181
5
05-18-2020 10:34 AM
FilipKuzman
Occasional Contributor

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
Frequent Contributor

 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
Emerging Contributor

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

LayerFactory.Instance.CopyLayer(sourceLayer, map);

FilipKuzman
Occasional Contributor

Thank you guys, 

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

Have a great day!

0 Kudos
DavidWilton
Frequent Contributor

If you have a featureclass:

 

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

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