How to provide GPS-related positioning services on Android

How to provide GPS-related positioning services on Android

Today, due to work needs, I took out a GPS test program I wrote before and revised it. This program has a history. I wrote it in 2011, not long after I learned Android development. It was an experimental work. Now it is needed for work, so I took it out and revised it. At the same time, I found that I don’t know much about Android’s GPS service, so today I specially read some materials about GPS service and recorded the relevant knowledge points.

I have been working on GPS-related embedded software for several years, so when it comes to making a program to test the GPS positioning module, my first reaction is to read the data of the GPS module through the serial port, and then parse the GPS NMEA format data. NMEA is a standardized data format that is not only used in GPS, but also in some other industrial communications. Parsing the relevant data and then displaying it completes a basic GPS positioning test function.

After some research, I found that there is no need to read NMEA data for GPS-related positioning services on Android. Android has already packaged the relevant services, and all you have to do is call the API. I don't know whether I should feel happy or confused about this. (Android also provides an interface for reading NMEA, which will be discussed below)

1. Android Location Services

Let's first look at the support provided by Android for location services:

Android location services are all located under location, and there are relevant instructions above, so I won't analyze them in detail here. One thing I need to say is that GpsStatus.NmeaListener officially says that it can read NMEA data, but I tested it and found that it did not read NMEA data. I checked some information and it said that Google did not implement the data feedback function at the bottom layer. I need to check the source code when I have time.

2. LocationManager positioning

  1. //Get location service
  2. LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
  3. // Determine whether the GPS module is turned on
  4. if (locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {
  5. //GPS module is turned on, positioning operation can be performed
  6. }
  7. // Using GPS positioning
  8. String LocateType = locationManager.GPS_PROVIDER;
  9. Location location = locationManager.getLastKnownLocation(LocateType);
  10. // Set the listener, set the automatic update interval to 1000ms, and the moving distance to 0 meters.
  11. locationManager.requestLocationUpdates(provider, 1000, 0, locationListener);
  12. // Set the status listener callback function. statusListener is the listener callback function.
  13. locationManager.addGpsStatusListener(statusListener);
  14. //Also given through network positioning settings
  15. String LocateType = locationManager.NETWORK_PROVIDER;
  16. Location location = locationManager.getLastKnownLocation(LocateType);

3. GpsStatus Listener

The above gives the initialization steps of the positioning service, but we all know that GPS satellites broadcast data regularly, which means that we will receive GPS data from satellites regularly. We cannot actively request data from satellites, but can only receive data passively. (China's Beidou 2 can send satellite messages to satellites) Therefore, we need to register a listener to process the data returned by the satellite.

  1. private final GpsStatus.Listener statusListener = new GpsStatus.Listener() {
  2. public void onGpsStatusChanged( int event) {
  3. //Callback when GPS status changes, get the current status
  4. GpsStatus status = locationManager.getGpsStatus( null );
  5. //Method written by myself to obtain satellite status related data
  6. GetGPSStatus(event, status);
  7. }
  8. };

4. Get the searched satellites

  1. private void GetGPSStatus( int event, GpsStatus status) {
  2. Log.d(TAG, "enter the updateGpsStatus()" );
  3. if (status == null ) {
  4. } else if (event == GpsStatus.GPS_EVENT_SATELLITE_STATUS) {
  5. //Get the maximum number of satellites (this is just a preset value)
  6. int maxSatellites = status.getMaxSatellites();
  7. Iterator it = status.getSatellites().iterator();
  8. numSatelliteList.clear();
  9. //Record the actual number of satellites
  10. int   count = 0;
  11. while (it.hasNext() && count <= maxSatellites) {
  12. //Save the satellite data to a queue for refreshing the interface
  13. GpsSatellite s = it. next ();
  14. numSatelliteList.add (s) ;
  15. count ++;
  16. Log.d(TAG, "updateGpsStatus----count=" + count );
  17. }
  18. mSatelliteNum = numSatelliteList. size ();
  19. } else if (event == GpsStatus.GPS_EVENT_STARTED) {
  20. //Start positioning
  21. } else if (event == GpsStatus.GPS_EVENT_STOPPED) {
  22. //End of positioning
  23. }
  24. }

The above is to get the number of satellites searched from the status value, mainly through status.getSatellites(). The obtained GpsSatellite object is saved in a queue for later interface refresh. The above is to get the GPS status listener. In addition to the GPS status, we also need to listen to a service, namely: LocationListener, positioning listener, to listen to the change of location. This is very important for applications that provide positioning services.

5. LocationListener

  1. private final LocationListener locationListener = new LocationListener()
  2. {
  3. public void onLocationChanged(Location location)
  4. {
  5. //This function is triggered when the coordinates change. If the Provider passes in the same coordinates, it will not be triggered.
  6. updateToNewLocation(location);
  7. Log.d(TAG, "LocationListener onLocationChanged" );
  8. }
  9. public void onProviderDisabled(String provider)
  10. {
  11. //This function is triggered when the provider is disabled, such as when the GPS is turned off.
  12. Log.d(TAG, "LocationListener onProviderDisabled" );
  13. }
  14. public void onProviderEnabled(String provider)
  15. {
  16. // This function is triggered when the Provider is enabled, such as when GPS is turned on
  17. Log.d(TAG, "LocationListener onProviderEnabled" );
  18. }
  19. public void onStatusChanged(String provider, int status, Bundle extras)
  20. {
  21. Log.d(TAG, "LocationListener onStatusChanged" );
  22. // This function is triggered when the Provider switches between available, temporarily unavailable, and out of service states.
  23. if (status == LocationProvider.OUT_OF_SERVICE || status == LocationProvider.TEMPORARILY_UNAVAILABLE) {
  24. }
  25. }
  26. };

The location monitoring callback is used to automatically call back when the GPS location changes. We can get the current GPS data from here. In addition, we can get the GPS location information, including longitude and latitude, speed, altitude, etc., through the location parameter provided by the callback function. 6. Get location information (longitude and latitude, number of satellites, altitude, positioning status)

  1. //The location object is obtained from the parameters of the positioning service callback function above.
  2. mLatitude = location.getLatitude(); // Longitude
  3. mLongitude = location.getLongitude(); // Latitude
  4. mAltitude = location.getAltitude(); //Altitude
  5. mSpeed ​​= location.getSpeed(); //speed
  6. mBearing = location.getBearing(); //Direction

7. Get the specified satellite information (direction angle, altitude angle, signal-to-noise ratio)

  1. //temgGpsSatellite is the satellite we searched and saved above
  2. //Direction angle
  3. float azimuth = temgGpsSatellite.getAzimuth();
  4. //Altitude angle
  5. float elevation = temgGpsSatellite.getElevation();
  6. //Signal to noise ratio
  7. float snr = temgGpsSatellite.getSnr();

Using the direction angle and altitude angle, we can draw a two-dimensional graph to show the position of the satellite on the earth where the signal-to-noise ratio has a greater effect. General satellite positioning test software provides a state diagram of the signal-to-noise ratio, which represents the GPS module's satellite search capability.

8. Draw a 2D satellite position map

Here is the effect of the GPS test I did:

Below is a method for calculating the position of a satellite in a two-dimensional image based on the direction angle and altitude angle. The green dot on the left side of the above rendering represents the satellite position.

The signal-to-noise ratio bar graph on the right represents the satellite's ability to receive signals.

  1. //Calculate the satellite's displayed position based on the direction angle and altitude angle
  2. Point point = new Point();
  3. int x = mEarthHeartX; //The X coordinate of the center of the circle on the left
  4. int y = mEarthHeartY; //Y coordinate of the center of the circle on the left
  5. int r = mEarthR;
  6. x+=( int )((r*elevation*Math.sin(Math.PI*azimuth/180)/90));
  7. y-=( int )((r*elevation*Math.cos(Math.PI*azimuth/180)/90));
  8. point.x = x;
  9. point.y = y;
  10. //point is the starting coordinates of the satellite image you need to draw

The drawing of signal-to-noise ratio is just a unit conversion, so I won’t give the code here.

9. Summary:

Android provides us with very convenient location services, mainly through the GpsStatus, LocationManager, GpsSatellite these classes to implement related services and monitoring.

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

<<:  Four scenarios that are best suited for RxJava

>>:  Designing an APP from scratch: Android design specifications

Recommend

Bilibili Promotion: Hardcore teacher’s skills to become popular!

Why can a teacher who teaches criminal law attrac...

Responsive Image Processing in Web Development

At present, there are relatively good solutions f...

Mobile game manufacturers enter the era of all-round marketing

The domestic mobile game industry has become a re...

Have you ever heard of an assassination that comes from the sky?

Introduction: Mysterious backs, silent assassinat...

Zang Qichao Equity Incentive and Mechanism Design

Zang Qichao's equity incentive and mechanism ...