JS API 4.17 sketch widget rectangle, how to get height width

946
2
Jump to solution
10-20-2020 07:57 AM
johnbrosowsky
Occasional Contributor II

How can you get height and width in screen pixels of a rectangle drawn with the sketch widget?

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Get the geometry (extent) use the xmin, ymin, xmax, ymax to create a points then the views toScreen method to get the point screen coordinates and do the math. 

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Get the geometry (extent) use the xmin, ymin, xmax, ymax to create a points then the views toScreen method to get the point screen coordinates and do the math. 

0 Kudos
UndralBatsukh
Esri Regular Contributor

Hi there, 

You can listen to create event on the sketch. When the create operation is completed, check the tool that created a shape, then divide the rectangle polygon extent's width and height by the view resolution. This will give you the rectangle polygon's extent width and heights in pixels. 

sketch.on("create", function(event){
  // when draw rectangle create event is completed
  if (event.state === "complete" && event.tool === "rectangle"){
     console.log(event.graphic.geometry.extent.width / view.resolution, "pixels");
     console.log(event.graphic.geometry.extent.height / view.resolution, "pixels");
  }
});‍‍‍‍‍‍

Here is a doc for the MapView.resolution

-Undral