Custom map center

1505
13
03-02-2018 06:04 AM
EvelynHernandez
Occasional Contributor III

Hello.

I want to know if there is a way to move the center of the map when querying a little bit.

Iam querying some feature (just 1 result) and im doing this for showing it in the center of the map.

    map.centerAndZoom(featureSet.features[0].geometry.getExtent().getCenter(),20);

In my app when i do this i have a problem on visualization, thats why i want to move it to the right a little bit.

This is what i obtain: (See the blue line on the screen)

The red container is over the map, thats why when i do the center i got that.

this is what i want to get:

Thanks in advice

0 Kudos
13 Replies
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

   Have you considered extent offset?

https://developers.arcgis.com/javascript/3/jsapi/extent-amd.html#offset 

0 Kudos
EvelynHernandez
Occasional Contributor III

Oh no, is there any example on how to apply that?. I should do it into the map or how?

Thanks in advice

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Evelyn,

   So thing like this after your query is done and you get the extent you want to zoom to.

//your get the extent of your feature to zoom to and before
//you set the maps extent ofsset it by a certain Y
map.setExtent(extent.offset(200,0), true);
EvelynHernandez
Occasional Contributor III

This work for me, thanks!

map.centerAndZoom(featureSet.features[0].geometry.getExtent().getCenter(),20);
map.setExtent(featureSet.features[0].geometry.getExtent().offset(-10,0), true);
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Great, Don't forget to mark this question as answered by clicking on the "Mark Correct" link on the reply that answered your question.

0 Kudos
HoriaTudosie
Occasional Contributor II

I wonder if you cannot do it in just one step:

map.centerAndZoom(featureSet.features[0].geometry.getExtent().offset(10,0).getCenter(),20);
EvelynHernandez
Occasional Contributor III

Hello again.

Im having an issue with this 

map.centerAndZoom(featureSet.features[0].geometry.getExtent().offset(10,0).getCenter(),20);

The error says Cannot read property 'offset' of null.

Im doing a query to a layer and i got the geometry and attributes correctly.

So still i cannot move the extent to the position i want.

0 Kudos
HoriaTudosie
Occasional Contributor II

I do not have an environment at this moment to check the error.

Beside may be the fault of a corrupted layer.

 

I would put a breakpoint on that line to check if getExtent really returns something.

Sometimes, the layer may be corrupted and one or more features may have no geometry!

The error may come from those incomplete features without geometry.

 

Another ways to go around would be to extract the geometry and check it for null.

Then getExtent should return something, but you may check it again for null.

If you got a null for the two above checks, log the feature.

 

Or put in in a try/catch block and log the error along with the feature Id (or just the feature.)

 

This (either) way, you may find if that was the case (some features do not have geometries, although they should!

If so, keep (or add) the try/catch block with or without the log.

0 Kudos
EvelynHernandez
Occasional Contributor III

There is a geometry, but the function doesnt

0 Kudos