Share |

Convert GPS coordinates from one map grid to another using Visual Basic

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

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

Creating the project

Start the Visual Basic IDE, and select the "New Project" option from the "File" menu. The "New Project" dialog now appears, select the "Standard EXE" option to generate a Windows GUI application.

Performing map projection transformations using Visual Basic

Add a reference to the ActiveX component

In order to declare and create the objects from your Visual Basic application, you need to add a reference to the ActiveX object, by choosing the "References" option from the "Project" menu. A list of components installed on the system is displayed. Just check the checkbox in front of the "Eye4Software GPS Toolkit 3.0" and click "OK".

Performing map grid transformations using Visual Basic

Declare and create the object(s)

After adding the reference to the control, you can declare the objects like this:

Private objProjection As GpsProjection
Private objGridDst As GpsGridParameters
Private objGridSrc As GpsGridParameters

The objects can be created in your code by double clicking on the Form you created, this will open the Form_Load handler. Paste the following code in this function:

Set objProjection = CreateObject ("Eye4Software.GpsProjection")
Set objGridSrc    = CreateObject ("Eye4Software.GpsGridParameters")
Set objGridDst    = CreateObject ("Eye4Software.GpsGridParameters")

The source code

The source code below demonstrates how to build a very simple map grid transformation program using VB. The software reads the source latitude and longitude ( in WGS84 ) from two edit boxes, performs a map grid projection on the Irish National Grid, and displays the result in Easting and Northing. To modify the code to use another map grid, please have a look at the map grids list to find the ID for the required grid.

Option Explicit

'/////////////////////////////////////////////////////////////////////////

Private objProjection As GpsProjection
Private objGridSrc As GpsGridParameters
Private objGridDst As GpsGridParameters

'/////////////////////////////////////////////////////////////////////////

Private Sub CommandTranslate_Click()
    ' Set Source Grid ( WGS84, Geographic Latitude and Longitude  )
    ' The ID for WGS84 is 4326, see 'http://www.eye4software.com/resources/datums' for a list of supported datums 
    ' To convert from another datum or grid, just change the code below (EPSG code)
    objGridSrc.LoadFromId(4326)

    ' Set Destination Grid ( Irish Grid )
    ' The ID for the Irish Grid is 29901, see 'http://www.eye4software.com/resources/grids' for a list of grids 
    ' To convert to another grid, just change the code below (EPSG code)
    objGridDst.LoadFromId(29901)
    
    ' Set Source coordinates
    objProjection.Latitude  = CDbl(TextLatitude.Text)
    objProjection.Longitude = CDbl(TextLongitude.Text)

    ' Perform the datum transformation
    objProjection.TransformGrid objGridSrc, objGridDst

    ' Display the result
    TextNorthing.Text = objProjection.Northing
    TextEasting.Text = objProjection.Easting
    
End Sub

'/////////////////////////////////////////////////////////////////////////

Private Sub Form_Load()
    ' Create the objects
    Set objProjection = CreateObject("Eye4Software.GpsProjection")
    Set objGridSrc = CreateObject("Eye4Software.GpsGridParameters")
    Set objGridDst = CreateObject("Eye4Software.GpsGridParameters")
      
    ' Set default values for input boxes
    TextLatitude.Text = "54.66"
    TextLongitude.Text = "-6.81"
End Sub

'/////////////////////////////////////////////////////////////////////////