19 December, 2007

Getting the map to zoom to a feature in ESRI webADF is a chore

ESRI can be as frustrating as it can get. My lack of experience with ASP.NET 2.0/3.0 combined with the vast differences betwen the desktop functionality of ArcGIS and the web functionality of ArcIMS / WebADF is giving me gray hair faster then my kids every could dream of doing.

I have a .NET pretty much out of the box site - that is, I went into "ArcIMS Web Manager for the Microsoft .NET Framework" and setup a new site. I have some queries that had to be customized, and I ran out of time trying to get the TaskResults panel to display the query results (I'll post about that experience when I have a little more time), so I put them in an HTML table in their own panel. I have a hyperlink in the table which, when clicked, should zoom to that feature on the map. I have looked and, and am using some code from this thread how can i zoom to my selected features - only it isn't working fully.

// This is the method to zoom to the feature, param 'key' is the objectid of the feature, and the param 'layer' is the name of the feature layer
protected void ZoomToFeature(string key, string layer)
{
ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality mf;
ESRI.ArcGIS.ADF.IMS.Carto.MapView mv;
mf = (ESRI.ArcGIS.ADF.Web.DataSources.IMS.MapFunctionality)Map1.GetFunctionality(0);
mv = mf.MapView;
ESRI.ArcGIS.ADF.IMS.Carto.Layer.FeatureLayer queryLayer;
queryLayer = (FeatureLayer)mv.Layers.FindByName(layer);
Filter queryFilter = new Filter();
QueryParameters queryParams = new QueryParameters(queryFilter);
queryFilter.WhereExpression = "ObjectID = '" + key + "'";
queryFilter.Tolerance = 20;
queryFilter.ToleranceUnits = BufferUnits.Meters;
queryParams.ReturnGeometries = true;
queryParams.ReturnGlobalEnvelope = true;
ESRI.ArcGIS.ADF.IMS.Display.Symbol.FeatureSymbol fs = new ESRI.ArcGIS.ADF.IMS.Display.Symbol.SimpleFillSymbol(System.Drawing.Color.White, System.Drawing.Color.Red, ESRI.ArcGIS.ADF.IMS.Display.Symbol.PolygonFillType.Solid);
ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer sr = new ESRI.ArcGIS.ADF.IMS.Display.Renderer.SimpleRenderer(fs);
queryLayer.Visible = true;
FeatureTable ft = queryLayer.Query(queryParams);
if (ft.Rows.Count > 0)
{
ESRI.ArcGIS.ADF.IMS.Geometry.Geometry featureGeom = (ESRI.ArcGIS.ADF.IMS.Geometry.Geometry)ft.Rows[0]["#SHAPE#"];
ESRI.ArcGIS.ADF.Web.Geometry.Geometry adfGEom = ESRI.ArcGIS.ADF.Web.DataSources.IMS.Converter.ToADFGeometry(featureGeom);
ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfEnv = ESRI.ArcGIS.ADF.Web.Geometry.Envelope.GetMinimumEnclosingEnvelope(adfGEom);
Map1.Extent = adfEnv;
}
Map1.Refresh();
}

// This is the code in RaiseCallbackEvent(string responseString) method which calls the ZoomToFeature method
this.ZoomToFeature(eventArg, layerArg);
response = Map1.CallbackResults.ToString();

I am getting new envelope for the feature, and I am setting the map extents to that envelope. I know this works because I put some dummy code in a Map1_ExtentsChanged event handler and checked to ensure the map extents were changed. Then I hit Map1.Refresh(); but the map does not change. I'm returning the Map1.CallBackResults.ToString();, but I'm ignorant of what to do with the results on the client side. The RaiseCallbackEvent method returns the 'response' string.


After playing around and doing some more searching with different keywords, I found this thread; The ESRI Map control does not refresh ..... which talks about client side map.redraw(); giving the following example:

var map = Maps['<%=Map1.ClientID %>'];
map.redraw();

I thought this would solve my problem and it almost-but-not-quite does. It does redraw the map (at least it looks like it does). But what I get is a completely blank map until I either zoom in or out. Then the map appears zoomed to roughly the correct location (depending on if I zoomed in or out to make it show up). Also works using the mouse wheel.




Technorati Tags: , , , ,

5 comments:

Anonymous said...

hello,
i have the same problem as the one described by you. i have read that esri forum thread and after redraw operation the map is blank and it still remains blank even if i zoomin/zoomout/etc. have you solved the problem without having the blank map?

Unknown said...

Yes! As a matter of fact I have been able to get this to work. I really need to post an update on this process. Thanks for reminding me. Basically, I added the following to the Page_Load event:

ClientScriptManager cm = Page.ClientScript;
string pcr = cm.GetCallbackEventReference(this, "arg", "processCallbackResult", "context", true);
string pcrFunc = "function callServer(arg,context) {" + pcr + "}";
cm.RegisterClientScriptBlock(this.GetType(), "callServer", pcrFunc, true);

And in addition, I added the following to the zoom to method after setting the map.Extents property:


Map1.Refresh();
Page.ClientScript.GetCallbackEventReference(Page, Map1.CallbackResults.ToString(), "processCallbackResult", "Map1", true);


If this doesn't work for you, if you can, send me an example of your code to bigsibling2k5 at gmail dot com, and I will see if there is anything I can do to help.

Anonymous said...

me again!
firstly i want to thank you for your quick answer.

i have tried your code and it seems to work a little bit - the map is no more blank after trying to zoomto a feature, but the map is efectively "zoomed to" only after a pan or zoom operation.

that's why i think there is a problem with redraw operation. Cann you please tell me where is map.redraw() called?
i have tried to put it after Map.Refresh() but it still doesn't work.

Unknown said...

What I can advise here, is to make sure your callback script, that is, the one that will handle the callback after the map zooms, is set to "processCallbackResult". The map.redraw() is a red herring, I don't believe it does anything at all. And ESRI has informed me that there is no functionality to redraw or refresh the map from JavaScript.

vipul said...

Hi I have a similar problem of map1.refresh(); not working..i tried to implement your code. but did not work for me. Iam using arcgis 10 .net adf. please suggest..