/// <summary> /// Get the service info for the layer description asynchronously. When the call completes, the MapService /// property will be set. /// </summary> /// <param name="callback"></param> public void GetServiceInfoAsync(EventHandler <AsyncEventArgs> callback) { // test for custom layers first - TODO remove this logic when AGOL web maps can handle bing and OSM // if (LayerType == "BingMapsAerial" || LayerType == "BingMapsRoad" || LayerType == "BingMapsHybrid" || LayerType == "OpenStreetMap") { Service = new MapService() { SpatialReference = new SpatialReference(WKIDs.WebMercatorAuxiliarySphere), Units = "esriMeters" }; callback(this, new AsyncEventArgs() { Succeeded = true }); return; } ArcGISService.GetServiceInfoAsync(Url, null, (sender, e) => { Service = e.Service; callback(this, new AsyncEventArgs() { Succeeded = e.Service != null }); }); }
/// <summary> /// Gets the service information for a MapService asynchronously. /// </summary> public static void GetServiceInfoAsync(string url, object userState, EventHandler <ServiceEventArgs> callback) { WebUtil.OpenReadAsync(new Uri(url + "?f=json"), null, (sender, e) => { if (e.Error != null) { callback(null, new ServiceEventArgs()); return; } MapService mapService = WebUtil.ReadObject <MapService>(e.Result); if (mapService != null && mapService.Name != null && mapService.Units != null) { mapService.Url = url; mapService.RequiresProxy = e.UsedProxy; mapService.InitTitle(); callback(null, new ServiceEventArgs() { Service = mapService, UserState = userState }); return; } FeatureService featureService = WebUtil.ReadObject <FeatureService>(e.Result); if (featureService != null && featureService.Layers != null && featureService.Layers.Length > 0) { featureService.Url = url; featureService.RequiresProxy = e.UsedProxy; featureService.InitTitle(); callback(null, new ServiceEventArgs() { Service = featureService, UserState = userState }); return; } ImageService imageService = WebUtil.ReadObject <ImageService>(e.Result); if (imageService != null && imageService.PixelType != null) { imageService.Url = url; imageService.RequiresProxy = e.UsedProxy; imageService.InitTitle(); callback(null, new ServiceEventArgs() { Service = imageService, UserState = userState }); return; } FeatureLayerService featureLayerService = WebUtil.ReadObject <FeatureLayerService>(e.Result); if (featureLayerService != null && featureLayerService.Type == "Feature Layer") { featureLayerService.Url = url; featureLayerService.RequiresProxy = e.UsedProxy; featureLayerService.Title = featureLayerService.Name; callback(null, new ServiceEventArgs() { Service = featureLayerService, UserState = userState }); return; } callback(null, new ServiceEventArgs()); }); }
/// <summary> /// Initializes the layer by creating the appropriate ESRI.ArcGIS.Client.Layer and /// holding onto it (_internalLayer). /// </summary> public void InitializeAsync(Map map, object userState, EventHandler <LayerInitializedEventArgs> callback) { if (_internalLayer != null) { return; } ConnectionStatus = LayerConnectionStatus.NotConnected; // determine the layer type - TODO - change this when the web map format supports Bing and OSM // TileLayer.LayerType bingLayerType = TileLayer.LayerType.Aerial; switch (LayerDescription.LayerType) { case "BingMapsAerial": bingLayerType = TileLayer.LayerType.Aerial; Type = LayerType.BingLayer; break; case "BingMapsRoad": bingLayerType = TileLayer.LayerType.Road; Type = LayerType.BingLayer; break; case "BingMapsHybrid": bingLayerType = TileLayer.LayerType.AerialWithLabels; Type = LayerType.BingLayer; break; case "OpenStreetMap": Type = LayerType.OpenStreetMapLayer; break; default: if (LayerDescription.Service is ImageService) { Type = LayerType.ArcGISImageServiceLayer; } else if (LayerDescription.Service is FeatureLayerService) { Type = LayerType.ArcGISFeatureServiceLayer; } else { bool tiled = LayerDescription.Service is MapService && ((MapService)LayerDescription.Service).IsTiled && (map.SpatialReference == null || map.SpatialReference.Equals(((MapService)LayerDescription.Service).SpatialReference)); Type = tiled ? LayerType.ArcGISTiledMapServiceLayer : LayerType.ArcGISDynamicMapServiceLayer; // if the service's spatial reference is different from the map's spatial reference, always set the layer type to be // ArcGISDynamicMapServiceLayer if (map != null && LayerDescription.Service != null) { if (!SpatialReference.AreEqual(LayerDescription.Service.SpatialReference, map.SpatialReference, true)) { Type = LayerType.ArcGISDynamicMapServiceLayer; } } } break; } // now create the appropriate ESRI.ArcGIS.Client.Layer // string proxy = (LayerDescription.Service != null && LayerDescription.Service.RequiresProxy) ? ArcGISOnlineEnvironment.ConfigurationUrls.ProxyServerEncoded : null; if (Type == LayerType.ArcGISTiledMapServiceLayer) { ArcGISTiledMapServiceLayer tiledLayer = new ArcGISTiledMapServiceLayer() { Url = this.Url, ProxyURL = proxy }; _internalLayer = tiledLayer; MapService mapService = LayerDescription.Service as MapService; if (mapService != null && mapService.TileInfo != null && mapService.TileInfo.LODs != null) { double maxResolution = 0; double minResolution = 0; foreach (LODInfo lod in mapService.TileInfo.LODs) { if (lod.Resolution > maxResolution) { maxResolution = lod.Resolution; } if (minResolution <= 0 || minResolution > lod.Resolution) { minResolution = lod.Resolution; } } if (maxResolution > 0) { tiledLayer.MaximumResolution = maxResolution * 4; } if (minResolution > 0) { tiledLayer.MinimumResolution = minResolution / 4; } } } else if (Type == LayerType.ArcGISDynamicMapServiceLayer) { _internalLayer = new ArcGISDynamicMapServiceLayer() { Url = this.Url, ProxyURL = proxy } } ; else if (Type == LayerType.ArcGISImageServiceLayer) { int[] bandIds = ((ImageService)LayerDescription.Service).BandCount < 4 ? null : new int[] { 0, 1, 2 }; _internalLayer = new ArcGISImageServiceLayer() { Url = this.Url, ProxyURL = proxy, ImageFormat = ArcGISImageServiceLayer.ImageServiceImageFormat.PNG8, BandIds = bandIds }; } else if (Type == LayerType.ArcGISFeatureServiceLayer) { _internalLayer = new FeatureLayer() { Url = this.Url, ProxyUrl = proxy, Mode = LayerDescription.QueryMode, OutFields = new ESRI.ArcGIS.Client.Tasks.OutFields() { "*" }, Renderer = new ESRI.ArcGIS.Mapping.Core.Symbols.HiddenRenderer() }; } else if (Type == LayerType.OpenStreetMapLayer) { _internalLayer = new OpenStreetMapLayer(); } else if (Type == LayerType.BingLayer) { TileLayer tileLayer = new TileLayer() { LayerStyle = bingLayerType }; tileLayer.Token = ArcGISOnlineEnvironment.BingToken; tileLayer.ServerType = ServerType.Production; _internalLayer = tileLayer; } _internalLayer.Visible = LayerDescription.Visible; _internalLayer.Opacity = LayerDescription.Opacity; if (!string.IsNullOrEmpty(LayerDescription.Title)) { _internalLayer.SetValue(ESRI.ArcGIS.Client.Extensibility.MapApplication.LayerNameProperty, LayerDescription.Title); } if (LayerDescription.IsReference) { ESRI.ArcGIS.Mapping.Core.LayerExtensions.SetIsReferenceLayer(_internalLayer, true); } // remember the map's spatial reference in order to determine after initialization // if the layer will work properly // if (map != null) { _mapSpatialReference = map.SpatialReference; } _internalLayer.Initialized += _internalLayer_Initialized; _internalLayer.InitializationFailed += _internalLayer_InitializationFailed; _initializationCallState = new CallState() { Callback = callback, UserState = userState }; _internalLayer.Initialize(); }