Click or drag to resize

PortalItemCreateAsync Method (ArcGISPortal, String)

Initializes a new instance of the PortalItem class from the specified item ID.

Namespace:  Esri.ArcGISRuntime.Portal
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.8.0.0
Syntax
C#
public static Task<PortalItem> CreateAsync(
	ArcGISPortal portal,
	string itemId
)

Parameters

portal
Type: Esri.ArcGISRuntime.PortalArcGISPortal
The ArcGIS portal.
itemId
Type: SystemString
The item Id.

Return Value

Type: TaskPortalItem
The task object representing the asynchronous create operation. The value of the task result is a PortalItem object.
Remarks
The portal itemId can be determined from the URL of the item details web page or the map viewer URL (for maps only) in your portal. The item details page has the following format (http://www.arcgis.com/home/item.html?id=[item_id]). The webmap viewer has the following format (http://www.arcgis.com/home/webmap/viewer.html?webmap=[item_id]). In both cases, the [item_id] can be used as the ID to instantiate a portal item.
Examples

Android

Example Name: DisplayKml

Display KML from a URL, portal item, or local KML file.

Code example screen shot.

C#
// Copyright 2018 Esri.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
// language governing permissions and limitations under the License.

using System;
using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using ArcGISRuntime.Samples.Managers;
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.UI.Controls;

namespace ArcGISRuntimeXamarin.Samples.DisplayKml
{
    [Activity (ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Display KML",
        "Layers",
        "Display a KML file from URL, a local file, or a portal item.",
        "")]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("324e4742820e46cfbe5029ff2c32cb1f")]
    public class DisplayKml : Activity
    {
        // Hold references to UI controls.
        private SceneView _mySceneView;
        private Button _dataChoiceButton;
        private readonly Envelope _usEnvelope = new Envelope(-144.619561355187, 18.0328662832097, -66.0903762761083, 67.6390975806745, SpatialReferences.Wgs84);
        private readonly string[] _sources = {"URL", "Local file", "Portal item"};

        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            Title = "Display KML";

            CreateLayout();
            Initialize();
        }

        private void Initialize()
        {
            // Set up the basemap.
            _mySceneView.Scene = new Scene(Basemap.CreateImageryWithLabels());
        }

        private void CreateLayout()
        {
            // Create a new vertical layout for the app.
            LinearLayout layout = new LinearLayout(this) {Orientation = Orientation.Vertical};

            // Create button to show data choices
            _dataChoiceButton = new Button(this)
            {
                Text = "Choose a KML data source"
            };
            _dataChoiceButton.Click += DataChoiceButtonOnClick;

            // Add button
            layout.AddView(_dataChoiceButton);

            // Add the scene view to the layout
            _mySceneView = new SceneView(this);
            layout.AddView(_mySceneView);

            // Show the layout in the app
            SetContentView(layout);
        }

        private void DataChoiceButtonOnClick(object sender, EventArgs e)
        {
            // Create menu to show the rendering rule options
            PopupMenu sourceMenu = new PopupMenu(this, _dataChoiceButton);
            sourceMenu.MenuItemClick += DataChoiceButton_Clicked;

            // Create menu options
            foreach (string option in _sources)
            {
                sourceMenu.Menu.Add(option);
            }

            // Show menu in the view
            sourceMenu.Show();
        }

        private async void DataChoiceButton_Clicked(object sender, PopupMenu.MenuItemClickEventArgs e)
        {
            // Clear existing layers.
            _mySceneView.Scene.OperationalLayers.Clear();

            // Get the name of the selected layer.
            string name = e.Item.TitleCondensedFormatted.ToString();

            try
            {
                // Create the layer using the chosen constructor.
                KmlLayer layer;
                switch (name)
                {
                    case "URL":
                    default:
                        layer = new KmlLayer(new Uri("https://www.wpc.ncep.noaa.gov/kml/noaa_chart/WPC_Day1_SigWx_latest.kml"));
                        break;
                    case "Local file":
                        string filePath = DataManager.GetDataFolder("324e4742820e46cfbe5029ff2c32cb1f", "US_State_Capitals.kml");
                        layer = new KmlLayer(new Uri(filePath));
                        break;
                    case "Portal item":
                        ArcGISPortal portal = await ArcGISPortal.CreateAsync();
                        PortalItem item = await PortalItem.CreateAsync(portal, "9fe0b1bfdcd64c83bd77ea0452c76253");
                        layer = new KmlLayer(item);
                        break;
                }

                // Add the selected layer to the map.
                _mySceneView.Scene.OperationalLayers.Add(layer);

                // Zoom to the extent of the United States.
                await _mySceneView.SetViewpointAsync(new Viewpoint(_usEnvelope));
            }
            catch (Exception ex)
            {
                new AlertDialog.Builder(this).SetMessage(ex.ToString()).SetTitle("Error").Show();
            }
        }
    }
}
Examples

Xamarin Forms Android

Example Name: MapReferenceScale

Set the map's reference scale and which feature layers should honor the reference scale.

Code example screen shot.

C#
// Copyright 2019 Esri.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific 
// language governing permissions and limitations under the License.

using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using System;
using Xamarin.Forms;

namespace ArcGISRuntimeXamarin.Samples.MapReferenceScale
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Map reference scale",
        "Map",
        "Set a map's reference scale and control which feature layers honor that scale.",
        "")]
    public partial class MapReferenceScale : ContentPage
    {
        // List of reference scale options.
        private readonly double[] _referenceScales =
        {
            50000,
            100000,
            250000,
            500000
        };

        public MapReferenceScale()
        {
            InitializeComponent();
            Initialize();
        }

        private async void Initialize()
        {
            // Create a portal and an item; the map will be loaded from portal item.
            ArcGISPortal portal = await ArcGISPortal.CreateAsync(new Uri("https://runtime.maps.arcgis.com"));
            PortalItem mapItem = await PortalItem.CreateAsync(portal, "3953413f3bd34e53a42bf70f2937a408");

            // Create the map from the item.
            Map webMap = new Map(mapItem);

            // Display the map.
            MyMapView.Map = webMap;

            // Populate the reference scale choices.
            ReferenceScaleBox.ItemsSource = _referenceScales;
            ReferenceScaleBox.SelectedIndex = 0;

            // NOTE: this sample uses binding, so explicit control of reference scale isn't seen here.
            // See the iOS or Android samples for an implementation that does not rely on data binding.
        }
    }
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
    x:Class="ArcGISRuntimeXamarin.Samples.MapReferenceScale.MapReferenceScale"
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
    xmlns:resources="clr-namespace:Forms.Resources;assembly=ArcGISRuntime">
    <RelativeLayout>
        <esriUI:MapView
            x:Name="MyMapView"
            BindingContext="{x:Reference Name=ResponsiveFormContainer}"
            Style="{StaticResource MapWithFormStyle}" />
        <resources:ResponsiveFormContainer x:Name="ResponsiveFormContainer">
            <StackLayout>
                <Label Text="Choose a map reference scale:" />
                <!--  When the user's selection changes, the SelectedItem binding will apply the value to the Map's ReferenceScale property.  -->
                <Picker
                    x:Name="ReferenceScaleBox"
                    Margin="0,5,0,5"
                    BindingContext="{x:Reference MyMapView}"
                    ItemDisplayBinding="{Binding StringFormat='{0:n0}'}"
                    SelectedItem="{Binding Path=Map.ReferenceScale}"
                    TextColor="Black" />
                <Label Text="Choose layers to apply scale to:" />
                <!--  Binding is used to display the operational layers for the map view's map, no code behind needed.  -->
                <ListView
                    Margin="0,5,0,5"
                    BindingContext="{x:Reference MyMapView}"
                    HeightRequest="125"
                    ItemsSource="{Binding Path=Map.OperationalLayers}"
                    SelectionMode="None">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <!--
                                    When the user interacts with the switch,
                                    the two-way binding will update the ScaleSymbols (bool) property automatically.
                                -->
                                <StackLayout Orientation="Horizontal">
                                    <Switch IsToggled="{Binding ScaleSymbols}" VerticalOptions="Center" />
                                    <Label
                                        HorizontalOptions="EndAndExpand"
                                        HorizontalTextAlignment="End"
                                        Text="{Binding Name}"
                                        VerticalOptions="Center" />
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
                <Label BindingContext="{x:Reference MyMapView}" Text="{Binding Path=MapScale, Mode=OneWay, StringFormat='Current map scale: {0:n0}'}" />
            </StackLayout>
        </resources:ResponsiveFormContainer>
    </RelativeLayout>
</ContentPage>
See Also
Additional Examples
Hyperlink to ExampleDescription
DisplayKmlDisplay KML from a URL, portal item, or local KML file.
DownloadPreplannedMapTake a map offline using a preplanned map area.
FeatureCollectionLayerFromPortalCreate a feature collection layer from a portal item.
GenerateOfflineMapTake a web map offline.
GenerateOfflineMapWithOverridesTake a web map offline with additional options for each layer.
MapReferenceScaleSet the map's reference scale and which feature layers should honor the reference scale.
OAuthThis sample demonstrates how to authenticate with ArcGIS Online (or your own portal) using OAuth2 to access secured resources (such as private web maps or layers). Accessing secured items requires a login on the portal that hosts them (an ArcGIS Online account, for example).
OfflineBasemapByReferenceUse the `OfflineMapTask` to take a web map offline, but instead of downloading an online basemap, use one which is already on the device.
OpenSceneOpen a web scene from a portal item.
ViewContentBeneathSurfaceSee through terrain in a scene and move the camera underground.