I have to clarify what I said in this thread because it could be confusing.
If your data are dynamic either because the data are loaded by other applications or because you are filtering at the client side (using layerdefs), it's not possible to easily know the extent at the client side (this was the initial subject of this thread).
Now if your data are static and that you can rely on the extent provided by the service, it's possible to get the extent of layer and sublayers at the client side:
- for a layer, there is a 'FullExtent' property
- for a sublayer, you can deserialize the info provided by the sublayer rest resource. For example, look at the extent info provided by this sublayer.
I'm not sure about doing this in Silverlight, but I had the exact same problem using the JSAPI and I solved it using an AJAX call to the server hosting the map services. (Caveat: This will only work if the map services are hosted on the same server as the web app.) Here's my code:function zoomToLayer(id) {
//first, try the layer.fullExtent property
var layer = map.getLayer(id);
if (layer != null) {
map.setExtent(layer.fullExtent);
}
else {
//the layer is a sublayer, so we must dig deeper
dojo.forEach(map.layerIds, function(layerId) {
layer = map.getLayer(layerId);
dojo.forEach(layer.layerInfos, function(layerInfo) {
if (layerInfo.name == id) {
//now we have a match, so make an AJAX request to the server to get the
//json description of this sublayer, which contains the extent
//so f'ing dumb that the LayerInfo object doesn't wrap this for us...
var args = {
url: "/arcgis/rest/services/Foo/Bar/MapServer/"+layerInfo.id,
handleAs: "json",
content: {f:"pjson"},
load:function(data) {
var extent = data.extent;
var sr = data.extent.spatialReference;
map.setExtent(new esri.geometry.Extent(extent.xmin,extent.ymin,extent.xmax,extent.ymax,sr));
}
};
var deferred = dojo.xhrGet(args);
}
});
});
}
}
Problem solved. I'm sure there's a way to do this in Silverlight, but I literally have zero experience with it. I hope this saves someone some time.