# 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 Friday, March 9, 2012 2:12 PM (From:Bing Maps: Map Control and Web services Development)
Friday, January 14, 2011 11:38 PM

• 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
Tuesday, January 18, 2011 1:01 PM

### All replies

• Haversine formula doesn't work in all scenario.

The formula just calculates the distance between two points.  How does it "not work"?  Are you working across the dateline or at the poles?
Saturday, January 15, 2011 12:04 PM
• 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.
So what is the solution for this way of generating draggable route along with waypoint?
Monday, January 17, 2011 4:07 PM
• 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
Tuesday, January 18, 2011 1:01 PM