Android GPS positioning test (with renderings and examples)

Source: Internet
Author: User

I have developed GPS-related embedded software for several years, So when talking about a program to test the GPS positioning module, the first reaction is to read the data from the GPS module through the serial port, and then parse the NMEA format data of GPS.

Today, due to work needs, I took out a previously written GPS test program and modified it again. I wrote this program for 11 years. It was not long before I learned Android development. It was an experimental work. Work now needs to be done again. At the same time, I found that I do not know much about the GPS service of android, so today I read some documents about the GPS service and recorded the relevant knowledge points.

I have been using GPS-related embedded software for several years, so when talking about how to test the GPS positioning module program, the first reaction is to read the data from the GPS module through the serial port, then parse the NMEA format data of GPS. NMEA is a standard data format, which is not only applied on GPS, but also used by some other industrial communications. Parse the relevant data and display it. Then, it completes a basic GPS positioning test function.

After checking this information, we found that GPS-related positioning services are available on Android, and NMEA data analysis is not required. Android has encapsulated related services. You need to call APIs. I don't know whether it should be nice or not. (Android also provides the NMEA reading interface, which will be discussed below)

1. Android positioning service
Next, let's take a look at the Android positioning service support:

The Android positioning service is located under the location, and relevant descriptions are provided above, which will not be detailed here. Something to talk about
Yes: GpsStatus. NmeaListener the official statement is that NMEA data can be read, but I have tested it here and found that NMEA data is not read. I have consulted some materials, saying that google does not implement data feedback at the underlying layer. If you have time, check the source code.

2. LocationManager Positioning

Copy codeThe Code is as follows:
// Obtain the location service
LocationManager locationManager = (LocationManager) this. getSystemService (Context. LOCATION_SERVICE );
// Determine whether the GPS module is enabled
If (locationManager. isProviderEnabled (android. location. LocationManager. GPS_PROVIDER ))
{
// When the GPS module is enabled, you can locate the operation.
}

 

Copy codeThe Code is as follows:
// Locate by GPS
String LocateType = locationManager. GPS_PROVIDER;
Location location = locationManager. getLastKnownLocation (LocateType );
// Set the listener and set the automatic update interval. Here, the setting is 1000 ms and the moving distance is 0 meters.
LocationManager. requestLocationUpdates (provider, 1000, 0, locationListener );
// Set the status listening callback function. StatusListener is the callback function of the listener.
LocationManager. addGpsStatusListener (statusListener );
// The network Location settings are also provided.
String LocateType = locationManager. NETWORK_PROVIDER;
Location location = locationManager. getLastKnownLocation (LocateType );


3. GpsStatus listener
The initialization settings of the positioning service are given above, but we all know that GPS satellites broadcast data on a regular basis, that is, they receive GPS data on a regular basis. We cannot apply for data with satellites, but can only passively receive data. (China's Beidou 2 can send satellite messages to satellites) So we need to register a listener to process the data returned by the satellites.

Copy codeThe Code is as follows:
Private final GpsStatus. Listener statusListener = new GpsStatus. Listener ()
{
Public void onGpsStatusChanged (int event)
{
// Callback when the GPS status changes to get the current status
GpsStatus status = locationManager. getGpsStatus (null );
// Self-compiled method to obtain satellite status data
GetGPSStatus (event, status );
}
};


4. Retrieve the searched Satellite

Copy codeThe Code is as follows:
Private void GetGPSStatus (int event, GpsStatus status)
{
Log. d (TAG, "enter the updateGpsStatus ()");
If (status = null)
{
}
Else if (event = GpsStatus. GPS_EVENT_SATELLITE_STATUS)
{
// Obtain the maximum number of satellites (this is only a preset value)
Int maxSatellites = status. getMaxSatellites ();
Iterator <GpsSatellite> it = status. getSatellites (). iterator ();
NumSatelliteList. clear ();
// Record the actual number of satellites
Int count = 0;
While (it. hasNext () & count <= maxSatellites)
{
// Save the satellite data to a queue for refreshing the interface
GpsSatellite s = it. next ();
NumSatelliteList. add (s );
Count ++;

Log. d (TAG, "updateGpsStatus ---- count =" + count );
}
MSatelliteNum = numSatelliteList. size ();
}
Else if (event = GpsStatus. GPS_EVENT_STARTED)
{
// Locate startup
}
Else if (event = GpsStatus. GPS_EVENT_STOPPED)
{
// Positioning ends
}
}


The above figure shows the number of satellites searched from the status value, mainly through status. getSatellites. The obtained GpsSatellite object,
Save it to a queue to refresh the interface later. The above figure shows the GPS status listener. In addition to the GPS status, we also need to listen to a service,
It is: LocationListener, which locates the listener and listens for changes in the location. This is very important for positioning services.

5. LocationListener listener

Copy codeThe Code is as follows:
Private final LocationListener locationListener = new LocationListener ()
{
Public void onLocationChanged (Location location)
{
// This function is triggered when the coordinates change. If the Provider transmits the same coordinates, it will not be triggered.
UpdateToNewLocation (location );
Log. d (TAG, "LocationListener onLocationChanged ");
}
Public void onProviderDisabled (String provider)
{
// This function is triggered when the Provider is disable, for example, GPS is disabled.
Log. d (TAG, "LocationListener onProviderDisabled ");
}
Public void onProviderEnabled (String provider)
{
// This function is triggered when the Provider is enabled. For example, if GPS is enabled
Log. d (TAG, "LocationListener onProviderEnabled ");
}
Public void onStatusChanged (String provider, int status, Bundle extras)
{
Log. d (TAG, "LocationListener onStatusChanged ");
// Trigger this function when the Provider's transition state is available, temporarily unavailable, and no service is directly switched
If (status = LocationProvider. OUT_OF_SERVICE | status = LocationProvider. TEMPORARILY_UNAVAILABLE ){
}
}
};


The location listening callback is used to handle the automatic callback when the GPS position changes. We can obtain the current GPS data from here. In addition, we can use the location parameter provided by the callback function to obtain the geographical location information of GPS, including latitude and longitude, speed, and altitude.

6. Obtain geographic location information (longitude and latitude, number of satellites, altitude, and positioning status)

Copy codeThe Code is as follows:
// The location object is obtained from the parameters of the Service callback function located above.
Mlatlatitude = location. getLatitude (); // longitude
Mlongpolling = location. getlongpolling (); // latitude
MAltitude = location. getAltitude (); // altitude
MSpeed = location. getSpeed (); // speed
MBearing = location. getBearing (); // direction


7. Obtain the specified satellite information (direction angle, height angle, and signal-to-noise ratio)

Copy codeThe Code is as follows:
// TemgGpsSatellite is the satellite we saved above
// Direction
Float azimuth = temgGpsSatellite. getAzimuth ();
// Height Angle
Float elevation = temgGpsSatellite. getElevation ();
// Signal-to-Noise Ratio
Float snr = temgGpsSatellite. getSnr ();


Using the direction angle and height angle, we can draw a two-dimensional image to indicate the location of the satellite on the earth, and the signal-to-noise ratio is greater. Generally, satellite positioning and testing software provides signal-to-noise ratio status diagrams, which represent the search capability of the GPS module.

8. draw 2D satellite location map
The following is my GPS test:

The following is a method for calculating the positions in the two-dimensional satellite map based on the direction angle and height angle. The green dots on the left above represent the positions of the satellite.

The signal-to-noise ratio column on the right represents the satellite's signal receiving capability.

Copy codeThe Code is as follows:
// Calculate the position displayed by the Satellite Based on the direction angle and height angle.
Point point = new Point ();
Int x = mearheartx; // X coordinate of the Center of the Earth circle on the left
Int y = mearhearty; // Y coordinate of the Center of the Earth circle on the left
Int r = mearr;
X + = (int) (r * elevation * Math. sin (Math. PI * azimuth/180)/90 ));
Y-= (int) (r * elevation * Math. cos (Math. PI * azimuth/180)/90 ));
Point. x = x;
Point. y = y;
// Point is the starting coordinate of the satellite map to be painted.


The painting of signal-to-noise ratio is a unit conversion. No code is provided here.9. Conclusion:
Android provides a convenient location service, mainly through GpsStatus, LocationManager, GpsSatellite to implement related services and listening.

However, I personally think it is very convenient to directly read NMEA data, at least for some applications, more information can be obtained.

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.