Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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);

...

share|improve this answer
    
Nice solution and clean. –  Jamie Mar 19 '13 at 20:04
    
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. –  Zachary Mar 19 '13 at 20:21
add comment

So here are two quick solutions. I really do not think either are the optimal way, but would be easy.

  1. 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.

  2. 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

share|improve this answer
2  
Per @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. –  awesomo Mar 19 '13 at 19:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.