Click or drag to resize

MobileMapPackageOpenAsync Method

Opens a new instance of the MobileMapPackage class.

Namespace:  Esri.ArcGISRuntime.Mapping
Assembly:  Esri.ArcGISRuntime (in Esri.ArcGISRuntime.dll) Version: 100.6.0.0
Syntax
C#
public static Task<MobileMapPackage> OpenAsync(
	string path
)

Parameters

path
Type: SystemString
A path to mobile map package.

Return Value

Type: TaskMobileMapPackage
The task object representing the asynchronous open operation. The value of the task result is a MobileMapPackage object.
Exceptions
ExceptionCondition
ExpiredExceptionPackage was authored to prevent expired access.
UnpackRequiredExceptionPackage contains data that requires unpack before opening.
Remarks
The path can refer to a file with a .mmpk extension or a directory containing an unpacked mobile map package.
Examples

Android

Example Name: OpenMobileMap

Display a map from a mobile map package.

Code example screen shot.

C#
// Copyright 2017 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 System.IO;
using Android.App;
using Android.OS;
using Android.Widget;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.UI.Controls;
using System.Linq;
using ArcGISRuntime.Samples.Managers;

namespace ArcGISRuntime.Samples.OpenMobileMap
{
    [Activity (ConfigurationChanges=Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("e1f3a7254cb845b09450f54937c16061")]
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Open mobile map (map package)",
        "Map",
        "This sample demonstrates how to open a map from a mobile map package.",
        "The map package will be downloaded from an ArcGIS Online portal automatically.")]
    public class OpenMobileMap : Activity
    {
        private MapView _myMapView;

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

            Title = "Open mobile map (map package)";

            CreateLayout();
            Initialize();
        }

        private async void Initialize()
        {
            // Get the path to the mobile map package.
            string filepath = GetMmpkPath();

            try
            {
                // Load directly or unpack then load as needed by the map package.
                if (await MobileMapPackage.IsDirectReadSupportedAsync(filepath))
                {
                    // Open the map package.
                    MobileMapPackage myMapPackage = await MobileMapPackage.OpenAsync(filepath);

                    // Display the first map in the package.
                    _myMapView.Map = myMapPackage.Maps.First();
                }
                else
                {
                    // Create a path for the unpacked package.
                    string unpackedPath = filepath + "unpacked";

                    // Unpack the package.
                    await MobileMapPackage.UnpackAsync(filepath, unpackedPath);

                    // Open the package.
                    MobileMapPackage package = await MobileMapPackage.OpenAsync(unpackedPath);

                    // Load the package.
                    await package.LoadAsync();

                    // Show the first map.
                    _myMapView.Map = package.Maps.First();
                }
            }
            catch (Exception e)
            {
                new AlertDialog.Builder(this).SetMessage(e.ToString()).SetTitle("Error").Show();
            }
        }

        /// <summary>
        /// This abstracts away platform & sample viewer-specific code for accessing local files.
        /// </summary>
        /// <returns>String that is the path to the file on disk.</returns>
        private string GetMmpkPath()
        {
            return DataManager.GetDataFolder("e1f3a7254cb845b09450f54937c16061", "Yellowstone.mmpk");
        }

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

            // Add a 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: OpenMobileMap

Display a map from a mobile map package.

Code example screen shot.

C#
// Copyright 2017 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 System.IO;
using System.Linq;
using Esri.ArcGISRuntime.Mapping;
using ArcGISRuntime.Samples.Managers;
using Xamarin.Forms;

namespace ArcGISRuntime.Samples.OpenMobileMap
{
    [ArcGISRuntime.Samples.Shared.Attributes.Sample(
        "Open mobile map (map package)",
        "Map",
        "This sample demonstrates how to open a mobile map from a map package.",
        "The map package will be downloaded from an ArcGIS Online portal automatically.")]
    [ArcGISRuntime.Samples.Shared.Attributes.OfflineData("e1f3a7254cb845b09450f54937c16061")]
    public partial class OpenMobileMap : ContentPage
    {
        public OpenMobileMap()
        {
            InitializeComponent();
            Initialize();
        }

        private async void Initialize()
        {
            // Get the path to the mobile map package.
            string filepath = GetMmpkPath();

            try
            {
                // Load directly or unpack then load as needed by the map package.
                if (await MobileMapPackage.IsDirectReadSupportedAsync(filepath))
                {
                    // Open the map package.
                    MobileMapPackage myMapPackage = await MobileMapPackage.OpenAsync(filepath);

                    // Display the first map in the package.
                    MyMapView.Map = myMapPackage.Maps.First();
                }
                else
                {
                    // Create a path for the unpacked package.
                    string unpackedPath = filepath + "unpacked";

                    // Unpack the package.
                    await MobileMapPackage.UnpackAsync(filepath, unpackedPath);

                    // Open the package.
                    MobileMapPackage package = await MobileMapPackage.OpenAsync(unpackedPath);

                    // Load the package.
                    await package.LoadAsync();

                    // Show the first map.
                    MyMapView.Map = package.Maps.First();
                }
            }
            catch (Exception e)
            {
                await Application.Current.MainPage.DisplayAlert("Error", e.ToString(), "OK");
            }
        }

        /// <summary>
        /// This abstracts away platform & sample viewer-specific code for accessing local files.
        /// </summary>
        /// <returns>String that is the path to the file on disk.</returns>
        private string GetMmpkPath()
        {
            return DataManager.GetDataFolder("e1f3a7254cb845b09450f54937c16061", "Yellowstone.mmpk");
        }
    }
}
XAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage 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:mapping="clr-namespace:Esri.ArcGISRuntime.Mapping;assembly=Esri.ArcGISRuntime" 
             x:Class="ArcGISRuntime.Samples.OpenMobileMap.OpenMobileMap">
    <esriUI:MapView x:Name="MyMapView"/>
</ContentPage>
See Also
Additional Examples
Hyperlink to ExampleDescription
DownloadPreplannedMapTake a map offline using an available preplanned map area.
MobileMapSearchAndRouteDisplay maps and use locators to enable search and routing offline using a Mobile Map Package.
OpenMobileMapDisplay a map from a mobile map package.