Basic4android development tutorial (9) use GPS

Source: Internet
Author: User

GPS plays an important role in many Android devices.
Fortunately, it is very convenient to make it work.
In this tutorial, we will complete a program to display the current position and satellite status.

The GPS function is encapsulated in the GPS library.
Therefore, we must first add this library to the reference:

There is a type related to it. The most important thing is GPS.
GPS manages connections and events. The second is location. location is a structure that stores the modified values of longitude and latitude coordinates, and other information, such as the direction and height.
Sometimes there is not all information available (for example, the signal is weak ).

Location also includes other functions, such as calculating the distance and orientation from another location, and also the method for converting the coordinate string format.
Generally, you need to process the location object passed in through the locationchanged event. Of course, you can also initialize such an object by yourself (this is useful when calculating the distance and orientation of different locations ).

The last type is gpssatellite. It is also a structure that stores various information about the currently recognized satellite. it is transmitted to you through the GP sStatus event.

Back to GPS.
The GPS object should be defined as the process_global object. Otherwise, a new instance will be created when the activity is rebuilt.

The first step is to initialize the object. Like other initialization methods, it requires an eventname parameter, which is the prefix for completing the GPS object event.

The complete code is as follows:

Code:
Sub Process_Globals
    Dim GPS1 As GPS
End Sub

Sub Globals
    Dim lblLon As Label
    Dim lblLat As Label
    Dim lblSpeed As Label
    Dim lblSatellites As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
      GPS1.Initialize("GPS")
     End If
   Activity.LoadLayout("1")
End?Sub

Sub Activity_Resume
    If GPS1.GPSEnabled = False Then
     ToastMessageShow("Please enable the GPS device.", True)
     StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
      GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

Sub Activity_Pause (UserClosed As Boolean)
   GPS1.Stop
End Sub

Sub GPS_LocationChanged (Location1 As Location)
    lblLat.Text = "Lat = " & Location1.ConvertToMinutes(Location1.Latitude)
    lblLon.Text = "Lon = " & Location1.ConvertToMinutes(Location1.Longitude)
    lblSpeed.Text?= "Speed = " & Location1.Speed
End Sub

Sub GPS_UserEnabled(Enabled As Boolean)
    ToastMessageShow("GPS device enabled = " & Enabled, True)
End?Sub

Sub GPS_GpsStatus (Satellites As List)
     lblSatellites.Text = "Satellites:" & CRLF
     For i = 0 To Satellites.Size - 1
     Dim Satellite As GPSSatellite
     Satellite = Satellites.Get(i)
     lblSatellites.Text = lblSatellites.Text & CRLF & Satellite.Prn & _
           " " & Satellite.Snr & " " & Satellite.UsedInFix & " " & Satellite.Azimuth _ 
               & " " & Satellite.Elevation
 Next
End Sub

The next step is to notify GPS to start listening for data. GPS will consume a lot of power. therefore, we recommend that you disable it when you do not use it. we recommend that you start listening in activity_resume and stop listening in activity_pause.

In many cases, the user will disable GPS. Due to privacy concerns, Android OS cannot enable GPS in the program. The best way is to ask the user to enable the gps device.

The following code displays a message if GPS is not enabled, and opens the GPS Control Panel so that you can directly select the GPS option:

Code:
Sub Activity_Resume
   If GPS1.GPSEnabled = False Then
     ToastMessageShow("Please enable the GPS device.", True)
     StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    Else
      GPS1.Start(0, 0) 'Listen to GPS with no filters.
    End If
End Sub

Once GPS is enabled, it starts to listen for data. the start method uses two values: Minimum Time (millisecond) and minimum distance, which are used to determine the minimum interval of the trigger time. When any condition is reached, the event is triggered. this can be used to save power.
Here, we set it to 0 to accept all fixed events.

GPS triggers three events:
-Gps_locationchanged (location1 as location)
This is the main event. location1 includes the new corrected value.

-Gps_gpsstatus (satellites as List)
This event displays information about the currently found satellite. note that not all satellites are used to calculate the final correction value. therefore, there are several satellites in this list, and it is still possible that the received data is not enough to calculate the correction value.

-Gps_userenabled (enabled as Boolean)
T once the user changes the status of the GPS device, the event will be triggered. At the same time, the event will be triggered immediately after the start is called.

.
The attachment is here gps.zip (6.3 kb, 462 views)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.