Obtaining Lat/Long of the user's position in ArcGIS API JS 4.16

1748
4
07-13-2020 09:22 AM
OmarElhadi
New Contributor II

I want to acquire the user's current position to use it in different functions. Locate widget works perfectly but I am not able to get the latitude and longitude from it. Are there any alternatives to Locate widget?

#web-apps

#arcgis-api-js-4.16

#geolocation

0 Kudos
4 Replies
RobertScheitlin__GISP
MVP Emeritus

Omar,

   The locate widget has locate event that returns the GeoLocationPostion object that contains position.coords.latitude and longitude.

locateBtn.on('locate', function(pos){
  console.info(pos.position.coords.latitude, pos.position.coords.longitude);
});
OmarElhadi
New Contributor II

Thanks, Robert Scheitlin, GISP‌ for the quick response. It WORKS just fine.

Is there is another way to fire the locate function rather than clicking the locate button?

Right now I have a neat menu from where all my functions are triggered. However, when I need the current position I press a button to show the locate button and say "Hey! click this button". A very complicated method.

Can I request the position with my own custom button?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Omar,

   Sure just dont add the widget to the view and then just call the locate method:

https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Locate.html#locate 

OmarElhadi
New Contributor II

Robert,

All issues have been resolved. Thanks for your support, much appreciated.

0 Kudos