GetCentroid of selected feature layer, scale-dependent issue, Javascript API

4349
9
06-20-2015 05:41 PM
ShawnaErnst
Occasional Contributor

I've written some code based on this ESRI sample:  Feature Layer with Selection

My goal is to have the user select property boundaries (parcels) on the map, use the selected features to get the centroid of each property, and then send those centroids through to the Directions widget.

Here's the function that runs after the user has already selected their property boundaries (wired to a button click):

Capture.JPG

And here's the issue...depending on my zoom-level at the time of selection, I get vastly different results.

When selecting parcels while zoomed-out, centroids look correct, but selection graphics become triangles:

OddGeometry1.JPG

Mid-zoom selection produces these results, which appear correctly:

OddGeometry2.JPG

When selecting while zoomed-in closely, I get wildly inaccurate centroids.

OddGeometry3.JPG

It's obvious that I have something wrong, but I'm not sure what...  I'm new at JavaScript, so I'm guessing that I haven't accounted for something in the way the graphics work.

Any help would be much appreciated!

Message was edited by: Shawna Ernst Added attachment with the full code (service names removed).

0 Kudos
9 Replies
RobertScheitlin__GISP
MVP Emeritus

Shawna,

   WOW, I have not seen such displacement errors before. Can you share all of your code for review?

ShawnaErnst
Occasional Contributor

Sure, Robert.  I appreciate you taking a look.  I just attached it to my original post.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shawna,

   I don't seem to have these issues when I swap out your URL for mine and use your exact code. Seems like you may be having map service issues.

ShawnaErnst
Occasional Contributor

Interesting.  Okay- I will play around with my map service.

It's running on 10.3 and set to Web Mercator to match the ESRI basemap.

0 Kudos
ShawnaErnst
Occasional Contributor

I've tried this with a 10.3 service, 10.0 service, and an AGOL-hosted feature layer - and I get the exact same issue with all my tests.

I've also tried updating the maxAllowableOffset parameter in Javascript, updating the "Max number of records returned by the server" parameter on the map service, and using a completely different dataset - all tests produced the same result.

I think I may open a support case.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shawna,

   Yes my recommendation would be to call tech support.

0 Kudos
SteveCole
Frequent Contributor

What happens when you specify "autoGeneralize: false" when constructing the parcel featureLayer?..

ShawnaErnst
Occasional Contributor

Steve,  that makes it so my third picture (the zoomed-in selection), is what I get every single time.  So, I never get the centroids within the parcels, but it does help with the weird triangles from the first (zoomed-out) picture.

0 Kudos
ShawnaErnst
Occasional Contributor

For everyone following this...I submitted a support ticket to ESRI and they came back with the suggestion that I change

var parcelCentroid = graphic.geometry.getCentroid()

to:

var parcelCentroid = graphic.geometry.getExtent().getCenter();

They gave the following reason:  "The geometries can be complex at times, may be self intersecting and essentially depends on the pointcollection and rings used to create the polygon. So generally the approach is to generalize or simplify the geometry. You could use the above line instead of getcentroid() method.  I have tested it on my end and is working for me."

I didn't go with this approach initially because I was concerned that the center of the extent won't always be located within the polygon boundary.

I may end up creating parcel points and just selecting those.  I'd prefer to do it in code, but I want to make sure what I've got is accurate.