I have a widget that creates a and populates a map after a queryTask returns its results. My problem is when i perform the query a second time with a different result, the map won't recenter nor draw the graphics around the polygon (the result).
I create the map like this:
newExtent = new Extent(results.features[0].geometry.getExtent());
fichaMap = new Map("mapDiv", {
extent: newExtent,
showLabels: true,
logo: false,
zoom: 16,
slider: false
});
After that i create a mapPoint and have a centerAt() inside a on("click") function to recenter the map to the new result as it doesn't do it automatically. Is there anything i can do to make this onCenter work? I can provide more info if needed:
edit: Pastebin with all the code in it(service urls removed for security purposes): [JavaScript] widget.js - Pastebin.com
Solved! Go to Solution.
Matheus,
Why are you creating a new map object?.. In your widget if you want to set the maps extent you just use:
this.map.setExtent(newExtent);
Matheus,
Why are you creating a new map object?.. In your widget if you want to set the maps extent you just use:
this.map.setExtent(newExtent);
I unfortunately need a new map object, as this widget generates a report (to later be printed) in a separate modal. The map serves the purpose of illustrating the location of whatever the query result is.
Are there any errors in the web console when you perform the query a second time?
Nope, no errors. Here it is in a pastebin: [JavaScript] widget.js - Pastebin.com
Matheus,
I think you should be creating the new Map once (at startup) and not each time you click the print button, unless you are going to use domConstruct.destroy on the mapDiv.
Could i use destroy to "remake" the map on every click of the button?
So i tried having the map be declared and created at startup, and it seems to work as the extent changes at every click of the button. Now there's a new issue, however. Upon the second click, i believe the layers added to the map are out of order, as one of them is drawn over the other (should be the other way around). That and the graphics aren't redrawn. But that i believe is caused by it being inside the on "load" function (i had it placed in here as i'd get an error if it wasn't)
Matheus,
So now you will have to re-evaluate your work flow. As it stands you are adding featurelayers to the map each time the button is clicked. But now that the map is already created you need to see if that featurelayer is already part of the map and if it is then do not add it again. Anything you had in the map load event will only get called one time when the map initially loads so you might need to move those in your workflow.
I got the map to be remade at every click by just calling .removeAllLayers(); before any layers are added. That way the map will be "remade" on the second and consecutive presses of the button.