[GIS] Add point to center of polygon graphic in the Esri javascript api

arcgis-javascript-apijavascript

I’m using the ESRI JavaScript API 3.3 to develop an application. I am selecting parcels and adding a graphic for the selected polygon. What I would like to do is add a point marker to the center of the polygon graphic after the graphic is added. Does anyone know of a sample I could take a look at, or know how to quickly implement this functionality? I feel like I’m missing something simple.

Best Answer

Unless your parcel polygons are unusually shaped, you get get the center point of the extent of the parcel polygon and use that to draw your center graphic.

// in this example, the graphic variable is the graphic of the parcel you added to the map
var centerPoint;
switch (graphic.geometry.type) {
    case "point":
        // if the graphic is a point
        centerPoint = graphic.geometry;
        break;
    case "extent":
        // if the graphic is an extent
        centerPoint = graphic.getCenter();
    default:
        // if the graphic is a line or polygon, which for a parcel this will probably
        // be the case.
        centerPoint = graphic.getExtent().getCenter();
}
var centerGraphic = new esri.Graphic(centerPoint, centerSymbol);

...