Get selected single point geometry, use in map.centerAndZoom

2908
5
Jump to solution
10-06-2015 12:27 AM
DavidChrest
Occasional Contributor II

I can easily zoom to a selection of points in my FeatureLayer with this handy one-liner:

map.setExtent(graphicsUtils.graphicsExtent(myFeatureLayer.getSelectedFeatures()), true);

In this case, it's assigned to a button.

How do I utilize the map.centerAndZoom when just a single point of my Feature Layer is selected?

Just want to assign to a different button. I can't seem to find the exact syntax but I know I need to grab the geometry of the single selected point to then use in map.centerAndZoom.

Thnks so much. Any help is greatly appreciated.

David

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

David,

So it would look more like this then:

var gExt = graphicsUtils.graphicsExtent(myFeatureLayher.getSelectedFeatures());
if (gExt) { 
  map.setExtent(gExt.expand(1.5), true); 
} else { 
  var mp2 = myFeatureLayher.getSelectedFeatures()[0].geometry; 
  map.centerAndZoom(mp2, 17); 
}

View solution in original post

5 Replies
RobertScheitlin__GISP
MVP Emeritus

David,

  Here is some code I use:

if (gExt) {
  map.setExtent(gExt.expand(1.5), true);
} else {
  var mp2 = currentLayerAdded.graphics[0].geometry;
  map.centerAndZoom(mp2, 12);
}
DavidChrest
Occasional Contributor II

Finally figured out how to insert code nicely to my replies. Here is my quick and dirty zoom and center fix for a single selected feature:

on(dom.byId("zoomToSelectedPoint"), "click", function () {
  centermap();
  setTimeout(function () {
  zoomin17();
  }, 500);
  });


  function centermap() {
  map.setExtent(graphicsUtils.graphicsExtent(activitiesDay1.getSelectedFeatures()), true);
  }


  function zoomin17() {
  map.setZoom(17);
  }
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

David,

So it would look more like this then:

var gExt = graphicsUtils.graphicsExtent(myFeatureLayher.getSelectedFeatures());
if (gExt) { 
  map.setExtent(gExt.expand(1.5), true); 
} else { 
  var mp2 = myFeatureLayher.getSelectedFeatures()[0].geometry; 
  map.centerAndZoom(mp2, 17); 
}
DavidChrest
Occasional Contributor II

Here is my code I came up with for a zoom button that works nicely. Works if multiple points or a single point is selected.

on(dom.byId("zoomToSelectedD1Activities"), "click", function () {
  var countp = activitiesDay1.getSelectedFeatures().length;
  if (countp > 1) {
  map.setExtent(graphicsUtils.graphicsExtent(activitiesDay1.getSelectedFeatures()), true);
  } else {
  var mp1A = activitiesDay1.getSelectedFeatures()[0].geometry;
  map.centerAndZoom(mp1A, 18);
  }
  });
DavidChrest
Occasional Contributor II

Aha! That's it. Thanks so much, Robert. I was trying this last night but had forgotten the "()" between "getSelectedFeatures" and "[0]." Silly me. That's what happens when you stay up 'til 3:00 AM coding.

0 Kudos