Answered by:
How to determine closet waypoints on the polyline when user clicks mouse on the route? (Apart from Haversine formula)
Question

If user clicks on routes then how can we determine closet waypoints to clicked point on the polyline(or route)? Haversine formula doesn't work in all scenario. So I need another option.
 Moved by Ricky_Brundritt Friday, March 9, 2012 2:12 PM (From:Bing Maps: Map Control and Web services Development)
Answers

I've created this functionality a number of times using the haversine formula. I understand your issue where you might click on one line but the closest waypoint may be in a different line segment. This is the tricky part. What you will want to do is first loop through coordinate pairs (p1, p2) then (p2, p2)... and calculate the distance from the clicked point to the line. (http://www.allegro.cc/forums/thread/589720 ). The algorithm in the provided link is not a spatial algorithm, however converting your coordinates to pixel coordinates will make this algorithm much better to use as it will give you distances in pixels which are much more relevant to the user. You then just need to find which coordinate pair has the shortest distance from the point.
Windows Live Developer MVP  http://rbrundritt.spaces.live.com  http://inknowledge.co.uk Marked as answer by Ricky_Brundritt Tuesday, January 18, 2011 1:04 PM
 Unmarked as answer by Ricky_Brundritt Tuesday, January 18, 2011 1:04 PM
 Proposed as answer by Ricky_Brundritt Tuesday, January 18, 2011 1:04 PM
 Marked as answer by Ricky_Brundritt Tuesday, February 1, 2011 12:30 PM
All replies


Consider one following scenarios which uses haversine formula where it fails:
 Create Route using start address: Irving, TX Destination Address: Durant, OK
 Drag the route using mouse & put first waypoint at Plano, TX. So route becomes Irving>Plano>Durant.
 Now drag route between Plano & Durant to Frisco, TX & put the waypoint at Frisco, TX.
 Now using Haversine formula you will get Irving & Plano as two closet point.
 If we insert new waypoint(i.e. Frisco) between Irving & Plano then It generates route like Irving > Frisco>Plano>Durant which is wrong. It should generate route like Irving > Plano>Frisco> Durant.

I've created this functionality a number of times using the haversine formula. I understand your issue where you might click on one line but the closest waypoint may be in a different line segment. This is the tricky part. What you will want to do is first loop through coordinate pairs (p1, p2) then (p2, p2)... and calculate the distance from the clicked point to the line. (http://www.allegro.cc/forums/thread/589720 ). The algorithm in the provided link is not a spatial algorithm, however converting your coordinates to pixel coordinates will make this algorithm much better to use as it will give you distances in pixels which are much more relevant to the user. You then just need to find which coordinate pair has the shortest distance from the point.
Windows Live Developer MVP  http://rbrundritt.spaces.live.com  http://inknowledge.co.uk Marked as answer by Ricky_Brundritt Tuesday, January 18, 2011 1:04 PM
 Unmarked as answer by Ricky_Brundritt Tuesday, January 18, 2011 1:04 PM
 Proposed as answer by Ricky_Brundritt Tuesday, January 18, 2011 1:04 PM
 Marked as answer by Ricky_Brundritt Tuesday, February 1, 2011 12:30 PM