Share |

Building GPS applications using Borland Delphi

Dowload Eye4Software GPS Toolkit free trial Download the Eye4Software GPS Toolkit fully functional 30 day trial version for free
Browse through the Eye4Software GPS Toolkit for Windows manual Browse through the Eye4Software GPS Toolkit manual

Introduction

The Eye4Software GPS toolkit allows software developers to add GPS functionality to their own programs without the need to have any knowledge on serial communications and GPS protocols like RS-232 and NMEA0183.

The product can be used in many programming environments, such as Visual Basic, Visual C++, Visual Studio.Net, Borland C++ Builder, Borland Delphi and VBA, but also web oriented applications such as ASP, ASP.NET and PHP, and all other programming environments that support ActiveX.

Prerequisites

First you must have Borland Delphi and the Eye4Software GPS Component installed on your computer. We will use Borland Delphi 7.0 in this document, but other versions from 6.0 and up can also be used. You can download the Eye4Software GPS Component here.

Creating the project

Start the Borland Delphi IDE, and create a new application by selecting "Application" from the "File" => "New" menu. A blank project with a single form is now created.

Declare and create the object(s)

First, we have to include some units to be able to use ActiveX controls in our Delphi project. We need to include the following units: OleServer, ComObj, ActiveX. You can modify the code like this:

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, OleServer, ComObj, ActiveX;

Now we are ready to declare and the GPS Toolkit objects. You can add the defintion in the "private" declarations section at the top of the source file:

private:

  objGps         : Variant;
  objConstants   : Variant;

create the GPS Toolkit objects in the form, double click on the form to create and edit the FormCreate handler:

  objGps            := CreateOleObject ( 'Eye4Software.Gps' );
  objGpsConstants   := CreateOleObject ( 'Eye4Software.GpsConstants' );

After these steps, you can start with programming the rest of the GPS code.

The source code

Below you can find the sourcecode from the Borland Delphi demo as shipped with the GPS Toolkit.

unit Demo;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, OleServer, ComObj, ActiveX;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    ComboBoxDevice: TComboBox;
    ComboBoxSpeed: TComboBox;
    ButtonStart: TButton;
    ButtonStop: TButton;
    GroupBox2: TGroupBox;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    EditLatitude: TEdit;
    EditLongitude: TEdit;
    EditSpeed: TEdit;
    EditCourse: TEdit;
    EditAltitude: TEdit;
    EditTime: TEdit;
    EditFix: TEdit;
    EditSats: TEdit;
    GroupBox3: TGroupBox;
    LabelStatus: TLabel;
    Timer1: TTimer;
    Label11: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure ButtonStartClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure ButtonStopClick(Sender: TObject);
  private

    objGps        : Variant;

  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{//////////////////////////////////////////////////////////////////////////////}

procedure TForm1.FormCreate(Sender: TObject);
var i : Integer;
begin

  objGps        := CreateOleObject ( 'Eye4Software.Gps' );

  ComboBoxDevice.Items.Add ( 'Garmin USB' );

  for i := 1 to 16 do
  begin
    ComboBoxDevice.Items.Add('COM' + IntToStr ( i ) );
  end;

  ComboBoxDevice.ItemIndex := 1;

  ComboBoxSpeed.Items.Add ( '1200' );
  ComboBoxSpeed.Items.Add ( '2400' );
  ComboBoxSpeed.Items.Add ( '4800' );
  ComboBoxSpeed.Items.Add ( '9600' );
  ComboBoxSpeed.Items.Add ( '19200' );
  ComboBoxSpeed.Items.Add ( '38400' );
  ComboBoxSpeed.Items.Add ( '57600' );
  ComboBoxSpeed.Items.Add ( '115200' );

  ComboBoxSpeed.ItemIndex := 2;

end;

{//////////////////////////////////////////////////////////////////////////////}

procedure TForm1.ButtonStartClick(Sender: TObject);
begin
  objGps.DeviceSerialPort := ComboBoxDevice.ItemIndex;
  objGps.DeviceBaudrate   := StrToInt ( ComboBoxSpeed.Text );

  objGps.Open;

  LabelStatus.Caption := objGps.LastErrorDescription;

  if ( objGps.LastError = 0 ) then begin
    ButtonStart.Enabled := False;
    ButtonStop.Enabled := True;
    Timer1.Enabled := True;
  end;
end;

{//////////////////////////////////////////////////////////////////////////////}

procedure TForm1.ButtonStopClick(Sender: TObject);
begin
  objGps.Close;

  LabelStatus.Caption := objGps.LastErrorDescription;

  if ( objGps.LastError = 0 ) then begin
    ButtonStart.Enabled := True;
    ButtonStop.Enabled := False;
    Timer1.Enabled := False;
  end;
end;

{//////////////////////////////////////////////////////////////////////////////}

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  EditLatitude.Text :=  objGps.gpsLatitudeString;
  EditLongitude.Text := objGps.gpsLongitudeString;
  EditSpeed.Text :=  FloatToStr ( objGps.gpsSpeed );
  EditCourse.Text :=  FloatToStr ( objGps.gpsCourse );
  EditAltitude.Text := FloatToStr ( objGps.gpsAltitude );
  EditFix.Text := IntToStr ( objGps.gpsQuality );
  EditSats.Text := IntToStr ( objGps.gpsSatellites );
  EditTime.Text := objGps.gpsTimeString;
end;

{//////////////////////////////////////////////////////////////////////////////}

end.