接下来的步骤是执行实际的提取信息,首先是纬度和经度。纬度和经度的形式存储在形如DDD MM SS.S的数据中,其中D代表小时(也称为
度),M代表分钟,S代表秒。坐标可以用缩写显示,如DD MM.M甚至DD。
句子中第四部分,3939.7,显示当前纬度 如小时和分钟
(39 39.7),除了数字排列在一起。前两个字(39)代表小时,其余的字(39.7)代表
分钟。经度的结构使用相同的方式,除了前三字符表示小时(10506.6)。单词五,七指示
半球,其中N表示北,西等W表示
半球被追加到的数字部分尾部,使一个完整的测量。本人已发现的NMEA 解析器非常容易。因为他们是事件驱动的。这是因为数据
不是以任何特定的顺序到达。事件驱动的类提供了解释器最大的灵活性和响应的应用程序。所以,我还得设计解释器来使用事件报告信息。第一事件,PositionReceived,当接受到当前纬度和经度会触发这个事件。
Public Class NmeaInterpreter
' Raised when the current location has changed
Public Event PositionReceived(ByVal latitude As String, ByVal longitude As String)
' Processes information from the GPS receiver
Public Function Parse(ByVal sentence As String) As Boolean
' Look at the first word to decide where to go next
Select Case GetWords(sentence)(0)
Case "$GPRMC"' A "Recommended Minimum" sentence was found!
Return ParseGPRMC(sentence)
Case Else
' Indicate that the sentence was not recognized
http://www.bao168.net
Return False
End Select
End Function
' Divides a sentence into individual words
Public Function GetWords(ByVal sentence As String) As String()
Return sentence.Split(","c)
End Function
' 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
这里有一点需要注意的是,当没有信息时一些GPS装置将报告空值。因此,在分离之前,最好先测试分析每个字的值。如果你需要
键入度数符号,按住Alt键,然后在数字键盘键入0176。