protected override void OnClick() { AddLayerToMap(); } private async Task AddLayerToMap() { try { // Get the first map called "Map" from the current project. Map myMap = null; myMap = await GetMapFromProject(Project.Current, "Map"); if (myMap == null) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to get map."); return; } // Create a url pointing to the source. In this case it is a url to an image service // which will result in an image service layer being created. string dataSoureUrl = @"https://services7.arcgis.com/ /arcgis/rest/services/地图/FeatureServer"; // Note: A url can also point to // 1.) An image on disk or an in a file geodatabase. e.g. string dataSoureUrl = @"C: empa.tif"; This results in a raster layer. // 2.) A mosaic dataset in a file gdb e.g. string dataSoureUrl = @"c: empmygdb.gdbMyMosaicDataset"; This results in a mosaic layer. // 3.) A raster or mosaic dataset in an enterprise geodatabase. // Create an ImageServiceLayer object to hold the new layer. Layer layer1 = null; // The layer has to be created on the Main CIM Thread (MCT). await QueuedTask.Run(() => { // Create a layer based on the url. In this case the layer we are creating is an image service layer. layer1 = (Layer)LayerFactory.Instance.CreateLayer(new Uri(dataSoureUrl), myMap); // Check if it is created. if (layer1 == null) { ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Failed to create layer for url:" + dataSoureUrl); return; } }); } catch (Exception exc) { // Catch any exception found and display a message box. ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Exception caught: " + exc.Message); return; } }
关注微信公众号,学习更多
/// <summary> /// Gets the map from a project that matches a map name. /// </summary> /// <param name="project">The project in which the map resides.</param> /// <param name="mapName">The map name to identify the map.</param> /// <returns>A Task representing the map.</returns> private Task<Map> GetMapFromProject(Project project, string mapName) { // Return null if either of the two parameters are invalid. if (project == null || string.IsNullOrEmpty(mapName)) return null; // Find the first project item with name matches with mapName MapProjectItem mapProjItem = project.GetItems<MapProjectItem>().FirstOrDefault(item => item.Name.Equals(mapName, StringComparison.CurrentCultureIgnoreCase)); if (mapProjItem != null) return QueuedTask.Run<Map>(() => { return mapProjItem.GetMap(); }, Progressor.None); else return null; }