<Grid x:Name="LayoutRoot">
<esri:Map x:Name="map_BaseMap" Layers="{Binding Path=MapLayers}" helpers:MapHelper.ZoomGeometry="{Binding Path=MapExtent}" MinimumResolution=".25">
<esri:Map.Extent>
<esri:Envelope XMin="-10510990.377700" YMin="5586273.272800" XMax="-10364472.557100" YMax="5682307.505400">
<esri:Envelope.SpatialReference>
<esri:SpatialReference WKID="102100"/>
</esri:Envelope.SpatialReference>
</esri:Envelope>
</esri:Map.Extent>
<interactivity:Interaction.Behaviors>
<behaviors:MouseCoordinateBehavior/>
</interactivity:Interaction.Behaviors>
</esri:Map>
<controls:LogisNavigation Margin="5" Map="{Binding ElementName=map_BaseMap}" FullExtentGeometry="{Binding Path=FullExtent}"
InitialExtentGeometry="{Binding Path=InitialExtent}" helpers:NavigationHelper.ZoomReset="{Binding Path=MapExtent}"
Background="Gray" BorderBrush="White" Foreground="Black"/>
<esri:MapProgressBar HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,5" Width="150" Map="{Binding ElementName=map_BaseMap}"/>
<controls:LogisScaleBar HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5" Width="200" MapUnit="Meters"
Map="{Binding ElementName=map_BaseMap}"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" Foreground="White"/>
</Grid>
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Projection;
namespace MVVM_Debug.Behaviors
{
public class MouseCoordinateBehavior : Behavior<Map>
{
#region Constructors
public MouseCoordinateBehavior()
: base()
{
}
#endregion
#region Properties
public string MouseCoordinates { get; private set; }
#endregion
#region Methods
protected override void OnAttached()
{
base.OnAttached();
this.AssociatedObject.MouseMove += new MouseEventHandler(AssociatedObject_MouseMove);
}
protected override void OnDetaching()
{
base.OnDetaching();
this.AssociatedObject.MouseMove -= new MouseEventHandler(AssociatedObject_MouseMove);
}
void AssociatedObject_MouseMove(object sender, MouseEventArgs e)
{
Map map = (Map)sender;
Point mouseLoc = e.GetPosition(map);
MapPoint coordLoc = map.ScreenToMap(mouseLoc);
if (coordLoc != null)
{
WebMercator webMercator = new WebMercator();
MapPoint latLon = webMercator.ToGeographic(coordLoc) as MapPoint;
MouseCoordinates = string.Format("Lon {0:F4}° Lat {1:F4}°", latLon.X, latLon.Y);
}
}
#endregion
}
}
public class MouseCoordinatesAction :TargetedTriggerAction<TextBlock>
{
/// <summary>
/// Invokes the action.
/// Search for the user provided address
/// </summary>
/// <param name="parameter">The parameter to the action. If the Action does not require a parameter, the parameter may be set to a null reference.</param>
protected override void Invoke(object parameter)
{
if ((Target != null) && (parameter != null))
Target.Text = GetCoordinateString(parameter as MouseEventArgs);
}
private string GetCoordinateString(MouseEventArgs e)
{
string mouseCoordString = string.Empty;
Map map = AssociatedObject as Map;
Point mouseLoc = e.GetPosition(map);
MapPoint coordLoc = map.ScreenToMap(mouseLoc);
if (coordLoc != null)
{
WebMercator webMercator = new WebMercator();
MapPoint latLon = webMercator.ToGeographic(coordLoc) as MapPoint;
mouseCoordString = string.Format("Lon {0:F4}° Lat {1:F4}°", latLon.X, latLon.Y);
}
return mouseCoordString;
}
}<Grid x:Name="LayoutRoot">
<esri:Map x:Name="map_BaseMap" Layers="{Binding Path=MapLayers}" helpers:MapHelper.ZoomGeometry="{Binding Path=MapExtent}" MinimumResolution=".25">
<esri:Map.Extent>
<esri:Envelope XMin="-10510990.377700" YMin="5586273.272800" XMax="-10364472.557100" YMax="5682307.505400">
<esri:Envelope.SpatialReference>
<esri:SpatialReference WKID="102100"/>
</esri:Envelope.SpatialReference>
</esri:Envelope>
</esri:Map.Extent>
<interactivity:Interaction.Triggers>
<interaction:EventTrigger EventName="MouseMove">
<actions:MouseCoordinatesAction TargetName="mouseCoordinates" />
</i:EventTrigger>
</esri:Map>
<controls:LogisNavigation Margin="5" Map="{Binding ElementName=map_BaseMap}" FullExtentGeometry="{Binding Path=FullExtent}"
InitialExtentGeometry="{Binding Path=InitialExtent}" helpers:NavigationHelper.ZoomReset="{Binding Path=MapExtent}"
Background="Gray" BorderBrush="White" Foreground="Black"/>
<esri:MapProgressBar HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,5" Width="150" Map="{Binding ElementName=map_BaseMap}"/>
<controls:LogisScaleBar HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5" Width="200" MapUnit="Meters"
Map="{Binding ElementName=map_BaseMap}"/>
<TextBlock x:Name="mouseCoordinates" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" Foreground="White"/>
</Grid>using System.Windows;
using System.Windows.Input;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Projection;
using Microsoft.Expression.Interactivity.Core;
public class CoordinateChangePropertyAction : ChangePropertyAction
{
protected override void Invoke(object parameter)
{
this.Value = GetCoordinateString(parameter as MouseEventArgs);
base.Invoke(parameter);
}
private string GetCoordinateString(MouseEventArgs e)
{
string mouseCoordString = string.Empty;
Map map = AssociatedObject as Map;
Point mouseLoc = e.GetPosition(map);
MapPoint coordLoc = map.ScreenToMap(mouseLoc);
if (coordLoc != null)
{
WebMercator webMercator = new WebMercator();
MapPoint latLon = webMercator.ToGeographic(coordLoc) as MapPoint;
mouseCoordString = string.Format("Lon {0:F4}° Lat {1:F4}°", latLon.X, latLon.Y);
}
return mouseCoordString;
}
}<esri:Map x:Name="map_BaseMap">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<MyActionNamespace:CoordinateChangePropertyAction TargetObject="{Binding DataContext, ElementName=LayoutRoot}" PropertyName="SomePropertyOnTheViewModelToBindTo"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</esri:Map><UserControl x:Class="MVVM_Debug.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:interactivity="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:interactions="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:esri="http://schemas.esri.com/arcgis/client/2009"
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"
xmlns:actions="clr-namespace:MVVM_Debug.Actions"
xmlns:behaviors="clr-namespace:MVVM_Debug.Behaviors"
xmlns:controls="clr-namespace:MVVM_Debug.Controls"
xmlns:helpers="clr-namespace:MVVM_Debug.Helpers"
xmlns:toolkit="clr-namespace:MVVM_Debug.Toolkit"
mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Skins/MainSkin.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<Grid x:Name="LayoutRoot">
<esri:Map x:Name="map_BaseMap" Layers="{Binding Path=MapLayers}" helpers:MapHelper.ZoomGeometry="{Binding Path=MapExtent}" MinimumResolution=".25">
<esri:Map.Extent>
<esri:Envelope XMin="-10510990.377700" YMin="5586273.272800" XMax="-10364472.557100" YMax="5682307.505400">
<esri:Envelope.SpatialReference>
<esri:SpatialReference WKID="102100"/>
</esri:Envelope.SpatialReference>
</esri:Envelope>
</esri:Map.Extent>
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="MouseMove">
<actions:MouseCoordinateAction TargetObject="{Binding Path=DataContext, ElementName=LayoutRoot}"
PropertyName="{Binding Path=Coordinates}"/>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</esri:Map>
<controls:LogisNavigation Margin="5" Map="{Binding ElementName=map_BaseMap}" FullExtentGeometry="{Binding Path=FullExtent}"
InitialExtentGeometry="{Binding Path=InitialExtent}" helpers:NavigationHelper.ZoomReset="{Binding Path=MapExtent}"
Background="Gray" BorderBrush="White" Foreground="Black"/>
<esri:MapProgressBar HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="0,0,0,5" Width="150" Map="{Binding ElementName=map_BaseMap}"/>
<controls:LogisScaleBar HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5" Width="200" MapUnit="Meters"
Map="{Binding ElementName=map_BaseMap}"/>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Top" Margin="5" Foreground="White" Text="{Binding Path=Coordinates.X, StringFormat=\{0:F4\}}"/>
</Grid>
</UserControl>using System.Windows;
using System.Windows.Input;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Projection;
using Microsoft.Expression.Interactivity.Core;
namespace MVVM_Debug.Actions
{
public class MouseCoordinateAction : ChangePropertyAction
{
protected override void Invoke(object parameter)
{
this.Value = GetLatLon(parameter as MouseEventArgs);
base.Invoke(parameter);
}
private MapPoint GetLatLon(MouseEventArgs e)
{
Map map = AssociatedObject as Map;
Point mouseLoc = e.GetPosition(map);
MapPoint coordLoc = map.ScreenToMap(mouseLoc);
if (coordLoc != null && coordLoc.Extent.SpatialReference.WKID == 102100)
{
WebMercator webMercator = new WebMercator();
MapPoint latLon = webMercator.ToGeographic(coordLoc) as MapPoint;
return latLon;
}
else
return null;
}
}
}
public class MouseCoordinatesAction : TriggerAction<Map>
{
/// <summary>
/// Invokes the action.
/// Search for the user provided address
/// </summary>
/// <param name="parameter">The parameter to the action. If the Action does not require a parameter, the parameter may be set to a null reference.</param>
protected override void Invoke(object parameter)
{
Messenger.Default.Send(GetCoordinateString(parameter as MouseEventArgs), "FromMainViewModel");
}
private string GetCoordinateString(MouseEventArgs e)
{
//this is the body of the method, but I will remove it to make the code shorter
return mouseCoordString;
}
}
//**************************** Inside MainViewModel *************************
//A string with the current mouse point coordinates
private string _currentMapPoint;
/// <summary>
/// A string with the current mouse point coordinates
/// </summary>
public string CurrentMapPoint
{
get { return _currentMapPoint; }
set
{
if (_currentMapPoint != value)
{
_currentMapPoint = value;
OnNotifyPropertyChanged("CurrentMapPoint");
}
}
}
public MainViewModel()
{
//requires a reference to GalaSoft.MvvmLight.SL4
Messenger.Default.Register<string>(this, "FromMainViewModel", (e => this.CurrentMapPoint = e));
}
//********************** In MainView xaml - Trigger is inside the Map control***********************
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<actions:MouseCoordinatesAction />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock Text="{Binding CurrentMapPoint}"
x:Name="mouseCoordinates" Width="200" Height="22"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>public class MouseCoordinatesAction : TargetedTriggerAction<MainViewModel>
{
/// <summary>
/// Invokes the action.
/// Search for the user provided address
/// </summary>
/// <param name="parameter">The parameter to the action. If the Action does not require a parameter, the parameter may be set to a null reference.</param>
protected override void Invoke(object parameter)
{
Target.CurrentMapPoint = GetCoordinateString(parameter as MouseEventArgs);
}
private string GetCoordinateString(MouseEventArgs e)
{
//removing the body for shortness
return mouseCoordString;
}
}
//**************************** Inside MainViewModel *************************
//A string with the current mouse point coordinates
private string _currentMapPoint;
/// <summary>
/// A string with the current mouse point coordinates
/// </summary>
public string CurrentMapPoint
{
get { return _currentMapPoint; }
set
{
if (_currentMapPoint != value)
{
_currentMapPoint = value;
OnNotifyPropertyChanged("CurrentMapPoint");
}
}
}
//********************** In MainView xaml - Trigger is inside the Map control***********************
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<actions:MouseCoordinatesAction TargetObject="{Binding DataContext, ElementName=LayoutRoot}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock Text="{Binding CurrentMapPoint}"
x:Name="mouseCoordinates" Width="200" Height="22"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>public class MouseCoordinatesAction : TriggerAction<Map>
{
/// <summary>
/// Invokes the action.
/// Search for the user provided address
/// </summary>
/// <param name="parameter">The parameter to the action. If the Action does not require a parameter, the parameter may be set to a null reference.</param>
protected override void Invoke(object parameter)
{
MousePoint = GetCoordinateString(parameter as MouseEventArgs);
}
private string GetCoordinateString(MouseEventArgs e)
{
//removing code for shortness
return mouseCoordString;
}
/// <summary>
/// The symbol used to display the addresss point buffer on the map
/// </summary>
public string MousePoint
{
get { return (string)GetValue(MousePointProperty); }
set { SetValue(MousePointProperty, value); }
}
public static readonly DependencyProperty MousePointProperty =
DependencyProperty.Register("MousePoint", typeof(string), typeof(MouseCoordinatesAction), null);
}
//**************************** Inside MainViewModel *************************
//A string with the current mouse point coordinates
private string _currentMapPoint;
/// <summary>
/// A string with the current mouse point coordinates
/// </summary>
public string CurrentMapPoint
{
get { return _currentMapPoint; }
set
{
if (_currentMapPoint != value)
{
_currentMapPoint = value;
OnNotifyPropertyChanged("CurrentMapPoint");
}
}
}
//********************** In MainView xaml - Trigger is inside the Map control***********************
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<actions:MouseCoordinatesAction MousePoint="{Binding CurrentMapPoint,Mode=TwoWay" />
</i:EventTrigger>
</i:Interaction.Triggers>
<TextBlock Text="{Binding CurrentMapPoint}"
x:Name="mouseCoordinates" Width="200" Height="22"
VerticalAlignment="Bottom" HorizontalAlignment="Right"/>