📄 gps.vb
字号:
Imports System.Threading
Imports System.Runtime.InteropServices
Namespace Microsoft.WindowsMobile.Samples.Location
Public Class Gps
Private deviceStateChangedHandle As EventWaitHandle
Private gpsEventThread As Thread = Nothing
Private gpsHandle As IntPtr = IntPtr.Zero
Private newLocationHandle As EventWaitHandle
Private stopHandle As EventWaitHandle
Private Event m_deviceStateChanged As DeviceStateChangedEventHandler
Public Custom Event DeviceStateChanged As DeviceStateChangedEventHandler
AddHandler(ByVal value As DeviceStateChangedEventHandler)
AddHandler m_deviceStateChanged, value
Me.CreateGpsEventThread()
End AddHandler
RemoveHandler(ByVal value As DeviceStateChangedEventHandler)
RemoveHandler m_deviceStateChanged, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal args As DeviceStateChangedEventArgs)
RaiseEvent m_deviceStateChanged(sender, args)
End RaiseEvent
End Event
Private Event m_locationChanged As LocationChangedEventHandler
Public Custom Event LocationChanged As LocationChangedEventHandler
AddHandler(ByVal value As LocationChangedEventHandler)
AddHandler m_locationChanged, value
Me.CreateGpsEventThread()
End AddHandler
RemoveHandler(ByVal value As LocationChangedEventHandler)
RemoveHandler m_locationChanged, value
End RemoveHandler
RaiseEvent(ByVal sender As Object, ByVal args As LocationChangedEventArgs)
RaiseEvent m_locationChanged(sender, args)
End RaiseEvent
End Event
Public Sub Close()
If (Me.gpsHandle <> IntPtr.Zero) Then
Gps.GPSCloseDevice(Me.gpsHandle)
Me.gpsHandle = IntPtr.Zero
End If
Me.stopHandle.Set()
SyncLock Me
Me.newLocationHandle.Close()
Me.deviceStateChangedHandle.Close()
Me.stopHandle.Close()
End SyncLock
End Sub
Private Sub CreateGpsEventThread()
If ((Me.gpsEventThread Is Nothing) AndAlso (Me.gpsHandle <> IntPtr.Zero)) Then
Me.gpsEventThread = New Thread(New ThreadStart(AddressOf Me.WaitForGpsEvents))
Me.gpsEventThread.Start()
End If
End Sub
Protected Overrides Sub Finalize()
Try
Me.Close()
Finally
MyBase.Finalize()
End Try
End Sub
Public Function GetDeviceState() As GpsDeviceState
Dim device As New GPS_DEVICE
device.dwVersion = 1
device.dwSize = Marshal.SizeOf(GetType(GPS_DEVICE))
If (Gps.GPSGetDeviceState(device) = 0) Then
Return New GpsDeviceState(device)
End If
Return Nothing
End Function
Public Function GetPosition() As GpsPosition
Return Me.GetPosition(TimeSpan.Zero)
End Function
Public Function GetPosition(ByVal maxAge As TimeSpan) As GpsPosition
If Me.Opened Then
Dim position As New GPS_POSITION
position.dwVersion = 1
position.dwSize = Marshal.SizeOf(GetType(GPS_POSITION))
If (Gps.GPSGetPosition(Me.gpsHandle, position, &H7A120, 0) = 0) Then
Dim gp As New GpsPosition(position)
If ((maxAge <> TimeSpan.Zero) AndAlso Not (gp.TimeValid AndAlso ((DateTime.Now - maxAge) <= gp.Time))) Then
Return Nothing
End If
Return gp
End If
End If
Return Nothing
End Function
<DllImport("gpsapi.dll")> _
Private Shared Function GPSCloseDevice(ByVal hGPSDevice As IntPtr) As Integer
End Function
<DllImport("gpsapi.dll")> _
Private Shared Function GPSGetDeviceState(ByRef pGPSDevice As GPS_DEVICE) As Integer
End Function
<DllImport("gpsapi.dll")> _
Private Shared Function GPSGetPosition(ByVal hGPSDevice As IntPtr, ByRef pGPSPosition As GPS_POSITION, ByVal dwMaximumAge As Integer, ByVal dwFlags As Integer) As Integer
End Function
<DllImport("gpsapi.dll")> _
Private Shared Function GPSOpenDevice(ByVal hNewLocationData As IntPtr, ByVal hDeviceStateChange As IntPtr, ByVal szDeviceName As String, ByVal dwFlags As Integer) As IntPtr
End Function
Public Sub Open()
If Not Me.Opened Then
Me.newLocationHandle = New EventWaitHandle(False, EventResetMode.AutoReset, Nothing)
Me.deviceStateChangedHandle = New EventWaitHandle(False, EventResetMode.AutoReset, Nothing)
Me.stopHandle = New EventWaitHandle(False, EventResetMode.AutoReset, Nothing)
Me.gpsHandle = Gps.GPSOpenDevice(Me.newLocationHandle.Handle, Me.deviceStateChangedHandle.Handle, Nothing, 0)
'If ((Not Me.locationChanged Is Nothing) OrElse (Not Me.deviceStateChanged Is Nothing)) Then
Me.CreateGpsEventThread()
'End If
End If
End Sub
Private Sub WaitForGpsEvents()
SyncLock Me
Dim listening As Boolean = True
Dim m_handles As WaitHandle() = New WaitHandle() {Me.stopHandle, Me.deviceStateChangedHandle, Me.newLocationHandle}
Do While listening
Select Case EventWaitHandle.WaitAny(m_handles)
Case 0
listening = False
Exit Select
Case 1
RaiseEvent DeviceStateChanged(Me, New DeviceStateChangedEventArgs(Me.GetDeviceState))
Exit Select
Case 2
RaiseEvent LocationChanged(Me, New LocationChangedEventArgs(Me.GetPosition))
Exit Select
End Select
Loop
Me.gpsEventThread = Nothing
End SyncLock
End Sub
Public ReadOnly Property Opened() As Boolean
Get
Return (Me.gpsHandle <> IntPtr.Zero)
End Get
End Property
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -