I have this logic that I am trying to accomplish: I would like to get the centroid point of the polygon after being created and then perform the reverse geocode from that point to get informtions such as the full address, the county and township.
Any advice on accomplishing this?
Also, since the update on editor is done automatically, I am wondering if there could be an event handler for editor complete or update complete!?
There is:
public event OnCreateFeatureEventHandler OnCreateFeature
There is help page (didn't find current doc, but the syntax is the same)
Listen to that event, get the feature, calculate center, do reversegeocodemagic, profit!
The OnCreateFeatureEventHandler is only for ArcObjects - this question relates to the JavaScript API
Rene has the right approach hooking in to the draw-end event of the Draw toolbar.
Also, since the update on editor is done automatically, I am wondering if there could be an event handler for editor complete or update complete!?
Are you referring to when the locator has returned a result or something else here?
Rene's example logs the address result to the console window but you could also store it.
Oh, sorry for confusing. I have lot to learn from this Geonet - I didn't notice the TOPIC of the discussion at all
You can grab the centroid of the polygon geometry and use that with the Locator task to find the address.
You'll want to use the locationToAddress method.
It would look something like this
//somewhere add listener tb = new Draw(map); tb.on("draw-end", addGraphic); // listener method function addGraphic(evt) { var pt = evt.geometry.getCentroid(); locator.locationToAddress(pt, 100).then(function(addresses) { console.debug('Address Results', addresses); }); map.graphics.add(new Graphic(evt.geometry, symbol)); }
I tried making a JSBIN to demonstrate, but you need to add a proxy to use the Locator Task service, so it won't work in entirely, but should give you a good start.
Hope that helps
Thank you Dene.
I am trying your approach which I think should be able to work.
the getCentroid method works fine but when calling the reverse geocode I get this error: Invalid query parameters which I believe may be related to the coordinates x and y?
You're going to want to use webMercatorUtils to convert the pt to match the GeocodeServer. That should work in most cases.
Hi Rene,
Sorry I am still having problems with this.
here is the code
map.graphics.add(new Graphic(evt.geometry, symbol));
var point = evt.geometry.getCentroid();
var point2 = new esri.geometry.Point(Number(point.x), Number(point.y), map.spatialReference);
var mp = esri.geometry.geographicToWebMercator(point2);
console.log(point2.x + " , " + point2.y);
console.log(mp);
and here is the output : and it is still failing when calling locator.locationToAddress()
Richard,
What is the Spatial Reference of the evt that you are getting back? In your code you are creating an new point and setting the new points SR to the maps and then using ToWebMercator. So if the map is already 102100 then this process is not right. What SR is your GeoCodeServer using?
If you GeocodeServer allows Geographic coords (i.e 4326) the you can use this line:
locator.locationToAddress(webMercatorUtils.webMercatorToGeographic(evt.mapPoint), 10);