Updating/Recentering a Map - JS API 3.28

1305
9
Jump to solution
07-29-2019 09:49 AM
Matheus_ViníciusRamos_da_Fonse
New Contributor III

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 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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);

View solution in original post

0 Kudos
9 Replies
RobertScheitlin__GISP
MVP Emeritus

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);
0 Kudos
Matheus_ViníciusRamos_da_Fonse
New Contributor III

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.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Are there any errors in the web console when you perform the query a second time?

0 Kudos
Matheus_ViníciusRamos_da_Fonse
New Contributor III

Nope, no errors. Here it is in a pastebin: [JavaScript] widget.js - Pastebin.com 

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

0 Kudos
Matheus_ViníciusRamos_da_Fonse
New Contributor III

Could i use destroy to "remake" the map on every click of the button?

0 Kudos
Matheus_ViníciusRamos_da_Fonse
New Contributor III

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)

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

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.

0 Kudos
Matheus_ViníciusRamos_da_Fonse
New Contributor III

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.

0 Kudos