[GIS] Draw a point with ArcGIS Javascript API

arcgis-javascript-apijavascript

I am currently trying to draw a point with the ArcGIS Javascript API.

Pretty much like this example, http://help.arcgis.com/en/webapi/javascript/arcgis/demos/toolbar/toolbar_draw.html, except the point will not be drawn with the mouse, but from an javascript object.

If we look at the code, we see that a geometry object/array is passed in the function.

function addToMap(geometry) {
    toolbar.deactivate();
    map.showZoomSlider();

    var symbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE, 10, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([255,0,0]), 1), new dojo.Color([0,255,0,0.25]));

    var graphic = new esri.Graphic(geometry, symbol);
    map.graphics.add(graphic);
  }

If someone could write the structure for that object, and a method to draw the point (such as a button, with an onclick event, containing fixed xy coords)

Dumping geometry looks like this http://pastebin.com/P9wfL36b (ouch!)

This code will be used in a GPS tracking experiement, where I'll pull the current position from a database every 20 seconds or so. (details will not be covered in this post)

Bonus question: How do I convert long/lat to xy coords from the sample?

I use UTM zone 29N, wkid: 32629

Best Answer

Adding a point from a button click can be as simple as:

dojo.connect(dojo.byId('pointButton'), 'onclick', function() {
  map.graphics.add(new esri.Graphic(
    // Point coordinates are 0, 0
    new esri.geometry.Point(0, 0, map.spatialReference),
    new esri.symbol.SimpleMarkerSymbol()
  ));
});

And the pointButton node is:

<button id="pointButton">Click to add a point.</button>

To add a point when the map is clicked is pretty straightforward as well:

dojo.connect(map, 'onClick', function(evt) {
  map.graphics.add(new esri.Graphic(
    evt.mapPoint,
    new esri.symbol.SimpleMarkerSymbol()
  ));
});

Live example showing both here: http://jsfiddle.net/swingley/auyHf/

Regarding converting from lat, long to another coordinate system: if you're converting to or from lat, long to web mercator, there are client side methods available to do this: esri.geometry.geographicToWebMercator and esri.geometry.WebMercatorToGeographic. If you need to convert from lat, long to something other than web mercator, then the geometry service's project method is the way to go.

Related Question