Select to view content in your preferred language

Force map in layout to zoom to selected feature

186
3
3 weeks ago
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
3 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
SumitMishra_016
Frequent Contributor
  • GetActiveView is not used — MapFrame doesn’t have that method.

  • Used MapFrame.Camera to control what’s shown in the layout map frame.

  • Used FeatureLayer.QueryExtent on the selection to get the exact bounding box.

  • Applied SetExtent to zoom out to that full selection in the layout view.

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

        // Create new layout
        var layout = LayoutFactory.Instance.CreateLayout(newReport);
        layout.SetName("Report for PAN " + lookupPAN);

        // Get the current map
        Map currMap = MapView.Active.Map;

        // Map frame position on layout
        Coordinate2D ur = new Coordinate2D(8.0, 9.75);
        Coordinate2D ll = new Coordinate2D(.5, 6);
        Envelope mapEnv = EnvelopeBuilderEx.CreateEnvelope(ur, ll);

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

        // Get the parcel layer and selection
        var layer = currMap.GetLayersAsFlattenedList()
                           .OfType<FeatureLayer>()
                           .FirstOrDefault(fl => fl.Name.Contains("Parcel"));

        if (layer != null)
        {
            var oidSet = layer.GetSelection().GetObjectIDs();
            if (oidSet.Count > 0)
            {
                // Get extent of the selection
                var selExtent = layer.QueryExtent(layer.GetSelection());

                if (selExtent != null && !selExtent.IsEmpty)
                {
                    // Get current camera for the map frame
                    var cam = newMapFrame.Camera;

                    // Set the camera to the selection extent
                    cam.SetExtent(selExtent);

                    // Apply the updated camera to the map frame
                    newMapFrame.SetCamera(cam);
                }
            }
        }

        return layout;
    });

    // Open layout pane
    await ProApp.Panes.CreateLayoutPaneAsync(newLayout);
}

 

0 Kudos