GPS应用程序编写:方向与速度提醒

来源:互联网 发布:nodejs部署到nginx 编辑:程序博客网 时间:2024/05/14 01:56

随着时间的推移,GPS设备分析你的位置,计算速度与方位。GPRMC语句包含了这些读数。速度总是以Knot为单位报告,
方位显示为一个方位角,水平测量的一种方法
顺时针测量从0到360,其中0代表北方,东部90表示,
一些数学应用到转换成英里每小时。下面的代码演示了全球定位系统的能力,
计算一辆车是否超过速度极限。


'*******************************************************
'** Listing 1-5. Extracting speed and bearing
http://www.bao168.net
'*******************************************************
Public Class NmeaInterpreter
 ' Raised when the current location has changed
 Public Event PositionReceived(ByVal latitude As String, ByVal longitude As String)
 Public Event DateTimeChanged(ByVal dateTime As DateTime)
 Public Event BearingReceived(ByVal bearing As Double)
 Public Event SpeedReceived(ByVal speed As Double)
 Public Event SpeedLimitReached()
 ' Processes information from the GPS receiver
 Public Function Parse(ByVal sentence As String) As Boolean
 ' Discard the sentence if its checksum does not match our calculated checksum
 If Not IsValid(sentence) Then Return False
 ' 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
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
 ' Do we have enough values to parse satellite-derived time?
 If Words(1) <> "" Then
' Yes. Extract hours, minutes, seconds and milliseconds
Dim UtcHours As Integer = CType(Words(1).Substring(0, 2), Integer)
Dim UtcMinutes As Integer = CType(Words(1).Substring(2, 2), Integer)
Dim UtcSeconds As Integer = CType(Words(1).Substring(4, 2), Integer)
Dim UtcMilliseconds As Integer
' Extract milliseconds if it is available
If Words(1).Length > 7 Then UtcMilliseconds = CType(Words(1).Substring(7), Integer)
' Now build a DateTime object with all values
Dim Today As DateTime = System.DateTime.Now.ToUniversalTime
Dim SatelliteTime As New System.DateTime(Today.Year, Today.Month, _
Today.Day, UtcHours, UtcMinutes, UtcSeconds, UtcMilliseconds)
' Notify of the new time, adjusted to the local time zone
RaiseEvent DateTimeChanged(SatelliteTime.ToLocalTime)
 End If
 ' Do we have enough information to extract the current speed?
 If Words(7) <> "" Then
' Yes. Convert it into MPH
Dim Speed As Double = CType(Words(7), Double) * 1.150779
' If we're over 55MPH then trigger a speed alarm!
If Speed > 55 Then RaiseEvent SpeedLimitReached()
' Notify of the new speed
RaiseEvent SpeedReceived(Speed)
 End If
 ' Do we have enough information to extract bearing?
 If Words(8) <> "" Then
' Indicate that the sentence was recognized
Dim Bearing As Double = CType(Words(8), Double)
RaiseEvent BearingReceived(Bearing)
 End If
 ' Indicate that the sentence was recognized
 Return True
 End Function
 ' Returns True if a sentence's checksum matches the calculated checksum
 Public Function IsValid(ByVal sentence As String) As Boolean
 ' Compare the characters after the asterisk to the calculation
 Return sentence.Substring(sentence.IndexOf("*") + 1) = GetChecksum(sentence)
 End Function
 ' Calculates the checksum for a sentence
 Public Function GetChecksum(ByVal sentence As String) As String
 ' Loop through all chars to get a checksum
 Dim Character As Char
 Dim Checksum As Integer
 For Each Character In sentence
Select Case Character
Case "$"c
 ' Ignore the dollar sign
Case "*"c
 ' Stop processing before the asterisk
 Exit For
Case Else
 ' Is this the first value for the checksum?
 If Checksum = 0 Then
 ' Yes. Set the checksum to the value
 Checksum = Convert.ToByte(Character)
 Else
 ' No. XOR the checksum with this character's value
 Checksum = Checksum Xor Convert.ToByte(Character)
 End If
End Select
 Next
 ' Return the checksum formatted as a two-character hexadecimal
 Return Checksum.ToString("X2")
 End Function
End Class

 

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 初三娃不听话该怎么办 小孩爱动不听话怎么办 宝宝吃饭讨神怎么办 孩子速度太慢怎么办 一年级做作业慢怎么办 小学三年级数学差怎么办 拼音基础太差怎么办 小孩学拼音差怎么办? 孩子字词基础差怎么办 孩子的语文不好怎么办 数学一直学不好怎么办 小孩数学成绩差怎么办 理科生语文不好怎么办 小学阅读题不好怎么办 如果孩子考不好怎么办 6岁不认识数字怎么办 数学一点都不会怎么办 初一数学太差怎么办 三年级孩子数学差怎么办 三年级孩子数学很差怎么办 初中学习不好高中怎么办 四年级孩子数学不好怎么办 孩子学习不开窍怎么办 孩子学习太笨怎么办 老师是个小人怎么办 孩子写字太差怎么办 孩子写字下手重怎么办 孩子一年级数学不好怎么办 孩子数学理解能力差怎么办 智商情商都低怎么办 一年级孩子数学很差怎么办 一年级孩子数学差怎么办 一年级数学学不好怎么办 孩子成绩差该怎么办 小学生数学太差怎么办 小学数学基础差怎么办 孩子一年级学习不好怎么办 快两岁的宝宝老尿裤怎么办 戒母乳宝宝哭闹怎么办 三周岁不肯说话怎么办 两岁宝宝打人怎么办