JSAPI v3.30 - Set map extent using offset

238
2
12-18-2019 01:07 PM
MaximeDemers
Occasional Contributor III

Hi,

I am trying to set map extent using an offset because there is a floating panel over the map and sometimes, when I zoom to a feature extent, the feature is partly hidden behind it.

I would like to offset the extent so it will always be displayed and centred in the map section that is not hidden by the panel

This is what I tried so far without success:

function zoomToExtent(extent) {
  var hiddenMapPartTopLeft = map.toMap(new ScreenPoint(0, 0)),
      hiddenMapPartBottomRight = map.toMap(new ScreenPoint(panel.position.left + panel.position.width, map.container.offsetHeight));

  var dx = new Extent({
    xmin: hiddenMapPartTopLeft.x,
    ymin: hiddenMapPartBottomRight.y,
    xmax: hiddenMapPartBottomRight.x,
    ymax: hiddenMapPartTopLeft.y,
    spatialReference: map.spatialReference
  }).getWidth();

  extent = extent.offset(dx, 0);
  map.setExtent(extent);
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

The map zoom way too far to the right. Is there a solution for this?

0 Kudos
2 Replies
BenElan
Esri Contributor

If the issue is that it is moving too far right, you can increment the extent by an arbitrary value in order to fine tune the offset.

var panIncr = 5000;
               
var xMin = layer.fullExtent.xmin + panIncr;
var xMax = layer.fullExtent.xmax + panIncr;

var extent = new Extent(xMin, layer.fullExtent.ymin, xMax, layer.fullExtent.ymax, map.spatialReference)

map.setExtent(extent)‍‍‍‍‍‍‍‍

Edit: Here is a sample: https://codepen.io/benesri/pen/qBERQwM

0 Kudos
MaximeDemers
Occasional Contributor III

Hi, thanks for your answer.

I am not sure how to define the arbitrary value. As the value is in map unit (meters) and the width of the floating panel in pixels, does the panIncr will change with the zoom level?

Also, I would like the extent to be centred in the right part of the map, the part that is not hidden by the panel. Then, I suppose the offset will be different from a layer extent to another?

0 Kudos