Share |

Display your current GPS location using PHP

Download 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

Please note that this will only work when the PHP parser is running on a Windows system. This sample will not work on linux / unix based environments, because they simply do not support ActiveX controls.

First you must have a PHP Script editor and the Eye4Software GPS Component installed on your computer. If you do not have a dedicated PHP editor, notepad will do. You can download the Eye4Software GPS Component here.

Creating the project

To create your PHP Script, you can just use notepad or any other text editor. Create a file with a ".php" extension.

Declare and create the object(s)

On top of your script, add the following code to create an instance of the objects:

$objGps       = new COM ( "Eye4Software.Gps" );
$objConstants = new COM ( "Eye4Software.GpsConstants" );

The source code

Below you can find the sourcecode from the PHP demo as shipped with the project. This script opens the serial port the GPS is connected to, waits until there is a valid GPS position, and writes the received information as HTML to the client.

<html>
  <head>
    <title>Eye4Software GPS Toolkit - PHP Sample</title>
  </head>
  <body>
    <hr>
    <h2>Eye4Software GPS Toolkit - PHP Sample</h2>
    <hr>
    <br>
    <?php
    // Create Gps object
    $objGps       = new COM ( "Eye4Software.Gps" );
    $objConstants = new COM ( "Eye4Software.GpsConstants" );
	
    // Serial Port Settings
    $objGps->DeviceSerialPort	= 3;
    $objGps->DeviceBaudrate		= 4800;
	
    // Optional: Only decode specific NMEA data
    // $objGps->Filter = objConstants->GPS_NMEA_FILTER_VTG | objConstants->GPS_NMEA_FILTER_GGA;

    // Optional: Logfile (use for troubleshouting)
    // $objGps->LogFile = "C:\\GpsLog.txt";
	
    // Optional: Enter you registration key here
    //$objGps->RegistrationCode = "XXXXXXXXXXXXXXX";
	
    // Open Serial Port
    $objGps->Open ();

    // Check Result
    if ( $objGps->LastError != 0 )
    {
      die ( "Failed to open serial port: " . $objGps->LastErrorDescription );
    }
	
    // Wait until GPS fix has received (you can add a timeout here in case there is no reception)
    while ( $objGps->gpsQuality == 0 )
    {
      sleep ( 1 );
    }
	
    // Display information after we received a fix
    echo "<table cellspacing=\"20px\" >\n";
    echo "<tr><td>Time:</td><td>"      . $objGps->gpsTimeString      . "</td></tr>\n";
    echo "<tr><td>Latitude:</td><td>"  . $objGps->gpsLatitudeString  . "</td></tr>\n";
    echo "<tr><td>Longitude:</td><td>" . $objGps->gpsLongitudeString . "</td></tr>\n";
    echo "<tr><td>Altitude:</td><td>"  . $objGps->gpsAltitude        . "</td></tr>\n";
    echo "<tr><td>Sats:</td><td>"      . $objGps->gpsSatellites      . "</td></tr>\n";
    echo "<tr><td>Quality:</td><td>"   . $objGps->gpsQuality         . "</td></tr>\n";
    echo "<tr><td>Speed:</td><td>"     . $objGps->gpsSpeed           . "</td></tr>\n";
    echo "<tr><td>Course:</td><td>"    . $objGps->gpsCourse          . "</td></tr>\n";
    echo "</table>\n";
	
    // Close Serial Port
    $objGps->Close ();
    ?>
  <br>
  <br>
  <hr>
  </body>
</html>

The output

This code will give the following result in your browser (assuming you have a valid GPS position):

PHP GPS Demo