Search code examples
javascriptesriarcgis-js-api

ESRI JavaScript API - Return coordinates from LocateButton widget?


I have successfully implemented ESRI's JavaScript API's LocateButton widget. I can zoom to my current location on-click. However, I've failed finding a method to return the X,Y coordinates of the location where it zooms.

Any advice or suggested readings? I'm new to JS but could I use something like an on-click event to return the values? How would I query them? Thanks!

I've included the code snippet below:

var testSymbol = new esri.symbol.PictureMarkerSymbol({
   "angle": 0,
   "xoffset": 0,
   "yoffset": 12,
   "type": "esriPMS",
   "url": "http://static.arcgis.com/images/Symbols/Basic/BlackStickpin.png",
   "contentType": "image/png",
   "width": 24,
   "height": 24
});

geoLocate = new LocateButton({
    map: map,
    highlightLocation: true,
    symbol: testSymbol
}, "LocateButton");
geoLocate.startup();

Solution

  • The key line in the documentation is this:

    This widget uses the geolocation api to find the users current location.

    The geolocation API isn't an ESRI-specific thing, it's Javascript - the LocateButton is just a pretty wrapper around the geolocation library. MDN has some good documentation on using geolocation, and specifically you're looking for the getCurrentPosition function:

    navigator.geolocation.getCurrentPosition(function(position) {
      do_something(position.coords.latitude, position.coords.longitude);
    });