Hello,
I can not figure out how to zoom the map frame in layout view to a specific layer using ArcGIS Pro SDK (C#)?
Thank you!
Hi Joshua
Here is a code snippet you can use to zoom the map frame in layout view to a specific layer:
LayoutProjectItem layoutItem <SPAN class="operator token">=</SPAN> Project<SPAN class="punctuation token">.</SPAN>Current<SPAN class="punctuation token">.</SPAN>GetItems<SPAN class="operator token"><</SPAN>LayoutProjectItem<SPAN class="operator token">></SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">FirstOrDefault</SPAN><SPAN class="punctuation token">(</SPAN>item <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> item<SPAN class="punctuation token">.</SPAN>Name<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Equals</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"MyLayout"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> QueuedTask<SPAN class="punctuation token">.</SPAN><SPAN class="token function">Run</SPAN><SPAN class="punctuation token">(</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">=</SPAN><SPAN class="operator token">></SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">//Get the layout from the layout project item</SPAN> Layout layout <SPAN class="operator token">=</SPAN> layoutItem<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetLayout</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Get the map frame in the layout</SPAN> MapFrame mapFrame <SPAN class="operator token">=</SPAN> layout<SPAN class="punctuation token">.</SPAN><SPAN class="token function">FindElement</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"New Map Frame"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">as</SPAN> MapFrame<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Get map </SPAN> <SPAN class="keyword token">var</SPAN> mapFrameMap <SPAN class="operator token">=</SPAN> mapFrame<SPAN class="punctuation token">.</SPAN>Map<SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Get the specific layer you want from the map</SPAN> <SPAN class="keyword token">var</SPAN> lyrOfInterest <SPAN class="operator token">=</SPAN> mapFrameMap<SPAN class="punctuation token">.</SPAN><SPAN class="token function">GetLayersAsFlattenedList</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>OfType<SPAN class="operator token"><</SPAN>FeatureLayer<SPAN class="operator token">></SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="token function">FirstOrDefault</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Get the Layer's extent</SPAN> <SPAN class="keyword token">var</SPAN> lyrExtent <SPAN class="operator token">=</SPAN> lyrOfInterest<SPAN class="punctuation token">.</SPAN><SPAN class="token function">QueryExtent</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">//Zoom to this extent in the MapFrame</SPAN> <SPAN class="comment token">//Note: This method will be deprecated at 2.3. </SPAN> <SPAN class="comment token">//But you can use mapFrame.SetCamera(lyrOfInterest) instead. (This will be a new overload available in 2.3)</SPAN> mapFrame<SPAN class="punctuation token">.</SPAN><SPAN class="token function">ZoomTo</SPAN><SPAN class="punctuation token">(</SPAN>lyrExtent<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Thanks
Uma
Thank you this worked great! But is there a way to 1) zoom to only a selected feature in a layer rather than the whole layer, and then 2) zoom out a certain percentage more? For example, zoom to California in the states layer. And then zoom out a bit more so that it is not zoomed all the way in to California?
Thank you
Hi Joshua,
The way I got this to work was to create a query and iterate using a cursor. You want to make sure your query returns one feature.
////Zoom to the envelope of each feature //Reference the layout Layout layout = LayoutView.Active.Layout;
//Select the feature await QueuedTask.Run(() => { //Reference the mapframe, associated map, and build a query MapFrame mf = layout.FindElement("Map Frame") as MapFrame; Map m = mf.Map; FeatureLayer fl = m.FindLayers("GreatLakes").First() as FeatureLayer; QueryFilter qf = new QueryFilter(); string whereClause = "NAME = 'Lake Erie'"; qf.WhereClause = whereClause;
//Zoom to the feature using (ArcGIS.Core.Data.RowCursor rowCursor = fl.Search(qf)) { while (rowCursor.MoveNext()) { // get the shape from the row ArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature; Polygon polygon = feature.GetShape() as Polygon; Envelope env = polygon.Extent as Envelope; mf.ZoomTo(env); } } });
I hope this helps,
Jeff
Thank you! I am trying to zoom out a certain percentage further, so for example, zoom to Lake Erie, but then zoom out 10% further. Is that possible? Also, I am trying to do a select by location (select state that intersects with a polygon project boundary, and then zoom to that state (but out a bit further such as 10%).
Just FYI. I was able to modify a little to make it work for my case where I need to zoom to a feature from certain feature layer in Map view. Hope it helps anyone.
await QueuedTask.Run(() =>{//Reference the mapframe, associated map, and build a queryFeatureLayer fl = MapView.Active.Map.FindLayers("FeatureLayerName").First() as FeatureLayer;QueryFilter qf = new QueryFilter();string whereClause = "MyField = 'FieldValue'";qf.WhereClause = whereClause;
//Zoom to the featureusing (ArcGIS.Core.Data.RowCursor rowCursor = fl.Search(qf)){while (rowCursor.MoveNext()){// get the shape from the rowArcGIS.Core.Data.Feature feature = rowCursor.Current as ArcGIS.Core.Data.Feature;Polygon polygon = feature.GetShape() as Polygon;Envelope env = polygon.Extent as Envelope;//mf.ZoomTo(env);MapView.Active.ZoomTo(env);
}}});
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.