How to get lat/long from drag event

2915
3
Jump to solution
01-30-2017 08:31 AM
BorjaCanseco
New Contributor

Hello!

I'm using the Esri JS API version 4.2 with angular-esri-map and I'm working with a 2D map.

I notice that the default behavior for when a user holds Shift and then clicks+drags on the map is that a rectangular bounding box is drawn, which then becomes the bounds of the view when the user lets go.

I'm trying to implement a new behavior in which doing the same thing but with the CTRL key instead of Shift will simply draw a rectangular box on the map without changing the view, and then store the latitude and longitude of both the Northwest and Southeast corners in some JavaScript variables (in my case, variables tied to my angularjs controller).

My question: How do I get lat/long from this? I'm only able to see x/y from both the origin and destination points using the drag event.

$ctrl.onViewCreated = function(view) {
    $ctrl.mapView = view;

    $ctrl.mapView.on('click', function(e) {
      $ctrl.clickCoordinates = {
        lat: e.mapPoint.latitude,
        lng: e.mapPoint.longitude,
      };
      // I can get lat/long here...
    });

    $ctrl.mapView.on('drag', ['Ctrl'], function(e) {
      // ...but not here!
      e.stopPropagation();
    });
};

Thanks!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Borja,

   The values you will be receiving are in screen coordinates and thus have to be converted to map coordinates, so you use the views toMap method:

https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#toMap 

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Borja,

   A click event has a mapPoint property but a drag event does not. The drag event does though has a origin property (in screen coordinates) and both x and y. so you just convert those screen coordinates and the x and y to get your drag bounds.

https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#event:drag 

BorjaCanseco
New Contributor

Thanks for the reply, Robert!

Could you point me in the right direction for conversion? I found a webMercatorUtils service but it seems like the .xyToLngLat() method returns strange values - probably because the map I'm working with isn't a WM projection?

Would I need to use the GeometryService instead?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Borja,

   The values you will be receiving are in screen coordinates and thus have to be converted to map coordinates, so you use the views toMap method:

https://developers.arcgis.com/javascript/latest/api-reference/esri-views-MapView.html#toMap