Get coordinates from draw toolbar

890
5
02-26-2013 11:47 AM
JessicaKnight1
New Contributor III
I am using the draw toolbar in my application to place a point. I'm pretty new to programming and can't seem to find any concrete way to get the x-y coordinates from that point. Can anyone point me in the right direction? Thanks.
0 Kudos
5 Replies
by Anonymous User
Not applicable
0 Kudos
JessicaKnight1
New Contributor III
What I'm trying to do is actually return the x,y coordinates in my application. Are there any samples that really illustrate doing that? As I said, I'm a programming novice and I guess I can't quite connect the dots from the documentation at the moment. Thanks.
0 Kudos
by Anonymous User
Not applicable
If you look at the first sample I linked to, find the 'addToMap' function.  This function has been connected to the toolbar's onDrawEnd event, and it illustrates how to take the resulting geometry and add it as a graphic to the map.  Within this callback function, you can access all of the properties of the geometry that was drawn on the map.

For example, in your case, if you just want to handle the x/y coordinates for point features, then try the following:

dojo.connect(toolbar,"onDrawEnd",function(geometry){
  if (geometry.type=="point")
  {
    // Do something.... 
    alert(geometry.x+", "+geometry.y);
  }
});


If you open the live sample, and execute the above code in the JavaScript console (pressing F12 will reveal the console for you in recent browsers), you should see coordinates alerted the next time you draw a point feature on the map.

At that point, you can do whatever you need to do with the coordinate values.
0 Kudos
JohnGravois
Frequent Contributor
mike is definitely right that using the onDrawEnd event is your best opportunity to capture the values from a sketched point.  heres a fiddle that shows how its done.

dojo.connect(tb, "onDrawEnd", writeCoordinates);
...
function writeCoordinates(geometry) {
        dojo.byId("coordinateInfo").innerHTML = "X coordinate is: " + geometry.x.toFixed(3) + ", Y coordinate is: " + geometry.y.toFixed(3);
      }
...
<span id="coordinateInfo"></span>


i adapted it from this sample...
0 Kudos
JessicaKnight1
New Contributor III
Thanks very much. I'll give that a shot!
0 Kudos