How to convert the extent of Mapview to lat/lon?

334
2
04-21-2020 01:39 AM
LucasLiu
New Contributor

Hi, guys.

I have got the extent of the map view from its extent property. As I need to load the data async from the backend when the user drags the map, so every time after user drag and stops, I need to send an API to the backend, which takes the current map lat/long on the four corners of the screen to the params of the API. I find the extent of the Mapview property, but its format is different from the lat/long. Is there any way or API to get the suitable lat/long of the four corners of the screen on the map?

Thank you.

Tags (1)
0 Kudos
2 Replies
ThanHtetAung_EsriAu
Esri Regular Contributor

Hi Lucas Liu‌,

You can re-project your extent to spatial reference wkid 4326. That is lat/long.

I assume you are using arcgis js 4.xx

Here is my sample code of view extent change with reprojection  - https://codepen.io/thanhtetaung/pen/NWGRZGJ 

And here are some help reference

projection | ArcGIS API for JavaScript 4.15 

GeometryService | API Reference | ArcGIS API for JavaScript 3.32 

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Lucas,

 An even simpler way than Than suggests (assuming you are using an ESRI Basemap) is to use WebMercatorUtils to convert the view which is in WebMercator (WKID 102100) to Geographic (WKID 4326).

https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-support-webMercatorUtils... 

require(["esri/geometry/support/webMercatorUtils"], function(webMercatorUtils) {
var latLonExtent = webmercatorUtils.webMercatorToGeographic(view.extent);
var xMin = latLonExtent.xmin;
var yMin = latLonExtent.ymin;
var xMax = latLonExtent.xmax;
var yMax = latLonExtent.ymax;
});‍‍‍
0 Kudos