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.
2 Answers
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);
...
-
-
Beautiful, that is it exactly. I had flirted with a variation on it but was missing the getExtent before I tried to do the getCenter. A +1 and the solution.– ZacharyCommented Mar 19, 2013 at 20:21
So here are two quick solutions. I really do not think either are the optimal way, but would be easy.
Place a marker symbol where the user clicks. That way the visual effect will be the point will be on the polygon that was selected.
User the label points geometry task to get an geometry to know where to place the marker symbol. http://help.arcgis.com/en/webapi/javascript/arcgis/jsapi/geometryservice.html#GeometryService/onLabelPointsComplete
-
2Per @Jamie #2 solution, you need to enable the geometry service on your arcgis server or you can use the sampleserver3.arcgisonline.com/ArcGIS/rest/services/Geometry/…. One caveat, for those who might use the service for centriod creation, it does not guarantee a centroid from the docs, but it does guarantee an interior point. You could publish a geoprocess from the "Feature To Point" tool to enable centroids.– awesomoCommented Mar 19, 2013 at 19:40