public void ZoomMapToExtent(Envelope env) { if (env == null || Map == null) return; if (!mapInitComplete) throw new InvalidOperationException(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionCannotCallZoomMapToExtentBeforeMapHasInitialized); if (Map.SpatialReference == null) throw new InvalidOperationException(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionCannotCallZoomMapToExtentBeforeMapHasInitializedMapReferenceNull); if (env.SpatialReference == null) throw new ArgumentException(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionMustSpecifySpatialReferenceOnEnvelope); if (Map.SpatialReference.Equals(env.SpatialReference)) { Map.Extent = env; } else { GeometryServiceOperationHelper geomHelper = new GeometryServiceOperationHelper( new ConfigurationStoreHelper().GetGeometryServiceUrl(ConfigurationStore)); geomHelper.GeometryServiceOperationFailed += (o, args) => { Logger.Instance.LogError(args.Exception); throw args.Exception; }; geomHelper.ProjectExtentCompleted += (o, args) => { Map.Extent = args.Extent; }; geomHelper.ProjectExtent(env, Map.SpatialReference); } }
private void CreateNewBaseMapWithSpatialReference(Envelope targetInitialExtent, Envelope targetFullExtent, BaseMapInfo baseMapInfo, SpatialReference targetServiceSpatialReference) { // Reproject oldMap's extent before comparison GeometryServiceOperationHelper geomHelper = new GeometryServiceOperationHelper( new ConfigurationStoreHelper().GetGeometryServiceUrl(ConfigurationStore), baseMapInfo.UseProxy ? baseMapInfo.ProxyUrl : null); geomHelper.GeometryServiceOperationFailed += (o, args) => { Logger.Instance.LogError(args.Exception); MessageBoxDialog.Show(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.MsgUnableToAccessGeometryService + Environment.NewLine + args.Exception != null ? args.Exception.Message : ESRI.ArcGIS.Mapping.Controls.Resources.Strings.MsgUnknownError); }; geomHelper.ProjectExtentCompleted += (o, args) => { Envelope targetExtent = null; // If the extents (compared in same projection) interesect, set the extent of the new map to the projected extent if (targetInitialExtent != null) { if (args.Extent.Intersects(targetInitialExtent)) { if (isFullyContainedWithin(args.Extent, targetInitialExtent)) targetExtent = targetFullExtent; // if the full extent of the new service is fully within, automatically zoom to it else targetExtent = args.Extent; } else targetExtent = targetInitialExtent; } else if (targetFullExtent != null) { if (args.Extent.Intersects(targetFullExtent)) { if (isFullyContainedWithin(args.Extent, targetFullExtent)) targetExtent = targetFullExtent; // if the full extent of the new service is fully within, automatically zoom to it else targetExtent = args.Extent; } else targetExtent = targetFullExtent; } // else don't set an extent // the map will default to the full extent of the service // Since map will not be in a different projection, we have to re-create the map BaseMapInfo targetBaseMapInfo = baseMapInfo; IBaseMapDataSource dataSoure = DataSourceProvider.CreateDataSourceForBaseMapType(targetBaseMapInfo.BaseMapType) as IBaseMapDataSource; if (dataSoure == null) throw new Exception(string.Format(ESRI.ArcGIS.Mapping.Controls.Resources.Strings.ExceptionDatasourceNotLoadedForBaseMapType, targetBaseMapInfo.BaseMapType.ToString())); TiledMapServiceLayer layer = dataSoure.CreateBaseMapLayer(targetBaseMapInfo); layer.SetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty, true); layer.SetValue(ESRI.ArcGIS.Client.Extensibility.MapApplication.LayerNameProperty, targetBaseMapInfo.DisplayName); checkAndEnsureBingMapsTokenIfRequired(layer); // Save current selected layer Layer currentSelectedLayer = SelectedLayer; saveGraphicsInViewForSelectedLayerInAttributeDisplay(); // Disable listening for layer changed events because we are re-adding layers to the collection. The initialization events are not fired nor is symbology changed Map.Layers.CollectionChanged -= Layers_CollectionChanged; List<Layer> oldBaseMapLayers = new List<Layer>(); foreach (Layer l in Map.Layers) { if ((bool)l.GetValue(ESRI.ArcGIS.Client.WebMap.Document.IsBaseMapProperty)) oldBaseMapLayers.Add(l); } switchBaseMapLayer(layer, targetExtent, oldBaseMapLayers); // Re-Enable listening for layer changed events Map.Layers.CollectionChanged += Layers_CollectionChanged; // Restore current selected layer SelectedLayer = currentSelectedLayer; restoreGraphicsInViewForSelectedLayerInAttributeDisplay(); OnNewMapCreated(new MapRecreatedEventArgs() { NewMap = Map }); OnBaseMapChangeComplete(EventArgs.Empty); }; geomHelper.ProjectExtent(Map.Extent, targetServiceSpatialReference); }
public override void Execute(object parameter) { if (Layer == null) return; bool isServerLayer = false; Envelope targetExtent = null; TiledLayer tiledLayer = Layer as TiledLayer; if (tiledLayer != null) { isServerLayer = true; targetExtent = LayerExtentExtensions.GetTiledLayerFullExtent(tiledLayer); // get the cached value (if any) which will likely be in the map's spRef already if (targetExtent == null) // value not known, use value on service metadata (will likely be in the services' spatial ref and not neccesarily in map's spRef) targetExtent = tiledLayer.FullExtent; if (tiledLayer is ESRI.ArcGIS.Client.Bing.TileLayer) { if (targetExtent.SpatialReference == null) targetExtent.SpatialReference = new SpatialReference(102100); } } DynamicLayer dynamicLayer = Layer as DynamicLayer; if (dynamicLayer != null) { isServerLayer = true; targetExtent = LayerExtentExtensions.GetDynamicLayerFullExtent(dynamicLayer); // get the cached value (if any) which will likely be in the map's spRef already if (targetExtent == null)// value not known, use value on service metadata (will likely be in the services' spatial ref and not neccesarily in map's spRef) targetExtent = dynamicLayer.FullExtent; } if (isServerLayer) { if (targetExtent == null || Map == null || Map.SpatialReference == null) return; if (Map.SpatialReference.Equals(targetExtent.SpatialReference)) { // spatial reference matches. can directly zoom Map.ZoomTo(targetExtent); } else { if (string.IsNullOrEmpty(GeometryServiceUrl)) throw new Exception(Resources.Strings.ExceptionNoGeometryServiceUrlSpecifiedLayerZoomedIsDifferentSpatialReference); GeometryServiceOperationHelper helper = new GeometryServiceOperationHelper(GeometryServiceUrl); helper.ProjectExtentCompleted += (o, e) => { // Cache (save) the projected extent values if (tiledLayer != null) LayerExtentExtensions.SetTiledLayerFullExtent(tiledLayer, e.Extent); else if (dynamicLayer != null) LayerExtentExtensions.SetDynamicLayerFullExtent(dynamicLayer, e.Extent); Map.ZoomTo(e.Extent); }; helper.ProjectExtent(targetExtent, Map.SpatialReference); } return; } // Non server rendererd layers follow GraphicsLayer graphicsLayer = Layer as GraphicsLayer; if (graphicsLayer == null) return; if (Map != null) { Envelope newMapExtent = null; if (graphicsLayer.Graphics.Count < 1) { FeatureLayer featureLayer = graphicsLayer as FeatureLayer; if (featureLayer != null && featureLayer.LayerInfo != null && featureLayer.LayerInfo.Extent != null) { Envelope env = featureLayer.LayerInfo.Extent; SpatialReference sr = env.SpatialReference; if (sr == null) sr = featureLayer.LayerInfo.DefaultSpatialReference; if (Map.SpatialReference.Equals(sr)) { Map.PanTo(env); } else if (sr != null) { GeometryServiceOperationHelper geomHelper = new GeometryServiceOperationHelper( new ConfigurationStoreHelper().GetGeometryServiceUrl(View.Instance.ConfigurationStore)); geomHelper.GeometryServiceOperationFailed += (o, args) => { Logger.Instance.LogError(args.Exception); throw args.Exception; }; geomHelper.ProjectExtentCompleted += (o, args) => { MapZoomOrPan(args.Extent); }; geomHelper.ProjectExtent(env, Map.SpatialReference); } } } else { foreach (Graphic graphic in graphicsLayer.Graphics) { if (graphic.Geometry != null && graphic.Geometry.Extent != null) newMapExtent = graphic.Geometry.Extent.Union(newMapExtent); } newMapExtent = MapZoomOrPan(newMapExtent); } } }