前言
本次开发第一个arcgis大项目,在过程中记录的一点点点滴滴,为以后记录,希望大家指正。
一、移动鼠标实现实时显示坐标比例尺
private void MyMapView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
//获取屏幕鼠标位置
System.Windows.Point screenPoint = e.GetPosition(MyMapView);
//从屏幕上鼠标位置获取实际坐标位置
MapPoint mapPoint = MyMapView.ScreenToLocation(screenPoint);
//或者坐标信息,转换mapPoint
Geometry myGeometry = GeometryEngine.Project(mapPoint, SpatialReferences.WebMercator);
MapPoint projectedLocation = (MapPoint)myGeometry;
//在lable中显示
lb.Content = string.Format("Lat: {0:F3} Long:{1:F3} 坐标:{2} 比例尺:{3}", projectedLocation.Y, projectedLocation.X, myGeometry.SpatialReference, Math.Round(MyMapView.MapScale,0));
}
二、按国家2000-35坐标系显示坐标
private void MyMapView_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
System.Windows.Point screenPoint = e.GetPosition(MyMapView);
MapPoint mapPoint = MyMapView.ScreenToLocation(screenPoint);
//2000坐标
SpatialReference spr = new SpatialReference(4523);
Geometry myGeometry = GeometryEngine.Project(mapPoint, spr);
MapPoint projectedLocation = (MapPoint)myGeometry;
lb.Content = string.Format("Lat: {0:F3} Long:{1:F3} 坐标:{2} 比例尺:{3}", projectedLocation.Y, projectedLocation.X, myGeometry.SpatialReference, Math.Round(MyMapView.MapScale, 0));
}
三、实现测量功能
前端代码
<Window.Resources>
<esri:SimpleLineSymbol x:Key="CompleteLineSymbol" Width="4" />
<esri:SimpleFillSymbol x:Key="CompletePolygonSymbol" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Border Background="#ccc">
<StackPanel Orientation="Horizontal">
<Button Height="30" Width="100" Click="MeasureLine">测距</Button>
<Button Width="100" Click="MeasureAreaLength">测面积</Button>
<Button Width="100" x:Name="DrawButton" Click="DrawButtonClick">编辑</Button>
<Button Width="100" x:Name="ClearButton" Click="ClearButtonClick">清除</Button>
<Button Width="100" x:Name="CompleteButton" Command="{Binding CompleteCommand}">完成</Button>
</StackPanel>
</Border>
<esri:MapView x:Name="MyMapView" Grid.Row="1" MouseRightButtonDown="MyMapView_MouseRightButtonDown"/>
</Grid>
后端代码
public MainWindow()
{
InitializeComponent();
Initialize();
}
private void Initialize()
{
// Create a light gray canvas map
Map myMap = new Map(Basemap.CreateLightGrayCanvas());
// Create graphics overlay to display sketch geometry
_sketchOverlay = new GraphicsOverlay();
MyMapView.GraphicsOverlays.Add(_sketchOverlay);
// Assign the map to the MapView
MyMapView.Map = myMap;
// Fill the combo box with choices for the sketch modes (shapes)
}
#region Graphic and symbol helpers
private Graphic CreateGraphic(Esri.ArcGISRuntime.Geometry.Geometry geometry)
{
// Create a graphic to display the specified geometry
Symbol symbol = null;
switch (geometry.GeometryType)
{
// Symbolize with a fill symbol
//case GeometryType.Envelope:
case GeometryType.Polygon:
{
symbol = new SimpleFillSymbol()
{
Color = Color.Red,
Style = SimpleFillSymbolStyle.Solid
};
break;
}
// Symbolize with a line symbol
case GeometryType.Polyline:
{
symbol = new SimpleLineSymbol()
{
Color = Color.Red,
Style = SimpleLineSymbolStyle.Solid,
Width = 5d
};
break;
}
}
// pass back a new graphic with the appropriate symbol
return new Graphic(geometry, symbol);
}
private async void MeasureAreaLength(object sender, RoutedEventArgs e)
{
try
{
// Let the user draw on the map view using the chosen sketch mode
//SketchCreationMode creationMode = (SketchCreationMode)SketchModeComboBox.SelectedItem;
if (skd == null)
{
skd = MyMapView.SketchEditor;
}
//Esri.ArcGISRuntime.Geometry.Geometry geometry = await MyMapView.SketchEditor.StartAsync(SketchCreationMode.Polygon, true
Esri.ArcGISRuntime.Geometry.Geometry geometry = await skd.StartAsync(SketchCreationMode.Polygon, true);
double are = Esri.ArcGISRuntime.Geometry.GeometryEngine.Area(geometry);
MapPoint centerPoint = new MapPoint((geometry.Extent.XMax + geometry.Extent.XMin) / 2, (geometry.Extent.YMax + geometry.Extent.YMin) / 2, geometry.SpatialReference);
TextSymbol bassRockTextSymbol = new TextSymbol(are.ToString(), Color.Blue, 10,
Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Left, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Bottom);
Graphic bassRockGraphic = new Graphic(centerPoint, bassRockTextSymbol);
//var parts = py.Parts[0].Points;
//parts.Points
// Esri.ArcGISRuntime.Geometry.PointCollection point = (PointCollection)(py.ToPolyline());
// Create and add a graphic from the geometry the user drew
Graphic graphic = CreateGraphic(geometry);
// graphic.
_sketchOverlay.Graphics.Add(graphic);
_sketchOverlay.Graphics.Add(bassRockGraphic);
// Enable/disable the clear and edit buttons according to whether or not graphics exist in the overlay
ClearButton.IsEnabled = _sketchOverlay.Graphics.Count > 0;
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel drawing
}
catch (Exception ex)
{
//Report exceptions
MessageBox.Show("Error drawing graphic shape: " + ex.Message);
}
}
private async void MeasureLine(object sender, RoutedEventArgs e)
{
try
{
// Let the user draw on the map view using the chosen sketch mode
//SketchCreationMode creationMode = (SketchCreationMode)SketchModeComboBox.SelectedItem;
if (skd == null)
{
skd = MyMapView.SketchEditor;
}
//Esri.ArcGISRuntime.Geometry.Geometry geometry = await MyMapView.SketchEditor.StartAsync(SketchCreationMode.Polygon, true
Esri.ArcGISRuntime.Geometry.Geometry geometry = await skd.StartAsync(SketchCreationMode.Polyline, true);
double dis = Esri.ArcGISRuntime.Geometry.GeometryEngine.Length(geometry);
Esri.ArcGISRuntime.Geometry.Polyline pl = geometry as Esri.ArcGISRuntime.Geometry.Polyline;
MapPoint PointAlong = Esri.ArcGISRuntime.Geometry.GeometryEngine.CreatePointAlong(pl, dis/2);
//MapPoint centerPoint = new MapPoint((geometry.Extent.XMax + geometry.Extent.XMin) / 2, (geometry.Extent.YMax + geometry.Extent.YMin) / 2, geometry.SpatialReference);
TextSymbol bassRockTextSymbol = new TextSymbol(dis.ToString(), Color.Blue, 10,
Esri.ArcGISRuntime.Symbology.HorizontalAlignment.Left, Esri.ArcGISRuntime.Symbology.VerticalAlignment.Bottom);
Graphic bassRockGraphic = new Graphic(PointAlong, bassRockTextSymbol);
//var parts = py.Parts[0].Points;
//parts.Points
// Esri.ArcGISRuntime.Geometry.PointCollection point = (PointCollection)(py.ToPolyline());
// Create and add a graphic from the geometry the user drew
Graphic graphic = CreateGraphic(geometry);
// graphic.
_sketchOverlay.Graphics.Add(graphic);
_sketchOverlay.Graphics.Add(bassRockGraphic);
// Enable/disable the clear and edit buttons according to whether or not graphics exist in the overlay
ClearButton.IsEnabled = _sketchOverlay.Graphics.Count > 0;
}
catch (TaskCanceledException)
{
// Ignore ... let the user cancel drawing
}
catch (Exception ex)
{
//Report exceptions
MessageBox.Show("Error drawing graphic shape: " + ex.Message);
}
}
private void MyMapView_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (skd == null)
{
skd = MyMapView.SketchEditor;
}
var aa = skd.CompleteCommand;
aa.Execute(null);
}
private void ClearButtonClick(object sender, RoutedEventArgs e)
{
// Remove all graphics from the graphics overlay
_sketchOverlay.Graphics.Clear();
// Disable buttons that require graphics
ClearButton.IsEnabled = false;
}