ThinkGeo.com    |     Documentation    |     Premium Support

GetClosestPointTo not returning actual vertex?

Here’s my scenario:

  1. The user loads some layers containing line features on the map.
  2. The user clicks near a line and I want to get the closest vertex contained withing that lineshape feature from where the user clicked.

Here’s the main line of code I’m doing this in:

PointShape vertex = (testLineShape.GetShape() as LineShape).GetClosestPointTo(new PointShape(e.WorldX, e.WorldY), GeographyUnit.DecimalDegree);

testLineShape is a line feature. First I get it’s LineShape and then do the .GetClosestPointTo method passing in the user’s click (e.WorldX and e.WorldY).

The problem is it’s not returning the exact vertex. The values for it just don’t match up and I really need them to.

Example:
Here’s the vertex given back to me from the .GetClosestPointTo method:

And here’s the actual vertex apart of the line:

They are the same up to 4 decimal places, but I need the exact value to match.

Hi Dan,

Did you used projection for your layer?
I have a little confuse for your actual vertex, where is come from? From your snapshots, the first value is come from the GetClosetPointTo, it looks correct, but the second, I am not sure what’s the value. It is better if you can provide one simple to help us to recreate this issue.

Thanks
Mark

Hello, I’m not using a projection, just setting the map unit to Decimal Degrees. All the data is unprojected WGS84.

The second value you see is the 102nd vertex in the LineShape. This is the vertex that I was assuming .GetClosestPointTo would give me. As you can see, it gave me a vertex that matched the 102nd vertex up to the 5th decimal place, but differed after.

Also, would it be possible to see the code for your GetClosestPointTo function? Are you just looping through the vertices and doing a normal distance calculation, keeping track of the minimum distance?

Hi Dan,

I think you misunderstand this API GetClosestPointTo, it return the point in target line, but in most case it’s not the vertex of target line.

The red point is the return one.

If you want to get the nearest vertex, you just need loop all vertexes in the line, get distance between them and the clicked point, then get the shortest distance. The corresponding vertex is what you need. You can build a custom function for it.

Regards,

Ethan

Oh okay I see now. Thank you!

Hi Dan,

Thanks for your update, any question please let us know.

Regards,

Ethan