[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
Reading Latitude and Longitude

Reading Latitude and Longitude

Latitude and longitude are stored in the form “DDD°MM’SS.S,” where D represents hours (also called “degrees”), M represents minutes and S represents seconds. Coordinates can be displayed in shorthand, such as “DD°MM.M’” or even “DD°.” The fourth word in the sentence, “3939.7,” shows the current latitude as hours and minutes (39°39.7’), except the numbers are squished together. The first two characters (39) represent hours and the remainder of the word (39.7) represents minutes. Longitude is structured the same way, except that the first three characters represent hours (105°06.6’). Words five and seven indicate the “hemisphere,” where “N” means “North,” “W” means “West” etc. The hemisphere is appended to the end of the numeric portion to make a complete measurement.

I’ve found that NMEA interpreters are much easier to work with they are event-driven. This is because data arrives in no particular order. An event-driven class gives the interpreter the most flexibility and responsiveness to an application. So, I’ll design the interpreter to report information using events. The first event, PositionReceived, will be raised whenever the current latitude and longitude are received.

[VB.NET]
'*******************************************************
'**  Extracting information from a sentence
'*******************************************************
Public Class NmeaInterpreter
  ' Raised when the current location has changed
  Public Event PositionReceived(ByVal latitude As String, _
           ByVal longitude As String)
  ' Interprets a $GPRMC message
  Public Function ParseGPRMC(ByVal sentence As String) As Boolean
    ' Divide the sentence into words
    Dim Words() As String = GetWords(sentence)
    ' Do we have enough values to describe our location?
    If Words(3) <> "" And Words(4) <> "" And Words(5) <> "" And _
       Words(6) <> "" Then
      ' Yes. Extract latitude and longitude
      Dim Latitude As String = Words(3).Substring(0, 2) & "°" ' Append hours
      Latitude = Latitude & Words(3).Substring(2) & """"      ' Append minutes
      Latitude = Latitude & Words(4)     ' Append the hemisphere
      Dim Longitude As String = Words(5).Substring(0, 3) & "°" ' Append hours
      Longitude = Longitude & Words(5).Substring(3) & """"     ' Append minutes
      Longitude = Longitude & Words(6)     ' Append the hemisphere
      ' Notify the calling application of the change
      RaiseEvent PositionReceived(Latitude, Longitude)
    End If
    ' Indicate that the sentence was recognized
    Return True
  End Function
End Class


If you need to type the degree symbol (°), hold down the Alt key and type “0176” on the numeric keypad.

Written By Jon Person (http://www.gpsdotnet.com)


last edited (December 21, 2004) by jperson, Number of views: 25278, Current Rev: 5 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Site Management by Lars Hagelin at Kontantkort.se.