Android GPS positioning test (with an effect map and examples) _android

Source: Internet
Author: User

Today, because of the needs of the work, the previous compilation of a GPS test program has been revised again. This program is a bit of a history, I wrote 11 years, when I learned how to develop Android, is an experimental work. Now the work needs to be put back out to fix. At the same time I found that the Android GPS service is not deep, so today I deliberately read some information about GPS services, the relevant knowledge points recorded.

I have done a GPS-related embedded software for several years, so said to do a test of GPS positioning module program, the first reaction is the serial port to read the GPS module data, and then analyze the GPS NMEA format data. NMEA is a standardized data format that is not only applied on GPS, but also used in some other industrial communications. Analyze the relevant data and then display it, then complete a basic GPS positioning test function.

Found Android to do GPS related positioning services, do not need to read NMEA data analysis, Android has encapsulated the relevant services, you have to do is invoke the API. This does not know should feel cool or feel tangled. (Android also provides a read NMEA interface, as the following says)

1. Android Positioning Service
Let's look at Android's support for location-based services:

Android positioning services are located under the location, there are related instructions, here is not detailed analysis. There's one thing to talk about.
Yes: Gpsstatus.nmealistener's official claim is that it can read NMEA data, but I'm testing here to find that it doesn't read the NMEA data. Consulted some data, said that Google at the bottom and did not realize the function of data feedback. Have time, need to check the source code.

2, Locationmanager positioning

Copy Code code as follows:

Get positioning Service
Locationmanager Locationmanager = (locationmanager) this.getsystemservice (Context.location_service);
Determine if the GPS module has been turned on
if (locationmanager.isproviderenabled (Android.location.LocationManager.GPS_PROVIDER))
{
GPS module Open, can locate operation
}

Copy Code code as follows:

Through GPS positioning
String locatetype= Locationmanager.gps_provider;
Location Location = locationmanager.getlastknownlocation (Locatetype);
Set the listener, set the automatic update interval here set 1000ms, move distance: 0 meters.
Locationmanager.requestlocationupdates (provider, 1000, 0, Locationlistener);
Sets the state listener callback function. Statuslistener is a listener callback function.
Locationmanager.addgpsstatuslistener (Statuslistener);
In addition, the positioning settings via network
String locatetype= Locationmanager.network_provider;
Location Location = locationmanager.getlastknownlocation (Locatetype);

3, Gpsstatus Listener
This gives the initialization steps for the positioning service, but we all know that GPS satellites are regularly broadcast data, which means they receive GPS data from satellites on a regular basis. We are not able to actively request data with the satellite, only passively receive data. (China's Beidou 2 can send satellite packets to the satellite) so we need to register a listener to handle the data returned by the satellite.
Copy Code code as follows:

Private final Gpsstatus.listener Statuslistener = new Gpsstatus.listener ()
{
public void ongpsstatuschanged (int event)
{
Callback when GPS state changes, get current state
Gpsstatus status = Locationmanager.getgpsstatus (null);
Their own methods to obtain satellite state-related data
Getgpsstatus (event, status);
}
};

4, access to the search satellite
Copy Code code 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)
{
Get the maximum number of satellites (this is just 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 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)
{
Positioning startup
}
else if (event==gpsstatus.gps_event_stopped)
{
End of position
}
}

Above is the number of satellites retrieved from the state value, mainly through Status.getsatellites (). Gets the Gpssatellite object,
Saved to a queue for subsequent refresh of the interface. Above is to obtain the GPS state listener, in addition to the GPS state, we also need to monitor a service,
is: Locationlistener, positioning listeners, monitoring the location of the changes. This is very important for the application of positioning service.

5, Locationlistener Listener
Copy Code code as follows:

Private final Locationlistener Locationlistener = new Locationlistener ()
{
public void onlocationchanged (Location Location)
{
This function is triggered when the coordinates change, and if provider passes 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, such as when the GPS is closed
LOG.D (TAG, "Locationlistener onproviderdisabled");
}
public void onproviderenabled (String provider)
{
This function is triggered when the provider is turned on, such as when the GPS is opened
LOG.D (TAG, "Locationlistener onproviderenabled");
}
public void onstatuschanged (String provider, int status, Bundle extras)
{
LOG.D (TAG, "Locationlistener onstatuschanged");
This function is triggered when the provider is switched directly between available, temporarily unavailable, and no service three states
if (status = = Locationprovider.out_of_service | | status = locationprovider.temporarily_unavailable) {
}
}
};

Position monitoring callback is used to deal with changes in the GPS position when the automatic callback method, we can obtain from here to the current GPS data. In addition, we can obtain GPS geographic information, including latitude and longitude, speed, elevation and so on, by using the location parameters provided by the callback function.

6, access to geographical information (latitude and longitude, number of satellites, altitude, positioning State)
Copy Code code as follows:

The location object is obtained from the parameter that locates the service callback function above.
Mlatitude = Location.getlatitude (); Longitude
Mlongitude = Location.getlongitude (); Latitude
Maltitude = Location.getaltitude (); Elevation
Mspeed = Location.getspeed (); Speed
mbearing = Location.getbearing (); Direction

7. Obtain the specified satellite information (direction angle, height angle, signal-to-noise ratio)
Copy Code code as follows:

Temggpssatellite is the search satellite we saved above.
Direction angle
float azimuth = Temggpssatellite.getazimuth ();
Height angle
Float elevation = temggpssatellite.getelevation ();
Signal-to-noise ratio (SNR)
Float Snr = Temggpssatellite.getsnr ();

By using the angle of direction and height angle, we can draw a two-dimensional graph, which indicates the position of the satellite in the Earth, the signal-to-noise ratio is more important. In general, the satellite positioning test software provides a state diagram of signal-to-noise ratio, which represents the ability of GPS module to search the star.

8, painting two-dimensional satellite location map
Here are the results of the GPS test I did:

A method for calculating the position of a satellite's two-dimensional map based on the angle of direction and altitude is given below, and the green dot on the left of the above effect chart represents the satellite position.

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

Copy Code code as follows:

According to the direction angle and the altitude angle calculates, the satellite display position
Point point = new Point ();
int x = MEARTHHEARTX; The center of the circle of the left Earth. x coordinates
int y = Mearthhearty; The center of the circle of the left Earth. Y-coordinate
int r = MEARTHR;
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 where you need to draw the starting coordinates of the satellite map

Signal-to-noise ratio of painting, is a unit conversion, here is not to the code.

9. Summary:
Android provides us with a convenient location service, mainly through the Gpsstatus, Locationmanager, gpssatellite these several classes to achieve the relevant services and monitoring.

But personally feel that if you can directly read NMEA data is also very convenient, at least for some applications, can get more information.

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.