How can I get the SceneView size in kilometers? I guess it would be based on the current zoom and the actual pixel size of the container, but have anyone done this?
What do you mean by size of the scene? If you want the distance between the left edge of the sceneview and the right edge, then I would just find the distance between those two points. You'll have to decide whether you want a planar or geodesic measurement.
Here's a code sample for this:
<SPAN class="comment token">// pull in esri/geometry/geometryEngine and esri/geometry/Polyline</SPAN> <SPAN class="comment token">// args: sceneView: SceneView, planar: boolean</SPAN> <SPAN class="keyword token">function</SPAN> <SPAN class="token function">getSceneWidth</SPAN><SPAN class="punctuation token">(</SPAN>sceneView<SPAN class="punctuation token">,</SPAN> planar<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> <SPAN class="keyword token">let</SPAN> leftBound <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>x<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">:</SPAN> sceneView<SPAN class="punctuation token">.</SPAN>height<SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// left edge of scene in pixels</SPAN> <SPAN class="keyword token">let</SPAN> rightBound <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN>x<SPAN class="punctuation token">:</SPAN> sceneView<SPAN class="punctuation token">.</SPAN>width<SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">:</SPAN> sceneView<SPAN class="punctuation token">.</SPAN>height<SPAN class="operator token">/</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">}</SPAN> <SPAN class="comment token">// right edge of scene in pixels</SPAN> <SPAN class="comment token">// iterate along scene until we have a valid point</SPAN> <SPAN class="comment token">// this is necessary if the scene is zoomed out and the</SPAN> <SPAN class="comment token">// left/right edges are in space</SPAN> <SPAN class="keyword token">while</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN>sceneView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">toMap</SPAN><SPAN class="punctuation token">(</SPAN>leftBound<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> leftBound <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> x<SPAN class="punctuation token">:</SPAN> leftBound<SPAN class="punctuation token">.</SPAN>x <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">:</SPAN> leftBound<SPAN class="punctuation token">.</SPAN>y <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">while</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="operator token">!</SPAN>sceneView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">toMap</SPAN><SPAN class="punctuation token">(</SPAN>rightBound<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">{</SPAN> rightBound <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN> x<SPAN class="punctuation token">:</SPAN> rightBound<SPAN class="punctuation token">.</SPAN>x <SPAN class="operator token">-</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> y<SPAN class="punctuation token">:</SPAN> rightBound<SPAN class="punctuation token">.</SPAN>y <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">let</SPAN> p1 <SPAN class="operator token">=</SPAN> sceneView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">toMap</SPAN><SPAN class="punctuation token">(</SPAN>leftBound<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">let</SPAN> p2 <SPAN class="operator token">=</SPAN> sceneView<SPAN class="punctuation token">.</SPAN><SPAN class="token function">toMap</SPAN><SPAN class="punctuation token">(</SPAN>rightBound<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="comment token">// planar measurement</SPAN> <SPAN class="keyword token">if</SPAN> <SPAN class="punctuation token">(</SPAN>planar<SPAN class="punctuation token">)</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// find in meters, divice by 1000 to get to km</SPAN> <SPAN class="keyword token">return</SPAN> p1<SPAN class="punctuation token">.</SPAN><SPAN class="token function">distance</SPAN><SPAN class="punctuation token">(</SPAN>p2<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">/</SPAN> <SPAN class="number token">1000</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="keyword token">else</SPAN> <SPAN class="punctuation token">{</SPAN> <SPAN class="comment token">// geodesic measurement</SPAN> <SPAN class="keyword token">let</SPAN> polyline <SPAN class="operator token">=</SPAN> <SPAN class="keyword token">new</SPAN> <SPAN class="token class-name">Polyline</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">{</SPAN> paths<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">[</SPAN> <SPAN class="punctuation token">[</SPAN>p1<SPAN class="punctuation token">.</SPAN>longitude<SPAN class="punctuation token">,</SPAN> p1<SPAN class="punctuation token">.</SPAN>latitude<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN>p2<SPAN class="punctuation token">.</SPAN>longitude<SPAN class="punctuation token">,</SPAN> p2<SPAN class="punctuation token">.</SPAN>latitude<SPAN class="punctuation token">]</SPAN> <SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> spatialReference<SPAN class="punctuation token">:</SPAN> <SPAN class="punctuation token">{</SPAN> wkid<SPAN class="punctuation token">:</SPAN> <SPAN class="number token">4326</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="keyword token">return</SPAN> geometryEngine<SPAN class="punctuation token">.</SPAN><SPAN class="token function">geodesicLength</SPAN><SPAN class="punctuation token">(</SPAN>polyline<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'kilometers'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">;</SPAN> <SPAN class="punctuation token">}</SPAN> <SPAN class="punctuation token">}</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Another option is to take the ratio between some distance in meters (or whichever linear unit) and that same distance in pixels and use that to convert. The issue with this method in a sceneview is that that ratio will vary based on where on the globe you're measuring, even at the same zoom.
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.