Android GPS positioning test (with renderings)

Source: Internet
Author: User

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 information 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 the data to complete 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. nmealistenerThe 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.

 

3. locationmanager Positioning

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
// Obtain the location service
Locationmanager = (locationmanager) This. getsystemservice (context. location_service );
// Determine whether the GPS module is enabledIf (locationmanager. isproviderenabled (Android. Location. locationmanager. gps_provider )){  //The GPS module is enabled to locate the operation.}
// Locate by GPSString locatetype = locationmanager. gps_provider; 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 = 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.

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
Private Final gpsstatus. Listener statuslistener = new gpsstatus. listener ()
{Public void ongpsstatuschanged (INT event)
{  // Callback when the GPS status changes to get the current statusGpsstatus status = locationmanager. getgpsstatus (null );
    // Self-compiled method to obtain satellite status dataGetgpsstatus (event, status );}};

 

4. Retrieve the searched Satellite

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
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 satellitesInt COUNT = 0; while (it. hasnext () & count <= maxsatellites ){
       // Save the satellite data to a queue for refreshing the interfaceGpssatellite 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 ended}}

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

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
Private Final locationlistener = new locationlistener ()
{Public void onlocationchanged (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 enabledLog. 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 switchedIf (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)

 

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
// The location object is obtained from the parameters of the Service callback function located above.
Mlatlatitude = location. getlatitude ();// LongitudeMlongpolling = location. getlongpolling ();// LatitudeMaltitude = location. getaltitude ();// AltitudeMspeed = location. getspeed ();// SpeedMbearing = location. getbearing ();// Direction

 

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

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
// Temggpssatellite is the searched satellite // direction angle stored above.Float azimuth = temggpssatellite. getazimuth ();// Height AngleFloat elevation = temggpssatellite. getelevation ();// Signal-to-Noise RatioFloat 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.

 
//Edited by mythou
//http://www.cnblogs.com/mythou/
// 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 circle on the leftInt y = mearhearty;// Y coordinate of the center of the circle on the leftInt 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.

 

Edited by mythou

Original blog, reprinted please indicate the source: http://www.cnblogs.com/mythou/p/3167648.html

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.