Thread: Snap to a Road
View Single Post

  #3 (permalink)  
Old 03-06-2006
Wilfried Wilfried is offline
Senior Member
Black Belt
 
Join Date: Nov 2004
Posts: 2,055
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(Sender: TObject);
begin
   AllocConsole;
end;

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

procedure TMain.TestBtnClick(Sender: TObject);
var
   MP : TMapPoint;
   Lat, Lon: Double;
   Streets: TStrings;
   Street, Direction: string;
   Distance: integer;
   n: integer;
begin
   Streets := TStringList.Create;
   MP := TMapPoint.Create(MPError);
   try
      Lat := 51.2497;
      Lon := 4.4888;
      MP.GetStreetAddr(Lat, Lon, Streets, Direction, Distance);

      if Streets.Count = 0 then begin
         WriteLn('No match found');
         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 := '';
      for n := 0 to Streets.Count - 1 do
         if Length(Streets[n]) > Length(Street) then
            Street := Streets[n];

      WriteLn('Point is ' + IntToStr(Distance) + ' meter ' + Direction + ' from ' + Street);
   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
Reply With Quote