2

I'm working with arcGIS javascript api 3.12.

My current call for displaying the feature layer is as follows:

var recLayer = new FeatureLayer("http://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3" ,{
  infoTemplate: recParkTemplate,
  outFields: ["STATE_NAME"]
});
map.addLayer(recLayer);

However, instead of plotting the polygon on the map as this is an esriGeometricPolygon. I would rather have it plot on the map like a esriGeometryPoint. I know this method in getting the specific polygon's centroid:

https://developers.arcgis.com/javascript/jsapi/polygon-amd.html#getcentroid

What I have done so far is something like this when the map has finished loading:

recLayer.on("graphic-add", function(graphic) {
        console.log("\n\n" + recLayer.graphics.length);
        console.log(graphic.graphic);
});

What this does is give me the graphics object as they are loaded. From here I figure I can manipulate them into displaying the centroid alone. However, this is not working as expected where I'd think it would be

graphic.getExtent().getCenter();

This comes with an error. From there I'd like to change the graphic to a plot point and then have a pin display at the center of the polygon.

Do I have the right approach? Is there something I'm missing?

Here is the current site if you would like to take a look at it: http://joshferrell.net/ece_project/

2

Set the geometry for the graphic using the setGeometry method. Also set the symbol on the graphic OR you may change the renderer (using marker symbols) on the featureLayer when the featureLayer loads.

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

recLayer.on("graphic-add", function(graphic) {

    console.log("\n\n" + fl.graphics.length);
    console.log(graphic.graphic.geometry.getCentroid());
    //get centroid
    var centroid = graphic.graphic.geometry.getCentroid();
    //set geometry for the graphic
    graphic.graphic.setGeometry(centroid);
    //set symbol for the graphic
    graphic.graphic.setSymbol(pointSymbol);

});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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