# GPS application - Part II

#### Erel

##### B4X founder
Staff member
Longtime User
This is the second part of the GPS tutorial.
The first part could be found here: http://www.b4x.com/forum/showthread.php?t=1093

In this part we will discuss how to convert the coordinates between Lat/Lon and UTM formats and between different datums.

The coordinates which are received from GPS devices are in Lat/Lon format.
While this format is useful for aviation and large scale maps, it is less useful for smaller scale maps.
The complete world is divided into 60 zones (each zone covers 6 degrees longitude).
In each zone a UTM grid is defined.
As long as the interest area is in one zone it is usually easier to work with UTM.
Most topographic maps only have a UTM grid.
One of the benefits of UTM is that the coordinates are measured in meters unlike Lat / Lon coordinates which are measured in degrees.
So calculations of distance and bearing with UTM coordinates are much easier (assuming that both coordinates are in the same zone).

To make things harder, there are many datums available and each datum represents a slightly different measurement of the earth surface.
Most GPS devices and most modern maps use the WGS84 datum.

Using the Converter (found in the GPS library), we can make the required conversions easily.

We will now add UTM coordinates to our GPS program.
First choose Tools - Add Object - Converter, and name it Converter.
The most useful method of Converter (with the longest name) is:
WGS84LatLonToUTM. This method receives Lat/Lon coordinates (in their decimal format) in WGS84 datum and returns UTM coordinates in the same datum.
The return value is a structure (or an array) with 4 fields:
XZone, X, YZone (ASCII value), Y.
We will declare such a structure in Sub Globals, and name it UTM:
B4X:
``````Sub Globals
port = 8
baudrate = 9600
counter = 0
Dim Type (Xzone,X,Yzone,Y) UTM 'The order is important!
End Sub``````
Inside the GPSDecoded event we will use this structure to get the four values.
Remember that when you need to reference a complete array or structure you should add '()' after its name.
B4X:
``````If GPS1.DecimalLatitude <> 0 OR GPS1.DecimalLongitude<>0 Then
UTM() = converter.WGS84LatLonToUTM(GPS1.DecimalLatitude,GPS1.DecimalLongitude)
lstData.Add("UTM Zone: " & UTM.Xzone & Chr(UTM.Yzone))
lstData.Add("UTM: " & Round(UTM.X) & " " & Round(UTM.Y))
End If``````
As you see in the code, we first check that the Lat/Lon coordinates are valid (if both equal 0 then there is probably no satellites reception).
The Yzone is an ASCII value, so we use Chr to show the relevant letter.

* The Yzone isn't displayed in this image.

Now for the other methods of Converter:
ChangeDatum - Converts Lat/Lon coordinates between two datums.
LatLonToUTM / UTMToLatLon - Converts between the two formats using a specific datum.
WGS84UTMToLatLon - Converts between the two formats using the WGS84 datum.
See the GPS help for more information and examples of using these methods.

#### Attachments

• GPS.zip
9.9 KB · Views: 694

#### Put Claude

##### Active Member
Longtime User
hi Erel,

A time ago I ask the question what will I use, UTM or LAT-LON... the replys I get are very good: if you do not CROSS the UTM border, use UTM... But that is something you never now... do you cross the UTM border?
So I decide to use for all time the LAT_LON data, and with much more calculation time , I am definatly happy with it, espacialy in B4PPC with his automatic data convertions...
If you cross the border in UTM, and you must do calculating time for that fact, I think you lose as much time as in calculating with LAT-LON data.
The results of my calculations are also meters at the end, so I get the same results, and I can ashure you: it works very good... So, thinks well before you decide...

Put Claude Belgium

Last edited:

#### gingerpuss

##### New Member
Hi all,

I would appreciate some advice.... I compiled the sample GPS app - making only one change - setting the Port to '1' as I know this is the one my GPS uses.

I can run the app just fine.. and the device's GPS-ON LED lights up after selecting 'Connect'.. then it says "GPS is connected".

Unfortunately that seems to be it... it never provides any GPS info in the main list.... i.e. the "GPS1_GPSDecoded" routine never seems to execute. One of my other GPS apps will happily locate satellites and display a Lat/Lon.

I have included the GPS.DLL file with the install as requested by the compiler.

Any ideas why the "GPS1_GPSDecoded" sub is not triggering??

Many thanks
Mark

#### derez

##### Expert
Longtime User
Mark
A guess - your device is HTC ? If so - do what Erel recommends, HTC does not work with GPS.dll.

#### Georg

##### Member
Longtime User
Hello

HTC with WM6.1 works with GPS.dll but HTC with 6.0 doesn't

#### Erel

##### B4X founder
Staff member
Longtime User
Small correction:
The problem is not with GPS.dll. The GPS library is a simple parsing library which receives strings and outputs the GPS data.
The problem is with the virtual serial port which the GPS intermediate driver should feed with data from the GPS device. So on newer devices (since WM5) it is better to connect directly to the GPS driver and fetch the data from it.

#### gingerpuss

##### New Member
Thanks for the advice guys.. I will try the updated library as you suggest. At the moment it is a WM6 pro device (Asus P526) but I am also going to be using work PDAs with WM5 installed for teaching.

I will let you know how I get on

Many thanks
Mark