Select to view content in your preferred language

Force map in layout to zoom to selected feature

57
2
Thursday
RogerAsbury
Frequent Contributor

I asked a similar question a while back and while I got an answer that is correct, it looks like the question I asked wasn't really what I was trying to do. For reference: Solved: Re: MapView.Active.Map not showing the actually ac... - Esri Community

Technically, that's a correct answer for what I asked. And the solution provided does exactly what you expect it to do: In the MapFrame, the came shows exactly the same area as what is in the active mapview. This works great if the MapFrame and MapView are the same size. 

However, if they are not the MapFrame may cut off parts of the selected feature. Below shows what I'm talking about. The first image is the map, the second is what the mapframe shows in the layout.

Mapview showing selected feature.Mapview showing selected feature.Layout view showing only part of selected feature.Layout view showing only part of selected feature.

The question then, is how do I force the layout view to zoom out so the entire feature shows up? I can get this to work by clicking the map, going to the layout menu and clicking the Zoom To Map View icon, but I'd like to do it programmatically. 

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
Tags (3)
0 Kudos
2 Replies
CharlesMacleod
Esri Regular Contributor

Zoom To Map View takes the extent from the map u pick (on the Zoom to Map View dropdown) and applies it to the map view in the (selected) map frame. So something like

var mv = mapFrame.GetActiveView(LayoutView.Active);

mv.ZoomTo(whateverExtentYouWant);//Assuming 2D map and so forth

0 Kudos
RogerAsbury
Frequent Contributor

I seem to get errors when I try what you suggest. I'll post the code below with comments the error.

private async void BtnGenReport_Click(object sender, RoutedEventArgs e)
{
	// Generate the report
	Layout newLayout = await QueuedTask.Run<Layout>(async () =>
	{
		CIMPage newReport = new CIMPage();
		// Add Properties
		newReport.Width = 8.5;
		newReport.Height = 11;
		newReport.Units = LinearUnit.Inches;
		// Add rulers
		newReport.ShowRulers = true;
		newReport.SmallestRulerDivision = 0.5;

		// Apply the CIM page to a new layout and set name
		newLayout = LayoutFactory.Instance.CreateLayout(newReport);
		newLayout.SetName("Report for PAN " + lookupPAN);

		// The map to add to this layout is the current map.
		Map currMap = MapView.Active.Map;

		// Build a map frame geometry and envelope
		Coordinate2D ur = new Coordinate2D(8.0, 9.75);
		Coordinate2D ll = new Coordinate2D(.5, 6);
		Envelope mapEnv = EnvelopeBuilderEx.CreateEnvelope(ur, ll);

		// Create the map frame and add it to the layout.
		MapFrame newMapFrame = ElementFactory.Instance.CreateMapFrameElement(newLayout, mapEnv, currMap);
		newMapFrame.SetName("Selected Area");

		//THE FOLLOWING LINE PRODUCES THE ERROR
        //MapFrame DOES NOT CONTAIN A DEFINITION FOR GetActiveView
        var mv = newMapFrame.GetActiveView(LayoutView.Active);
		var layer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains("Parcel")).FirstOrDefault();
		var selection = layer.GetSelection();
		mv.ZoomTo(selection);

		return newLayout;
	});

	var layoutPane = await ProApp.Panes.CreateLayoutPaneAsync(newLayout);
}

 

--
Roger Asbury
Analyst/Programmer - Fairbanks North Star Borough
0 Kudos