Click or drag to resize

ArcGISPortalCreateAsync Method (Uri)

Creates asynchronously a new instance of the ArcGISPortal class from the specified portal Uri. If the URI is null, ArcGIS Online is used by default (https://www.arcgis.com).

Namespace:  Esri.ArcGISRuntime.Portal
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.7.0.0
Syntax
C#
public static Task<ArcGISPortal> CreateAsync(
	Uri portalUri
)

Parameters

portalUri
Type: SystemUri
The portal URI.

Return Value

Type: TaskArcGISPortal
The task object representing the asynchronous create operation. The value of the task result is a ArcGISPortal object.
Examples

Android

Example Name: MapReferenceScale

Set a map's reference scale and control which feature layers should honor that 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 Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Portal;
using Esri.ArcGISRuntime.UI.Controls;
using System;
using System.Linq;

namespace ArcGISRuntimeXamarin.Samples.MapReferenceScale
{
    [Activity (ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Map reference scale",
        "Map",
        "Set a map's reference scale and control which feature layers honor that scale.",
        "")]
    public class MapReferenceScale : Activity
    {
        // Hold references to the UI controls.
        private MapView _myMapView;
        private TextView _currentScaleLabel;
        private TextView _referenceScaleSelectionLabel;
        private Button _layersButton;

        // List of reference scale options.
        private readonly double[] _referenceScales =
        {
            50000,
            100000,
            250000,
            500000
        };

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

            Title = "Map reference scale";

            CreateLayout();
            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);

            // Update the UI when the map navigates.
            _myMapView.ViewpointChanged += (o, e) => _currentScaleLabel.Text = $"Current map scale: 1:{_myMapView.MapScale:n0}";

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

            // Wait for the map to load.
            await webMap.LoadAsync();

            // Enable the button now that the map is ready.
            _layersButton.Enabled = true;
        }

        private void ChooseScale_Clicked(object sender, EventArgs e)
        {
            try
            {
                Button scaleButton = (Button) sender;

                // Create menu for showing layer controls.
                PopupMenu sublayersMenu = new PopupMenu(this, scaleButton);
                sublayersMenu.MenuItemClick += ScaleSelection_Changed;

                // Create menu options.
                int index = 0;
                foreach (double scale in _referenceScales)
                {
                    // Add the menu item.
                    sublayersMenu.Menu.Add(0, index, index, $"1:{scale:n0}");
                    index++;
                }

                // Show menu in the view
                sublayersMenu.Show();
            }
            catch (Exception ex)
            {
                new AlertDialog.Builder(this).SetMessage(ex.ToString()).SetTitle("Error").Show();
            }
        }

        private void ScaleSelection_Changed(object sender, PopupMenu.MenuItemClickEventArgs e)
        {
            // Find the index of the selected scale.
            int selectionIndex = e.Item.Order;

            // Apply the selected scale.
            _myMapView.Map.ReferenceScale = _referenceScales[selectionIndex];

            // Update the UI.
            _referenceScaleSelectionLabel.Text = $"1:{_referenceScales[selectionIndex]:n0}";
        }

        private void ChooseLayers_Clicked(object sender, EventArgs e)
        {
            try
            {
                Button layersButton = (Button) sender;

                // Create menu for showing layer controls.
                PopupMenu sublayersMenu = new PopupMenu(this, layersButton);
                sublayersMenu.MenuItemClick += LayerScaleSelection_Changed;

                // Create menu options.
                int index = 0;
                foreach (FeatureLayer layer in _myMapView.Map.OperationalLayers.OfType<FeatureLayer>())
                {
                    // Add the menu item.
                    sublayersMenu.Menu.Add(0, index, index, layer.Name);

                    // Configure the menu item.
                    sublayersMenu.Menu.GetItem(index).SetCheckable(true).SetChecked(layer.ScaleSymbols);

                    index++;
                }

                // Show menu in the view
                sublayersMenu.Show();
            }
            catch (Exception ex)
            {
                new AlertDialog.Builder(this).SetMessage(ex.ToString()).SetTitle("Error").Show();
            }
        }

        private void LayerScaleSelection_Changed(object sender, PopupMenu.MenuItemClickEventArgs e)
        {
            // Get the checked item.
            int selectedLayerIndex = e.Item.Order;

            // Find the layer.
            FeatureLayer selectedLayer = _myMapView.Map.OperationalLayers.OfType<FeatureLayer>().ElementAt(selectedLayerIndex);

            // Set the symbol scale mode.
            selectedLayer.ScaleSymbols = !selectedLayer.ScaleSymbols;

            // Update the UI.
            e.Item.SetChecked(selectedLayer.ScaleSymbols);
        }

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

            // Button for choosing the map's reference scale.
            Button chooseScaleButton = new Button(this);
            chooseScaleButton.Text = "Choose map reference scale";
            chooseScaleButton.Click += ChooseScale_Clicked;
            layout.AddView(chooseScaleButton);

            // Label for showing current reference scale.
            _referenceScaleSelectionLabel = new TextView(this);
            _referenceScaleSelectionLabel.Text = "Choose a reference scale";
            layout.AddView(_referenceScaleSelectionLabel);

            // Button for selecting which layers will have symbol scaling enabled.
            _layersButton = new Button(this);
            _layersButton.Text = "Manage scaling for layers";
            _layersButton.Click += ChooseLayers_Clicked;
            layout.AddView(_layersButton);

            // Add a horizontal line separator.
            View separatorView = new View(this);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, 1);
            separatorView.LayoutParameters = lp;
            layout.AddView(separatorView);

            // Add a label that shows the current map scale.
            _currentScaleLabel = new TextView(this);
            _currentScaleLabel.Text = "Current map scale: ";
            layout.AddView(_currentScaleLabel);

            // Add the map view to the layout.
            _myMapView = new MapView(this);
            layout.AddView(_myMapView);

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

Xamarin Forms Android

Example Name: MapReferenceScale

Set a map's reference scale and control which feature layers should honor that 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
AuthorMapCreate and save a map as an ArcGIS `PortalItem` (i.e. web map).
IntegratedWindowsAuthUse Windows credentials to access services hosted on a portal secured with Integrated Windows Authentication (IWA).
MapReferenceScaleSet a map's reference scale and control which feature layers should honor that 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).
SearchPortalMapsFind webmap portal items by using a search term.