protected override void OnAttached() { base.OnAttached(); Map.Layers.LayersInitialized += MapLayersInitialized; Map.Layers.CollectionChanged += LayersCollectionChanged; } protected override void OnDetaching() { base.OnDetaching(); Map.Layers.LayersInitialized -= MapLayersInitialized; Map.Layers.CollectionChanged -= LayersCollectionChanged; } private void MapLayersInitialized(object sender, EventArgs args) { var layers = sender as LayerCollection; if (layers == null || !layers.Any()) return; foreach (var layer in layers.Where(layer => layer is GraphicsLayer && layer.IsInitialized && layer.InitializationFailure == null)) Project((GraphicsLayer)layer); } private void LayersCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { if ((e.Action == NotifyCollectionChangedAction.Add || e.Action == NotifyCollectionChangedAction.Replace) && e.NewItems.Count > 0) { foreach (var layer in e.NewItems.Cast<Layer>().Where(layer => layer is GraphicsLayer && layer.IsInitialized && layer.InitializationFailure == null && !SpatialReference.AreEqual(Map.SpatialReference, layer.SpatialReference, false))) { Project((GraphicsLayer)layer); } } } private void Project(GraphicsLayer graphicsLayer) { if (graphicsLayer.Graphics.Any()) { Project(graphicsLayer.Graphics); } else { graphicsLayer.Graphics.CollectionChanged += (s, a) => { if (a.Action == NotifyCollectionChangedAction.Add && a.NewItems.Count > 0) { Project(a.NewItems.Cast<Graphic>().ToList()); } }; } } private void Project(IList<Graphic> graphics) { if (graphics.Any() && graphics.First().Geometry != null && SpatialReference.AreEqual(Map.SpatialReference, graphics.First().Geometry.SpatialReference, true)) return; GeometryService geometryService = new GeometryService("url for service"); geometryService.ProjectCompleted += GeometryServiceProjectCompleted; geometryService.ProjectAsync(graphics, Map.SpatialReference, graphics); } private static void GeometryServiceProjectCompleted(object sender, GraphicsEventArgs e) { var existingGraphics = e.UserState as IList<Graphic>; if (existingGraphics == null || existingGraphics.Count != e.Results.Count) throw new InvalidOperationException("The results from the project do not match the values passed in to the operation."); for (var i = 0; i < e.Results.Count; i++) { Graphic graphic = e.Results; if (graphic.Geometry.Extent != null) { Graphic graphicToDisplay = existingGraphics; graphicToDisplay.Geometry = graphic.Geometry; } } }
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.