Snippets

Philip O'Gorman Xamarin ArcGis Set ViewPoint using bindings. Listen for Viewpoint changed events using behaviours.

Created by Philip O'Gorman last modified
using Esri.ArcGISRuntime.Geometry;
using Esri.ArcGISRuntime.Mapping;
using Esri.ArcGISRuntime.Xamarin.Forms;
using Xamarin.Forms;

namespace FormsMapsShared
{
    /// <summary>
    /// Binding helpers
    /// </summary>
    public class CommandBinder
    {
        /// <summary>
        /// This command binding allows you to set the extent on a mapView from your view-model through binding
        /// </summary>
        public static Envelope GetRequestView(BindableObject obj)
        {
            return (Envelope)obj.GetValue(RequestViewProperty);
        }

        /// <summary>
        /// This command binding allows you to set the extent on a mapView from your view-model through binding
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="extent"></param>
        public static void SetRequestView(BindableObject obj, Envelope extent)
        {
            obj.SetValue(RequestViewProperty, extent);
        }

        /// <summary>
        /// Identifies the ZoomTo Attached Property.
        /// </summary>
        public static readonly BindableProperty RequestViewProperty =
        BindableProperty.CreateAttached(
            propertyName: "RequestView",
            returnType: typeof(Viewpoint),
            declaringType: typeof(CommandBinder),
            defaultValue: null,
            defaultBindingMode: BindingMode.OneWay,
            validateValue: null,
            propertyChanged: RequestViewPropertyChanged);

        private static void RequestViewPropertyChanged(BindableObject bindable, object oldValue, object newValue)
        {
            if (bindable is MapView)
            {
                MapView mapView = bindable as MapView;
                if (newValue is Viewpoint)
                {
                    mapView.SetViewpoint((Viewpoint)newValue);
                }
            }
        }
    }
}
<?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:local="clr-namespace:JcaFormsMapsShared;assembly=SmartMap.Droid"
             xmlns:esriUI="clr-namespace:Esri.ArcGISRuntime.Xamarin.Forms;assembly=Esri.ArcGISRuntime.Xamarin.Forms"
             xmlns:mapping="clr-namespace:Esri.ArcGISRuntime.Mapping;assembly=Esri.ArcGISRuntime"
             xmlns:controls="clr-namespace:FormsMapsShared.Controls;assembly=SmartMap.Droid"
             xmlns:b="clr-namespace:Xamarin.Behaviors;assembly=Xamarin.Behaviors"
             x:Class="FormsMapsShared.Pages.ArcGisPage">
  

  <StackLayout Orientation="Horizontal">

    <esriUI:MapView x:Name="MyMapView" VerticalOptions="FillAndExpand"
                         HorizontalOptions="FillAndExpand"
                    Map="{Binding RendererManager.Map}"
                    GraphicsOverlays="{Binding RendererManager.GraphicsOverlays}"
                    local:CommandBinder.RequestView="{Binding RendererManager.ViewpointRequested}">
      <b:Interaction.Behaviors>
        <b:BehaviorCollection>
          <b:EventToCommand EventName="ViewpointChanged"  Command="{Binding ViewpointChanged}"
                          CommandParameter="{Binding Source={x:Reference MyMapView}, Path=VisibleArea.Extent}"  />
        </b:BehaviorCollection>
      </b:Interaction.Behaviors>
    </esriUI:MapView>
                    
  </StackLayout>

</ContentPage>
        private Command _viewpointChanged;
        public Command ViewpointChanged
        {
            get
            {
                if (_viewpointChanged == null)
                {
                    _viewpointChanged = new Command<Envelope>((o) =>
                    {
                        if (o != null)
                        {
                            OffLineMapHelper.MapViewEnvelope = o;
                        }
                    });
                }
                return _viewpointChanged;
            }
        }

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.