View Full Version : Finding the nearest road given a lat long.

09-11-2002, 10:37 AM
I want to find the nearest road from a lat/long the reason I want this is because of the inherant inaccuracy in GPS's sometimes my car appears off road (only slightly) but when I try to calculate a route based on these waypoints it goes mad and totaly gets it wrong.


John Meyer
09-11-2002, 08:16 PM
Check out this article: Reverse Geocoding, Pt. III by Walt Cygan


10-10-2002, 01:06 PM
Hi Scarr,

I encontered the same problem. Could you give me an update once you have a fix?

Thanks in advance!

12-22-2002, 10:09 PM
I have seen this issue pop up a lot, and I have an idea that I am going to try (though i am not quite there yet).. if you are tracking GPS then you know your compass heading. if you are moving along a road, and your gps is returning an invalid point, it must be either to the left or right of you. so if you take a line perpendicular to your compass heading, you can start looking along that line in both directions until you can get a valid location. if you check like 100 feet to either side of you, you should hit the road you want (and not the wrong one). just a thought, but it cuts down on the number of iterations required in the method where you spiral outward 360 degrees around your gps point.

hope this makes sense. post here if you get this working and i will do the same!

ps is there any way to get the car icon up on the map screen (like in streets and trips application during gps tracking)

04-04-2005, 03:33 PM
I’m new to MapPoint, and I want to drag a line from a point that shows distance and direction. I thought pointing a line in a give direction—say 286 degrees—would be an obvious feature similar to “Measure Distance.” Have I missed something or isn’t this doable?

04-05-2005, 04:22 AM

I just encountered a similar problem (with other purpose, but my solution may be working for you). What I do is running in a circle around the point where the car 'pretends' to be. I start with a radius of 5 meter and check the circumfence of the circle every 5 meter. If not found I increment the radius by 5 meter till a street is found. In your case I only should do a small maximum of 10..15 meter because you can end up with a wrong street if car is on a non digitized street.

This is the code (in Delphi). DistanceRes is the resollution (I set it to 5 meter), and MaxDistance is the radius where the routine should give up. FindAddr is the routine that ask Mappoint for streetresults (not listed here).

function TMapPoint.CalcInCircle(CenterLat, CenterLon: double; Streets: TStrings; out Distance, Angle: integer): boolean;
Lat, Lon: double;
Radius: double; // radius of the circle in degree
Circum: integer; // circumfence of the circle in Distance
Count: integer; // the amount of points to calculate on the circumfence
AngleInc: integer; // angle increments in degree per calculation
Distance := DistanceRes;
while Distance <= MaxDistance do begin
Radius &#58;= 1 / 60 / 1852 * Distance;
Circum &#58;= Trunc&#40;Distance * 2 * PI&#41;;
Count &#58;= Circum div DistanceRes;
AngleInc &#58;= 360 div Count;
if AngleInc <= 0 then begin
Result &#58;= False;
Angle &#58;= 0;
while Angle < 360 do begin
CalcXY&#40;CenterLat, CenterLon, Angle, Radius, Lat, Lon&#41;;
Result &#58;= FindAddr&#40;Lat, Lon, Streets&#41;;
if Result then
Inc&#40;Angle, AngleInc&#41;;
Inc&#40;Distance, DistanceRes&#41;;
Result &#58;= False;

And this is the actulally working horse:

procedure TMapPoint.CalcXY&#40;centerLat, centerLon, Angle, Radius&#58; double; out Lat, lon&#58; double&#41;;
a, b, c, r&#58; double;
&#123; B
/ |
/ |
/ |
/ |
c / | a
/ |
/ |
/ |
A b C

given A en c
a = c * sin&#40;A&#41;
b = c * cos&#40;A&#41;

A == 0 is direction East in this drawing
a = Latitude offset
b = Longitude offset
c = Radius of circle &#125;

r &#58;= DegToRad&#40;Angle&#41;;
c &#58;= Radius;
a &#58;= c * Sin&#40;r&#41;;
b &#58;= c * Cos&#40;r&#41; / Cos&#40;DegToRad&#40;centerLat&#41;&#41;; // correct longitude for circle
Lat &#58;= CenterLat + a;
Lon &#58;= CenterLon + b;

12-04-2008, 05:54 PM
Hi Wilfried

I have a similar need. Actually, I need to show the nearest POI to the actual position of the vehicle. This info should be updated every 2 minutes or so. My POI table has some 1000s records.

What I'm doing so far is, in a separate thread, to load the POI data in an in-memory dataset (delphi kbmMemTable) and calculate the distance between every POI and the given position (for loop). I store the results (actual position - POI infos - distance) in an array, and finally select the closest POI (min distance).

I know what I'm doing is REALLY awful :oops2: but since I didn't have much time when I did it, I didn't really have the choice (optimizing and taking time to search for the better solution when my boss is crying about not meeting an impossible deadline isn't the easiest thing to do :p

Anyway, I would like to optimize this now, so Could you give me a hand in adapting your code to my need?

Best Regards,