Welcome to MapForums!

Register, sign in, or use Facebook Connect above to join in and participate in the forum.

When you are signed in, this message, the ads in this row, and the red-underlined link ads all go away.

 Subscribe to receive our newsletter. Subscribe Unsubscribe

## Snap to a Road

This is a discussion on Snap to a Road within the MP2K Magazine Articles forums, part of the Map Forums category; Snap to a Road Wilfried Mestdagh's article shows how to take a lat/lon and find the nearest road relative to ...

1. ## Snap to a Road

Snap to a Road

Wilfried Mestdagh's article shows how to take a lat/lon and find the nearest road relative to that point. The technique, written entirely in Delphi, employs reverse geocoding and determines the lat/lon using a CalcXY routine.

Read the full article here --
http://www.mp2kmag.com/a130--snap.ca....mappoint.html

Post a comment by clicking Post Reply above.

2. Junior Member White Belt
Join Date
Mar 2006
Posts
4
Hello wilfried !

First of all, thanks so much for your effort on the article !
Im pretty new with Delphi, so i was wondering if u could give
me some help, in using your code. I don't understand what your
function expects with : (ErrorProc: TOnError)

This is what i have done sofar :

- Saved your code to mp.pas
- Made a new project and added mp.pas to the project.

procedure TForm1.Button1Click(Sender: TObject);
var
mpx : TMapPoint;
d1, d2 : Double;
st : TStrings;
sx : String;
i : Integer;
begin
d1 := StrToFloat(Edit1.Text);
d2 := StrToFloat(Edit2.Text);

mpx.Create(); //<-- What to put here ?
mpx.GetStreetAddr(d1,d2,st,sx,i);
mpx.Destroy;
end;

I have no clue what to put behind mpx.Create(?). Is the rest of
the code the good way ? Would u be do kind to help me (Or anyone
else). I would love to see this working.

Once again, thanks for the article !

Regards

3. Senior Member Black Belt
Join Date
Nov 2004
Location
Belgium
Posts
2,433
Hi Robin,

In the constructor you give a callback function so if the component generates an exception this callback function can log it. You can give also nil to it then nothing is called back.

I made a small testproject to demonstrate how to use it.

Create a new project, add a new unit and copy the code in it as you alrady have done. Call the form TMain, add a button to the form and call it TestBtn.

Then add the following code:

Code:
```procedure TMain.FormCreate&#40;Sender&#58; TObject&#41;;
begin
AllocConsole;
end;

// This is a callback function.
// Just in case mappoint produces an exception then we can display it
procedure TMain.MPError&#40;Sender&#58; TObject; E&#58; Exception&#41;;
begin
WriteLn&#40;E.ClassName + ' ' + E.Message&#41;;
end;

procedure TMain.TestBtnClick&#40;Sender&#58; TObject&#41;;
var
MP &#58; TMapPoint;
Lat, Lon&#58; Double;
Streets&#58; TStrings;
Street, Direction&#58; string;
Distance&#58; integer;
n&#58; integer;
begin
Streets &#58;= TStringList.Create;
MP &#58;= TMapPoint.Create&#40;MPError&#41;;
try
Lat &#58;= 51.2497;
Lon &#58;= 4.4888;
MP.GetStreetAddr&#40;Lat, Lon, Streets, Direction, Distance&#41;;

if Streets.Count = 0 then begin
WriteLn&#40;'No match found'&#41;;
Exit;
end;

// We can have more than 1 streetname in the stringlist.
// In most cases the longest name is the most detailed
// So we display only the most detailed.

Street &#58;= '';
for n &#58;= 0 to Streets.Count - 1 do
if Length&#40;Streets&#91;n&#93;&#41; > Length&#40;Street&#41; then
Street &#58;= Streets&#91;n&#93;;

WriteLn&#40;'Point is ' + IntToStr&#40;Distance&#41; + ' meter ' + Direction + ' from ' + Street&#41;;
finally
MP.Free;
Streets.Free;
end;```
Compile it and correct my typo's in the class There are some points where I typed >:= wich must be >= of course

4. Junior Member White Belt
Join Date
Mar 2006
Posts
4
Wilfried !

You made my day It's working flawless !

Thank you so much for your article and your help !

Regards

Robin

5. Junior Member White Belt
Join Date
Mar 2006
Posts
4
Hi Wilfried,

One more question

It seems the Country information is missing from the
Street information. Is this caused by MapPoint self, or
is there another way to show the country information
togheter with the found adres ?

(e.g. ..... Amsterdam - Netherlands)

Thanks again !

Regards,

Robin.

6. Senior Member Black Belt
Join Date
Nov 2004
Location
Belgium
Posts
2,433
Hi Robin,

StreetAddress has a Country property. Probably not includeded in the Value property. I think it is easy to add. Use Country propretyand add it to the value to see.

7. Junior Member White Belt
Join Date
Mar 2006
Posts
4
Hello Wilfried !

Oke, thanks ! Im going to check it out

Cheers

Robin

8. Junior Member White Belt
Join Date
Apr 2006
Posts
2
Hello Wilfried,
When reverse geocoding, if a point is not on a road, I keep increasing the altitude until it falls onto a road(I limit the max altitude to 15).
Your technique looks interesting. Is it more efficient or better than increasing the altitude ? Because if so, I can give it a try(I need to convert your code to C#).

Thanks

9. Junior Member White Belt
Join Date
May 2006
Posts
1

## Thanks!

Thanks! This article combined with Gilles Kohl's article definitely gave me a jump start on getting reverse geocoding going. I combined the two approaches and added some additional features (making it behave a little like a heuristic search algorithm and trying to make sure we spend our time as well as possible). One *duh* moment I had that can save anyone time that thinks as slow as I do: make sure your map is zoomed in to the right level of detail! MapPoint gives you a lot better results when you do .

10. Junior Member White Belt
Join Date
Jun 2006
Posts
1
Would anyone have any suggestions for snapping to a road using the map point web service? Thanks

Page 1 of 2 12 Last

#### Thread Information

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•