Select to view content in your preferred language

popupdefinition=null ArcGIS Runtime SDK for .NET

2170
6
08-25-2017 07:14 AM
RomanBoros
Occasional Contributor

Hi, I am trying to show popups in ArcGIS Runtime SDK for .NET, but even with defined popup definitions on the portal when I retrieve the feature layer from the portal I get popupdefinition=null. Should I invoke a special method which will pull the popup definitions? I couldn't find anything like that in the documentation.  

0 Kudos
6 Replies
JoeHershman
MVP Alum

Have you called LoadAsync() on the feature layer?

Thanks,
-Joe
0 Kudos
RomanBoros
Occasional Contributor

Yes I am calling it, but anyway I don't get the PopupDefinition, it's null.

Any other ideas? 

0 Kudos
JoeHershman
MVP Alum

How are you getting to the FeatureLayer?  A FeatureLayer by itself does not have a PopupDefinition.  The only time a FeatureLayer object would have a PopupDefinition is if it is in a WebMap.  You would need to create a WebMap in Portal.  In the application you open the WebMap and then get to the FeatureLayer through the OperationLayers collection.

ArcGISRuntimeEnvironment.Initialize();

Credential credential =
     await AuthenticationManager.Current.GenerateCredentialAsync(
          new Uri("https://server.domain.com/portal/sharing/rest"), "", "");

ArcGISPortal portal =
     await ArcGISPortal.CreateAsync(new Uri("https://server.domain.com/portal"), credential);

PortalItem item = await PortalItem.CreateAsync(portal, "02f85448eb1049228c44065658984a0a");
Map map = new Map(item);
await map.LoadAsync();

foreach (var layer in map.OperationalLayers)
{
     //if configured PopupDefinition is not null here
}

FeatureLayer featureLayer = new FeatureLayer(new Uri("https://server.domain.com/server/rest/services/Mobile/ServiceName/FeatureServer/0"), credential);
await featureLayer.LoadAsync();
//PopupDefinition is null here
Thanks,
-Joe
JenniferNery
Esri Regular Contributor

As Joe already mentioned, FeatureLayer popup can be configured on the web map. But you can also configure it in the client. For example, try the following code (see current value of popup):

            MyMapView.Map = new Map(new Uri("https://www.arcgis.com/home/webmap/viewer.html?webmap=ff0e158ffb184e41b8cea71ebda3eb67"));
            MyMapView.LayerViewStateChanged += (s, e) =>
              {
                  if (e.Layer is FeatureLayer && e.LayerViewState.Status == LayerViewStatus.Active)
                  {
                      var popup = ((FeatureLayer)e.Layer).PopupDefinition;
                      
                  }
              };
        }

        private void OnLoad(object sender, RoutedEventArgs e)
        {
            var layer = new FeatureLayer(new Uri("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Wildfire/FeatureServer/0"));
            layer.PopupDefinition = new PopupDefinition() { Title = "My Title" };
            layer.PopupDefinition.Fields.Add(new PopupField() { Label = "description", FieldName = "description ", IsEditable = true, IsVisible = true, StringFieldOption = PopupStringFieldOption.SingleLine });
            MyMapView.Map.OperationalLayers.Add(layer);
        }
HS_PolkGIS
Occasional Contributor

Realize this is old but does anyone have a working example of creating Popup Definitions and displaying in a popupviewer?

Creating a popupdefinition and adding fields displays nothing except the Title in the popupviewer. Clearly missing something in the latest version.

0 Kudos
KevinL20
New Contributor

In PopupDefinition the Fields property only pertains to the order that the fields will be shown. The Elements property of PopupDefinition must be initialized so that the fields will appear in the callout.

Here's an example from a project that I've been working on recently:

//fields for popups
//Takes in FieldName (the field to render on), label (alias of the field); rest is self-explanatory
private static readonly PopupField[] _lotsFields =
{
    new PopupField()
    {
        FieldName = "building footprint",
        Label = "Building Footprint",
        IsVisible = true,
        IsEditable = false,
        StringFieldOption = PopupStringFieldOption.SingleLine
    },
    new PopupField()
    {
        FieldName = "land use",
        Label = "Land Use",
        IsVisible = true,
        IsEditable = false,
        StringFieldOption = PopupStringFieldOption.SingleLine
    },
    new PopupField()
    {
        FieldName = "lot size (acres)",
        Label = "Lot Size (acres)",
        IsVisible = true,
        IsEditable = false,
        StringFieldOption = PopupStringFieldOption.SingleLine
    },
    new PopupField()
    {
        FieldName = "land value/acre ($)",
        Label = "Land Value/acre ($)",
        IsVisible = true,
        IsEditable = false,
        StringFieldOption = PopupStringFieldOption.SingleLine
    },
};

//Popup title definition field
//Wrap title field in curly braces to indicate it's a field-derived title
private const string _lotsPopupTitle = "{address}";

//Method to create popupdefinition for the layer
private PopupDefinition ConfigurePopupDefinition(PopupField[] popupFields,    string popupTitleField)
{
    PopupDefinition popupDef = new PopupDefinition();
    popupDef.Title = popupTitleField;
    
    //add fields to popup elements to create a popup table
    popupDef.Elements.Add(new FieldsPopupElement(popupFields));
    return popupDef;
}

//GeoViewTapped event handler
//Will embed popup into callout
private async void MapView_GeoViewTapped(object sender, GeoViewInputEventArgs e)
{
    try
    {
        //Pixel-relative click position
        Point clickPosition = e.Position;

        //Lat-long relative click location
        MapPoint clickLocation = e.Location;

        //Get layer where click was at
        FeatureLayer currentLayer = MyMapView.Map.OperationalLayers[0] as FeatureLayer;

        //Clear selected features
        currentLayer.ClearSelection();

        Popup? popup = await GetPopupAsync(clickPosition, currentLayer);

        if (popup != null)
        {
            //Feature that was clicked
            Feature feature = popup.GeoElement as Feature;

            await ShowPopupAsync(popup, clickLocation, feature);
            currentLayer.SelectFeature(feature);
            return;
        }
        //Hide callout if no popup exists
        MyMapView.DismissCallout();
    }
    catch (Exception ex)
    {
        MessageBox.Show($"Loading popup definition for feature    failed!\n\n{ex.ToString()}");
    }
}

private async Task<Popup?> GetPopupAsync(Point clickLocation, FeatureLayer currentLayer)
{
    //Get the clicked feature
    IdentifyLayerResult identifiedFeature = await MyMapView.IdentifyLayerAsync(currentLayer, clickLocation, 1, true);

    return identifiedFeature?.Popups.FirstOrDefault() ?? null;
}

//show popup via callout
private async Task ShowPopupAsync(Popup popup, MapPoint clickLocation, Feature feature)
{
    await MyMapView.SetViewpointCenterAsync(clickLocation);

    _featurePopupViewer.Popup = popup;
    MyMapView.ShowCalloutAt(clickLocation, _featurePopupViewer);
}

 

0 Kudos