地图探索

地图视图

测试视图是否为 3D 视图

1
2
3
4
5
6
7
8
9
10
11
public bool IsView3D()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Return whether the viewing mode is SceneLocal or SceneGlobal
return mapView.ViewingMode == ArcGIS.Core.CIM.MapViewingMode.SceneLocal ||
mapView.ViewingMode == ArcGIS.Core.CIM.MapViewingMode.SceneGlobal;
}

设置查看模式

1
2
3
4
5
6
7
8
9
10
11
public void SetViewingModeToSceneLocal()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Check if the view can be set to SceneLocal and if it can set it.
if (mapView.CanSetViewingMode(ArcGIS.Core.CIM.MapViewingMode.SceneLocal))
mapView.SetViewingModeAsync(ArcGIS.Core.CIM.MapViewingMode.SceneLocal);
}

启用视图链接

1
2
3
4
5
6
7
8
9
10
public void EnableViewLinking()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Set the view linking mode to Center and Scale.
MapView.LinkMode = LinkMode.Center | LinkMode.Scale;
}

更新地图视图范围(缩放、平移等)

转到上一个相机

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task<bool> ZoomToPreviousCameraAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom to the selected layers in the TOC
if (mapView.HasPreviousCamera())
return mapView.PreviousCameraAsync();

return Task.FromResult(false);
}

转到下一个相机

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task<bool> ZoomToNextCameraAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom to the selected layers in the TOC
if (mapView.HasNextCamera())
return mapView.NextCameraAsync();

return Task.FromResult(false);
}

缩放至全图范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Task<bool> ZoomToFullExtent()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Zoom to the map's full extent
return mapView.ZoomToFullExtent();
});
}

public Task<bool> ZoomToFullExtentAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom to the map's full extent
return mapView.ZoomToFullExtentAsync(TimeSpan.FromSeconds(2));
}

固定放大

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Task<bool> ZoomInFixed()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Zoom in the map view by a fixed amount.
return mapView.ZoomInFixed();
});
}

public Task<bool> ZoomInFixedAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom in the map view by a fixed amount.
return mapView.ZoomInFixedAsync();
}

固定缩小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Task<bool> ZoomOutFixed()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Zoom out in the map view by a fixed amount.
return mapView.ZoomOutFixed();
});
}

public Task<bool> ZoomOutFixedAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom in the map view by a fixed amount.
return mapView.ZoomOutFixedAsync();
}

缩放至某个范围

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task<bool> ZoomToExtent(double xMin, double yMin, double xMax, double yMax, ArcGIS.Core.Geometry.SpatialReference spatialReference)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Create the envelope
var envelope = ArcGIS.Core.Geometry.EnvelopeBuilderEx.CreateEnvelope(xMin, yMin, xMax, yMax, spatialReference);

//Zoom the view to a given extent.
return mapView.ZoomToAsync(envelope, TimeSpan.FromSeconds(2));
}

缩放至点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
public Task<bool> ZoomToPoint(double x, double y, ArcGIS.Core.Geometry.SpatialReference spatialReference)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

return QueuedTask.Run(() =>
{
//Note: Run within QueuedTask
//Create a point
var pt = MapPointBuilderEx.CreateMapPoint(x, y, spatialReference);
//Buffer it - for purpose of zoom
var poly = GeometryEngine.Instance.Buffer(pt, buffer_size);

//do we need to project the buffer polygon?
if (!MapView.Active.Map.SpatialReference.IsEqual(poly.SpatialReference))
{
//project the polygon
poly = GeometryEngine.Instance.Project(poly, MapView.Active.Map.SpatialReference);
}

//Zoom - add in a delay for animation effect
return mapView.ZoomTo(poly, new TimeSpan(0, 0, 0, 3));
});
}

缩放至所选要素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Task<bool> ZoomToSelected()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Zoom to the map's selected features.
return mapView.ZoomToSelected();
});
}

public Task<bool> ZoomToSelectedAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom to the map's selected features.
return mapView.ZoomToSelectedAsync(TimeSpan.FromSeconds(2));
}

按名称缩放至书签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public Task<bool> ZoomToBookmark(string bookmarkName)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Get the first bookmark with the given name.
var bookmark = mapView.Map.GetBookmarks().FirstOrDefault(b => b.Name == bookmarkName);
if (bookmark == null)
return false;

//Zoom the view to the bookmark.
return mapView.ZoomTo(bookmark);
});
}

public async Task<bool> ZoomToBookmarkAsync(string bookmarkName)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Get the first bookmark with the given name.
var bookmark = await QueuedTask.Run(() => mapView.Map.GetBookmarks().FirstOrDefault(b => b.Name == bookmarkName));
if (bookmark == null)
return false;

//Zoom the view to the bookmark.
return await mapView.ZoomToAsync(bookmark, TimeSpan.FromSeconds(2));
}

缩放至可见图层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Task<bool> ZoomToAllVisibleLayersAsync()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Zoom to all visible layers in the map.
var visibleLayers = mapView.Map.Layers.Where(l => l.IsVisible);
return mapView.ZoomTo(visibleLayers);
});
}

缩放至所选图层

1
2
3
4
5
6
7
8
9
10
11
public Task<bool> ZoomToTOCSelectedLayersAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Zoom to the selected layers in the TOC
var selectedLayers = mapView.GetSelectedLayers();
return mapView.ZoomToAsync(selectedLayers);
}

在一定程度上平移

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public Task<bool> PanToExtent(double xMin, double yMin, double xMax, double yMax, ArcGIS.Core.Geometry.SpatialReference spatialReference)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Pan the view to a given extent.
var envelope = ArcGIS.Core.Geometry.EnvelopeBuilderEx.CreateEnvelope(xMin, yMin, xMax, yMax, spatialReference);
return mapView.PanTo(envelope);
});
}

public Task<bool> PanToExtentAsync(double xMin, double yMin, double xMax, double yMax, ArcGIS.Core.Geometry.SpatialReference spatialReference)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Create the envelope
var envelope = ArcGIS.Core.Geometry.EnvelopeBuilderEx.CreateEnvelope(xMin, yMin, xMax, yMax, spatialReference);

//Pan the view to a given extent.
return mapView.PanToAsync(envelope, TimeSpan.FromSeconds(2));
}

平移到所选要素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public Task<bool> PanToSelected()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Pan to the map's selected features.
return mapView.PanToSelected();
});
}

public Task<bool> PanToSelectedAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Pan to the map's selected features.
return mapView.PanToSelectedAsync(TimeSpan.FromSeconds(2));
}

平移到书签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public Task<bool> PanToBookmark(string bookmarkName)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Get the first bookmark with the given name.
var bookmark = mapView.Map.GetBookmarks().FirstOrDefault(b => b.Name == bookmarkName);
if (bookmark == null)
return false;

//Pan the view to the bookmark.
return mapView.PanTo(bookmark);
});
}

public async Task<bool> PanToBookmarkAsync(string bookmarkName)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Get the first bookmark with the given name.
var bookmark = await QueuedTask.Run(() => mapView.Map.GetBookmarks().FirstOrDefault(b => b.Name == bookmarkName));
if (bookmark == null)
return false;

//Pan the view to the bookmark.
return await mapView.PanToAsync(bookmark, TimeSpan.FromSeconds(2));
}

平移到可见图层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public Task<bool> PanToAllVisibleLayersAsync()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Pan to all visible layers in the map.
var visibleLayers = mapView.Map.Layers.Where(l => l.IsVisible);
return mapView.PanTo(visibleLayers);
});
}

异步平移到所选图层

1
2
3
4
5
6
7
8
9
10
11
public Task<bool> PanToTOCSelectedLayersAsync()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Pan to the selected layers in the TOC
var selectedLayers = mapView.GetSelectedLayers();
return mapView.PanToAsync(selectedLayers);
}

旋转地图视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public Task<bool> RotateView(double heading)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return Task.FromResult(false);

//Get the camera for the view, adjust the heading and zoom to the new camera position.
var camera = mapView.Camera;
camera.Heading = heading;
return mapView.ZoomToAsync(camera, TimeSpan.Zero);
}

// or use the synchronous method
public Task<bool> RotateViewAsync(double heading)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Get the camera for the view, adjust the heading and zoom to the new camera position.
var camera = mapView.Camera;
camera.Heading = heading;
return mapView.ZoomTo(camera, TimeSpan.Zero);
});
}

展开范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public Task<bool> ExpandExtentAsync(double dx, double dy)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return false;

//Expand the current extent by the given ratio.
var extent = mapView.Extent;
var newExtent = ArcGIS.Core.Geometry.GeometryEngine.Instance.Expand(extent, dx, dy, true);
return mapView.ZoomTo(newExtent);
});
}

地图

获取活动地图的名称

1
2
3
4
5
6
7
8
9
10
public string GetActiveMapName()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return null;

//Return the name of the map currently displayed in the active map view.
return mapView.Map.Name;
}

清除活动地图中的所有选择

1
2
3
4
5
6
7
QueuedTask.Run(() =>
{
if (MapView.Active.Map != null)
{
MapView.Active.Map.SetSelection(null);
}
});

以地图单位计算选择容差

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Selection tolerance for the map in pixels
var selectionTolerance = SelectionEnvironment.SelectionTolerance;
QueuedTask.Run(() =>
{
//Get the map center
var mapExtent = MapView.Active.Map.GetDefaultExtent();
var mapPoint = mapExtent.Center;
//Map center as screen point
var screenPoint = MapView.Active.MapToScreen(mapPoint);
//Add selection tolerance pixels to get a "radius".
var radiusScreenPoint = new System.Windows.Point((screenPoint.X + selectionTolerance), screenPoint.Y);
var radiusMapPoint = MapView.Active.ScreenToMap(radiusScreenPoint);
//Calculate the selection tolerance distance in map uints.
var searchRadius = GeometryEngine.Instance.Distance(mapPoint, radiusMapPoint);
});

地图视图叠加控制

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Creat a Progress Bar user control
var progressBarControl = new System.Windows.Controls.ProgressBar();
//Configure the progress bar
progressBarControl.Minimum = 0;
progressBarControl.Maximum = 100;
progressBarControl.IsIndeterminate = true;
progressBarControl.Width = 300;
progressBarControl.Value = 10;
progressBarControl.Height = 25;
progressBarControl.Visibility = System.Windows.Visibility.Visible;
//Create a MapViewOverlayControl.
var mapViewOverlayControl = new MapViewOverlayControl(progressBarControl, true, true, true, OverlayControlRelativePosition.BottomCenter, .5, .8);
//Add to the active map
MapView.Active.AddOverlayControl(mapViewOverlayControl);
await QueuedTask.Run(() =>
{
//Wait 3 seconds to remove the progress bar from the map.
Thread.Sleep(3000);

});
//Remove from active map
MapView.Active.RemoveOverlayControl(mapViewOverlayControl);

选择目录中的所有要素图层

1
2
3
4
5
6
7
8
9
10
11
public void SelectAllFeatureLayersInTOC()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Zoom to the selected layers in the TOC
var featureLayers = mapView.Map.Layers.OfType<FeatureLayer>();
mapView.SelectLayers(featureLayers.ToList());
}

闪烁所选功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public Task FlashSelectedFeaturesAsync()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Get the selected features from the map and filter out the standalone table selection.

//At 2.x
//var selectedFeatures = mapView.Map.GetSelection()
// .Where(kvp => kvp.Key is BasicFeatureLayer)
// .ToDictionary(kvp => (BasicFeatureLayer)kvp.Key, kvp => kvp.Value);

////Flash the collection of features.
//mapView.FlashFeature(selectedFeatures);

var selectedFeatures = mapView.Map.GetSelection();

//Flash the collection of features.
mapView.FlashFeature(selectedFeatures);
});
}

检查图层在给定的地图视图中是否可见

1
2
3
4
5
6
7
8
var mapView = MapView.Active;
var layer = mapView.Map.GetLayersAsFlattenedList().OfType<Layer>().FirstOrDefault();
if (mapView == null) return;
bool isLayerVisibleInView = layer.IsVisibleInView(mapView);
if (isLayerVisibleInView)
{
//Do Something
}

选择一个图层并打开其图层属性页面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// get the layer you want
var layer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();

// select it in the TOC
List<Layer> layersToSelect = new List<Layer>();
layersToSelect.Add(layer);
MapView.Active.SelectLayers(layersToSelect);

// now execute the layer properties command
var wrapper = FrameworkApplication.GetPlugInWrapper("esri_mapping_selectedLayerPropertiesButton");
var command = wrapper as ICommand;
if (command == null)
return;

// execute the command
if (command.CanExecute(null))
command.Execute(null);

清除特定图层的选择

1
2
3
4
5
var lyr = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().FirstOrDefault();
QueuedTask.Run(() =>
{
lyr.ClearSelection();
});

地图成员的显示表窗格

1
2
3
4
5
6
7
8
9
10
11
var mapMember = MapView.Active.Map.GetLayersAsFlattenedList().OfType<MapMember>().FirstOrDefault();
//Gets or creates the CIMMapTableView for a MapMember.
var tableView = FrameworkApplication.Panes.GetMapTableView(mapMember);
//Configure the table view
tableView.DisplaySubtypeDomainDescriptions = false;
tableView.SelectionMode = false;
tableView.ShowOnlyContingentValueFields = true;
tableView.HighlightInvalidContingentValueFields = true;
//Open the table pane using the configured tableView. If a table pane is already open it will be activated.
//You must be on the UI thread to call this function.
var tablePane = FrameworkApplication.Panes.OpenTablePane(tableView);

特征

遮罩功能

1
2
3
4
5
6
7
8
9
//Get the layer to be masked
var lineLyrToBeMasked = MapView.Active.Map.Layers.FirstOrDefault(lyr => lyr.Name == "TestLine") as FeatureLayer;
//Get the layer's definition
var lyrDefn = lineLyrToBeMasked.GetDefinition();
//Create an array of Masking layers (polygon only)
//Set the LayerMasks property of the Masked layer
lyrDefn.LayerMasks = new string[] { "CIMPATH=map3/testpoly.xml" };
//Re-set the Masked layer's defintion
lineLyrToBeMasked.SetDefinition(lyrDefn);

弹出窗口

显示要素的弹出窗口

1
2
3
4
5
6
7
8
9
public void ShowPopup(MapMember mapMember, long objectID)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

mapView.ShowPopup(mapMember, objectID);
}

显示自定义弹出窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void ShowCustomPopup()
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Create custom popup content
var popups = new List<PopupContent>
{
new PopupContent("<b>This text is bold.</b>", "Custom tooltip from HTML string"),
new PopupContent(new Uri("http://www.esri.com/"), "Custom tooltip from Uri")
};
mapView.ShowCustomPopup(popups);
}

使用弹出窗口属性显示要素的弹出窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void ShowPopupWithWindowDef(MapMember mapMember, long objectID)
{
if (MapView.Active == null) return;
// Sample code: https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/master/Map-Exploration/CustomIdentify/CustomIdentify.cs
var topLeftCornerPoint = new System.Windows.Point(200, 200);
var popupDef = new PopupDefinition()
{
Append = true, // if true new record is appended to existing (if any)
Dockable = true, // if true popup is dockable - if false Append is not applicable
Position = topLeftCornerPoint, // Position of top left corner of the popup (in pixels)
Size = new System.Windows.Size(200, 400) // size of the popup (in pixels)
};
MapView.Active.ShowPopup(mapMember, objectID, popupDef);
}

使用弹出窗口属性显示自定义弹出窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void ShowCustomPopupWithWindowDef()
{
if (MapView.Active == null) return;

//Create custom popup content
var popups = new List<PopupContent>
{
new PopupContent("<b>This text is bold.</b>", "Custom tooltip from HTML string"),
new PopupContent(new Uri("http://www.esri.com/"), "Custom tooltip from Uri")
};
// Sample code: https://github.com/Esri/arcgis-pro-sdk-community-samples/blob/master/Framework/DynamicMenu/DynamicFeatureSelectionMenu.cs
var topLeftCornerPoint = new System.Windows.Point(200, 200);
var popupDef = new PopupDefinition()
{
Append = true, // if true new record is appended to existing (if any)
Dockable = true, // if true popup is dockable - if false Append is not applicable
Position = topLeftCornerPoint, // Position of top left corner of the popup (in pixels)
Size = new System.Windows.Size(200, 400) // size of the popup (in pixels)
};
MapView.Active.ShowCustomPopup(popups, null, true, popupDef);
}

显示带有自定义命令的弹出窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public void ShowCustomPopup(MapMember mapMember, long objectID)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Create custom popup content from existing map member and object id
var popups = new List<PopupContent>();
popups.Add(new PopupContent(mapMember, objectID));

//Create a new custom command to add to the popup window
var commands = new List<PopupCommand>();
commands.Add(new PopupCommand(
p => MessageBox.Show(string.Format("Map Member: {0}, ID: {1}", p.MapMember, p.IDString)),
p => { return p != null; },
"My custom command",
new BitmapImage(new Uri("pack://application:,,,/ArcGIS.Desktop.Resources;component/Images/GenericCheckMark16.png")) as ImageSource));

mapView.ShowCustomPopup(popups, commands, true);
}

显示动态弹出窗口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public void ShowDynamicPopup(MapMember mapMember, List<long> objectIDs)
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Create popup whose content is created the first time the item is requested.
var popups = new List<PopupContent>();
foreach (var id in objectIDs)
{
popups.Add(new DynamicPopupContent(mapMember, id));
}

mapView.ShowCustomPopup(popups);
}

internal class DynamicPopupContent : PopupContent
{
public DynamicPopupContent(MapMember mapMember, long objectID)
{
MapMember = mapMember;
IDString = objectID.ToString();
IsDynamicContent = true;
}

//Called when the pop-up is loaded in the window.
protected override Task<string> OnCreateHtmlContent()
{
return QueuedTask.Run(() => string.Format("<b>Map Member: {0}, ID: {1}</b>", MapMember, IDString));
}
}

书签

使用活动地图视图创建新书签

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task<Bookmark> AddBookmarkAsync(string name)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return null;

//Adding a new bookmark using the active view.
return mapView.Map.AddBookmark(mapView, name);
});
}

从联昌国际添加新书签

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public Task<Bookmark> AddBookmarkFromCameraAsync(Camera camera, string name)
{
return QueuedTask.Run(() =>
{
//Set properties for Camera
CIMViewCamera cimCamera = new CIMViewCamera()
{
X = camera.X,
Y = camera.Y,
Z = camera.Z,
Scale = camera.Scale,
Pitch = camera.Pitch,
Heading = camera.Heading,
Roll = camera.Roll
};

//Create new CIM bookmark and populate its properties
var cimBookmark = new CIMBookmark() { Camera = cimCamera, Name = name, ThumbnailImagePath = "" };

//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return null;

//Add a new bookmark for the active map.
return mapView.Map.AddBookmark(cimBookmark);
});
}

获取项目的书签集合

1
2
3
4
5
public Task<ReadOnlyObservableCollection<Bookmark>> GetProjectBookmarksAsync()
{
//Get the collection of bookmarks for the project.
return QueuedTask.Run(() => Project.Current.GetBookmarks());
}

获取地图书签

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task<ReadOnlyObservableCollection<Bookmark>> GetActiveMapBookmarksAsync()
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return null;

//Return the collection of bookmarks for the map.
return mapView.Map.GetBookmarks();
});
}

将书签移到顶部

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task MoveBookmarkToTopAsync(Map map, string name)
{
return QueuedTask.Run(() =>
{
//Find the first bookmark with the name
var bookmark = map.GetBookmarks().FirstOrDefault(b => b.Name == name);
if (bookmark == null)
return;

//Remove the bookmark
map.MoveBookmark(bookmark, 0);
});
}

重命名书签

1
2
3
4
public Task RenameBookmarkAsync(Bookmark bookmark, string newName)
{
return QueuedTask.Run(() => bookmark.Rename(newName));
}

删除具有给定名称的书签

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task RemoveBookmarkAsync(Map map, string name)
{
return QueuedTask.Run(() =>
{
//Find the first bookmark with the name
var bookmark = map.GetBookmarks().FirstOrDefault(b => b.Name == name);
if (bookmark == null)
return;

//Remove the bookmark
map.RemoveBookmark(bookmark);
});
}

更改书签的缩略图

1
2
3
4
5
6
public Task SetThumbnailAsync(Bookmark bookmark, string imagePath)
{
//Set the thumbnail to an image on disk, ie. C:\Pictures\MyPicture.png.
BitmapImage image = new BitmapImage(new Uri(imagePath, UriKind.RelativeOrAbsolute));
return QueuedTask.Run(() => bookmark.SetThumbnail(image));
}

更新书签

1
2
3
4
5
6
7
8
9
10
11
12
13
public Task UpdateBookmarkAsync(Bookmark bookmark)
{
return QueuedTask.Run(() =>
{
//Get the active map view.
var mapView = MapView.Active;
if (mapView == null)
return;

//Update the bookmark using the active map view.
bookmark.Update(mapView);
});
}

更新书签的范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public Task UpdateBookmarkExtentAsync(Bookmark bookmark, ArcGIS.Core.Geometry.Envelope envelope)
{
return QueuedTask.Run(() =>
{
//Get the bookmark's definition
var bookmarkDef = bookmark.GetDefinition();

//Modify the bookmark's location
bookmarkDef.Location = envelope;

//Clear the camera as it is no longer valid.
bookmarkDef.Camera = null;

//Set the bookmark definition
bookmark.SetDefinition(bookmarkDef);
});
}

时间

时间提前 1 个月

1
2
3
4
5
6
7
8
9
10
11
public void StepMapTime()
{
//Get the active view
MapView mapView = MapView.Active;
if (mapView == null)
return;

//Step current map time forward by 1 month
TimeDelta timeDelta = new TimeDelta(1, TimeUnit.Months);
mapView.Time = mapView.Time.Offset(timeDelta);
}

禁用地图中的时间。

1
2
MapView.Active.Time.Start = null;
MapView.Active.Time.End = null;

动画

设置动画长度

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void SetAnimationLength(TimeSpan length)
{
var mapView = MapView.Active;
if (mapView != null)
return;

var animation = mapView.Map.Animation;
var duration = animation.Duration;
if (duration == TimeSpan.Zero)
return;

var factor = length.TotalSeconds / duration.TotalSeconds;
animation.ScaleDuration(factor);
}

缩放动画

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void ScaleAnimationAfterTime(TimeSpan afterTime, TimeSpan length)
{
var mapView = MapView.Active;
if (mapView != null)
return;

var animation = mapView.Map.Animation;
var duration = animation.Duration;
if (duration == TimeSpan.Zero || duration <= afterTime)
return;

var factor = length.TotalSeconds / (duration.TotalSeconds - afterTime.TotalSeconds);
animation.ScaleDuration(afterTime, duration, factor);
}

相机关键帧

1
2
3
4
5
6
7
8
9
10
public List<CameraKeyframe> GetCameraKeyframes()
{
var mapView = MapView.Active;
if (mapView != null)
return null;

var animation = mapView.Map.Animation;
var cameraTrack = animation.Tracks.OfType<CameraTrack>().First(); //There will always be only 1 CameraTrack in the animation.
return cameraTrack.Keyframes.OfType<CameraKeyframe>().ToList();
}

插值相机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public Task<List<Camera>> GetInterpolatedCameras()
{
//Return the collection representing the camera for each frame in animation.
return QueuedTask.Run(() =>
{
var mapView = MapView.Active;
if (mapView != null || mapView.Animation == null)
return null;

var animation = mapView.Map.Animation;

var cameras = new List<Camera>();
//We will use ticks here rather than milliseconds to get the highest precision possible.
var ticksPerFrame = Convert.ToInt64(animation.Duration.Ticks / (animation.NumberOfFrames - 1));
for (int i = 0; i < animation.NumberOfFrames; i++)
{
var time = TimeSpan.FromTicks(i * ticksPerFrame);
//Because of rounding for ticks the last calculated time may be greating than the duration.
if (time > animation.Duration)
time = animation.Duration;
cameras.Add(mapView.Animation.GetCameraAtTime(time));
}
return cameras;
});
}

插值时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public Task<List<TimeRange>> GetInterpolatedMapTimes()
{
//Return the collection representing the map time for each frame in animation.
return QueuedTask.Run(() =>
{
var mapView = MapView.Active;
if (mapView != null || mapView.Animation == null)
return null;

var animation = mapView.Map.Animation;

var timeRanges = new List<TimeRange>();
//We will use ticks here rather than milliseconds to get the highest precision possible.
var ticksPerFrame = Convert.ToInt64(animation.Duration.Ticks / (animation.NumberOfFrames - 1));
for (int i = 0; i < animation.NumberOfFrames; i++)
{
var time = TimeSpan.FromTicks(i * ticksPerFrame);
//Because of rounding for ticks the last calculated time may be greating than the duration.
if (time > animation.Duration)
time = animation.Duration;
timeRanges.Add(mapView.Animation.GetCurrentTimeAtTime(time));
}
return timeRanges;
});
}

插值范围

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public Task<List<ArcGIS.Desktop.Mapping.Range>> GetInterpolatedMapRanges()
{
//Return the collection representing the map time for each frame in animation.
return QueuedTask.Run(() =>
{
var mapView = MapView.Active;
if (mapView != null || mapView.Animation == null)
return null;

var animation = mapView.Map.Animation;

var ranges = new List<ArcGIS.Desktop.Mapping.Range>();
//We will use ticks here rather than milliseconds to get the highest precision possible.
var ticksPerFrame = Convert.ToInt64(animation.Duration.Ticks / (animation.NumberOfFrames - 1));
for (int i = 0; i < animation.NumberOfFrames; i++)
{
var time = TimeSpan.FromTicks(i * ticksPerFrame);
//Because of rounding for ticks the last calculated time may be greeting than the duration.
if (time > animation.Duration)
time = animation.Duration;
ranges.Add(mapView.Animation.GetCurrentRangeAtTime(time));
}
return ranges;
});
}

创建摄像机关键帧

1
2
3
4
5
6
7
8
9
10
public void CreateCameraKeyframe(TimeSpan atTime)
{
var mapView = MapView.Active;
if (mapView != null)
return;

var animation = mapView.Map.Animation;
var cameraTrack = animation.Tracks.OfType<CameraTrack>().First(); //There will always be only 1 CameraTrack in the animation.
cameraTrack.CreateKeyframe(mapView.Camera, atTime, ArcGIS.Core.CIM.AnimationTransition.FixedArc);
}

创建时间关键帧

1
2
3
4
5
6
7
8
9
10
public void CreateTimeKeyframe(TimeSpan atTime)
{
var mapView = MapView.Active;
if (mapView != null)
return;

var animation = mapView.Map.Animation;
var timeTrack = animation.Tracks.OfType<TimeTrack>().First(); //There will always be only 1 TimeTrack in the animation.
timeTrack.CreateKeyframe(mapView.Time, atTime, ArcGIS.Core.CIM.AnimationTransition.Linear);
}

创建范围关键帧

1
2
3
4
5
6
7
8
9
10
public void CreateRangeKeyframe(ArcGIS.Desktop.Mapping.Range range, TimeSpan atTime)
{
var mapView = MapView.Active;
if (mapView != null)
return;

var animation = mapView.Map.Animation;
var rangeTrack = animation.Tracks.OfType<RangeTrack>().First(); //There will always be only 1 RangeTrack in the animation.
rangeTrack.CreateKeyframe(range, atTime, ArcGIS.Core.CIM.AnimationTransition.Linear);
}

创建图层关键帧

1
2
3
4
5
6
7
8
9
10
public void CreateLayerKeyframe(Layer layer, double transparency, TimeSpan atTime)
{
var mapView = MapView.Active;
if (mapView != null)
return;

var animation = mapView.Map.Animation;
var layerTrack = animation.Tracks.OfType<LayerTrack>().First(); //There will always be only 1 LayerTrack in the animation.
layerTrack.CreateKeyframe(layer, atTime, true, transparency, ArcGIS.Core.CIM.AnimationTransition.Linear);
}

图形叠加

图形叠加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//Defined elsewhere
private IDisposable _graphic = null;
public async void GraphicOverlaySnippetTest()
{
// get the current mapview and point
var mapView = MapView.Active;
if (mapView == null)
return;
var myextent = mapView.Extent;
var point = myextent.Center;

// add point graphic to the overlay at the center of the mapView
_graphic = await QueuedTask.Run(() =>
{
//add these to the overlay
return mapView.AddOverlay(point,
SymbolFactory.Instance.ConstructPointSymbol(
ColorFactory.Instance.RedRGB, 30.0, SimpleMarkerStyle.Star).MakeSymbolReference());
});

// update the overlay with new point graphic symbol
MessageBox.Show("Now to update the overlay...");
await QueuedTask.Run(() =>
{
mapView.UpdateOverlay(_graphic, point, SymbolFactory.Instance.ConstructPointSymbol(
ColorFactory.Instance.BlueRGB, 20.0, SimpleMarkerStyle.Circle).MakeSymbolReference());
});

// clear the overlay display by disposing of the graphic
MessageBox.Show("Now to clear the overlay...");
_graphic.Dispose();

}

图形叠加与 CIMPicture图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// get the current mapview
var mapView = MapView.Active;
if (mapView == null)
return;

//Valid formats for PictureURL are:
// e.g. local file URL:
// file:///<path>
// file:///c:/images/symbol.png
//
// e.g. network file URL:
// file://<host>/<path>
// file://server/share/symbol.png
//
// e.g. data URL:
// data:<mediatype>;base64,<data>
// data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAU ...
//
// image/bmp
// image/gif
// image/jpeg
// image/png
// image/tiff
// image/x-esri-bglf

var pictureGraphic = new CIMPictureGraphic
{
PictureURL = @"file:///C:/Images/MyImage.png",
Box = envelope
};

IDisposable _graphic = mapView.AddOverlay(pictureGraphic);

添加带有文本的叠加图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
internal class AddOverlayWithText : MapTool
{
private IDisposable _graphic = null;
private CIMLineSymbol _lineSymbol = null;
public AddOverlayWithText()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Line;
SketchOutputMode = SketchOutputMode.Map;
}

protected override async Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
//Add an overlay graphic to the map view
_graphic = await this.AddOverlayAsync(geometry, _lineSymbol.MakeSymbolReference());

//define the text symbol
var textSymbol = new CIMTextSymbol();
//define the text graphic
var textGraphic = new CIMTextGraphic();

await QueuedTask.Run(() =>
{
//Create a simple text symbol
textSymbol = SymbolFactory.Instance.ConstructTextSymbol(ColorFactory.Instance.BlackRGB, 8.5, "Corbel", "Regular");
//Sets the geometry of the text graphic
textGraphic.Shape = geometry;
//Sets the text string to use in the text graphic
textGraphic.Text = "This is my line";
//Sets symbol to use to draw the text graphic
textGraphic.Symbol = textSymbol.MakeSymbolReference();
//Draw the overlay text graphic
_graphic = this.ActiveMapView.AddOverlay(textGraphic);
});

return true;
}
}

工具

更改草图工具的符号

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
internal class SketchTool_WithSymbol : MapTool
{
public SketchTool_WithSymbol()
{
IsSketchTool = true;
SketchOutputMode = SketchOutputMode.Map; //Changing the Sketch Symbol is only supported with map sketches.
SketchType = SketchGeometryType.Rectangle;
}

protected override Task OnToolActivateAsync(bool hasMapViewChanged)
{
return QueuedTask.Run(() =>
{
//Set the Sketch Symbol if it hasn't already been set.
if (SketchSymbol != null)
return;
var polygonSymbol = SymbolFactory.Instance.ConstructPolygonSymbol(ColorFactory.Instance.CreateRGBColor(24, 69, 59),
SimpleFillStyle.Solid,
SymbolFactory.Instance.ConstructStroke(ColorFactory.Instance.BlackRGB, 1.0, SimpleLineStyle.Dash));
SketchSymbol = polygonSymbol.MakeSymbolReference();
});
}
}

创建用于地图中单击的点的返回坐标的工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
internal class GetMapCoordinates : MapTool
{
protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
e.Handled = true; //Handle the event args to get the call to the corresponding async method
}

protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
return QueuedTask.Run(() =>
{
//Convert the clicked point in client coordinates to the corresponding map coordinates.
var mapPoint = MapView.Active.ClientToMap(e.ClientPoint);
ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(string.Format("X: {0} Y: {1} Z: {2}",
mapPoint.X, mapPoint.Y, mapPoint.Z), "Map Coordinates");
});
}
}

创建用于识别与草图几何相交的特征的工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
internal class CustomIdentify : MapTool
{
public CustomIdentify()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Rectangle;

//To perform a interactive selection or identify in 3D or 2D, sketch must be created in screen coordinates.
SketchOutputMode = SketchOutputMode.Screen;
}

protected override Task<bool> OnSketchCompleteAsync(Geometry geometry)
{
return QueuedTask.Run(() =>
{
var mapView = MapView.Active;
if (mapView == null)
return true;

//Get all the features that intersect the sketch geometry and flash them in the view.
var results = mapView.GetFeatures(geometry);
mapView.FlashFeature(results);

var debug = String.Join("\n", results.ToDictionary()
.Select(kvp => String.Format("{0}: {1}", kvp.Key.Name, kvp.Value.Count())));
System.Diagnostics.Debug.WriteLine(debug);
return true;
});
}
}

更改工具的光标

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
internal class CustomMapTool : MapTool
{
public CustomMapTool()
{
IsSketchTool = true;
SketchType = SketchGeometryType.Rectangle;
SketchOutputMode = SketchOutputMode.Map;
//A custom cursor file as an embedded resource
var cursorEmbeddedResource = new Cursor(new MemoryStream(Resource1.red_cursor));
//A built in system cursor
var systemCursor = System.Windows.Input.Cursors.ArrowCD;
//Set the "CustomMapTool's" Cursor property to either one of the cursors defined above
Cursor = cursorEmbeddedResource;
//or
Cursor = systemCursor;
}

具有可嵌入控件的工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Using the Visual Studio SDK templates, add a MapTool and an EmbeddableControl
// The EmbeddableControl is registered in the "esri_embeddableControls" category in the config.daml file
//
// <categories>
// <updateCategory refID = "esri_embeddableControls" >
// <insertComponent id="mapTool_EmbeddableControl" className="EmbeddableControl1ViewModel">
// <content className = "EmbeddableControl1View" />
// </insertComponent>
// <updateCategory>
// </categories>
internal class MapTool_WithControl : MapTool
{
public MapTool_WithControl()
{
// substitute this string with the daml ID of the embeddable control you added
ControlID = "mapTool_EmbeddableControl";
}

protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
e.Handled = true;
}

protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
//Get the instance of the ViewModel
var vm = EmbeddableControl;
if (vm == null)
return Task.FromResult(0);

// cast vm to your viewModel in order to access your properties

//Get the map coordinates from the click point and set the property on the ViewMode.
return QueuedTask.Run(() =>
{
var mapPoint = MapView.Active.ClientToMap(e.ClientPoint);
string clickText = string.Format("X: {0}, Y: {1}, Z: {2}", mapPoint.X, mapPoint.Y, mapPoint.Z);
});
}
}

具有可嵌入叠加控件的工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Using the Visual Studio SDK templates, add a MapTool and an EmbeddableControl
// The EmbeddableControl is registered in the "esri_embeddableControls" category in the config.daml file
//
// <categories>
// <updateCategory refID = "esri_embeddableControls" >
// <insertComponent id="mapTool_EmbeddableControl" className="EmbeddableControl1ViewModel">
// <content className = "EmbeddableControl1View" />
// </insertComponent>
// <updateCategory>
// </categories>

internal class MapTool_WithOverlayControl : MapTool
{
public MapTool_WithOverlayControl()
{
// substitute this string with the daml ID of the embeddable control you added
OverlayControlID = "mapTool_EmbeddableControl";
}

protected override void OnToolMouseDown(MapViewMouseButtonEventArgs e)
{
e.Handled = true;
}

protected override Task HandleMouseDownAsync(MapViewMouseButtonEventArgs e)
{
//Get the instance of the ViewModel
var vm = OverlayEmbeddableControl;
if (vm == null)
return Task.FromResult(0);

// cast vm to your viewModel in order to access your properties

//Get the map coordinates from the click point and set the property on the ViewMode.
return QueuedTask.Run(() =>
{
var mapPoint = MapView.Active.ClientToMap(e.ClientPoint);
string clickText = string.Format("X: {0}, Y: {1}, Z: {2}", mapPoint.X, mapPoint.Y, mapPoint.Z);
});
}
}

}