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:
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.
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.
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
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
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.