Android Location Service (reprint)

Source: Internet
Author: User

Today, because of the need for work , a previously written GPS test program to take out a re-modified a bit. This program said some history, I wrote it 11 years ago, when I learned Android development Not long, is an experimental work. Now the work needs to be re-trimmed. I also found that I do not know much about the Android GPS service, so I read some information about the GPS service today and record the relevant knowledge points.

I did a GPS-related embedded software for several years, so to do a test GPS positioning module program, the first reaction is to read the GPS module serial data, and then analyze the GPS NMEA format data. NMEA is a standardized data format that is not only applied on GPs, but also in some other industrial communications using this standardized data format. Parse the relevant data and then display it and complete a basic GPS location TestFunction. Check to find out that the Android GPS-related location services, do not need to read NMEA data analysis, Android has encapsulated the relevant services, all you have to do is call the API. This does not know should feel cool or feel tangled. (Android also provides read NMEA interface, which will be said below) 1. Android Location ServiceLet's take a look at the Android support for location services: Android Location services are located in the place, there are instructions, here is not detailed analysis. One thing to say is that the Gpsstatus.nmealistener official argument is that it can read the NMEA data, but I've found here that I haven't read the NMEA data. Read some of the information, said Google at the bottom of the data does not realize the function of feedback. Have time, need to check the source code. 2, Locationmanager positioning
Edited by mythou//http://www.cnblogs.com/mythou///get location service Locationmanager Locationmanager = (LocationManager) This.getsystemservice (Context.location_service);//Determine if the GPS module is turned on if (locationmanager.isproviderenabled ( Android.location.LocationManager.GPS_PROVIDER)) {//gps module Open, can locate operation}//Locate string locatetype= Locationmanager.gps via GPS _provider; Location location = locationmanager.getlastknownlocation (locatetype);//Set listener, set Automatic update interval set 1000ms here, move distance: 0 meters. Locationmanager.requestlocationupdates (provider, 0, Locationlistener);//Set the state listener callback function. Statuslistener is a callback function for listening. Locationmanager.addgpsstatuslistener (Statuslistener);//The setting of string via network positioning is also given locatetype= Locationmanager.network_provider; Location location = locationmanager.getlastknownlocation (Locatetype);
  3. Gpsstatus ListenerThe initialization setting of the location service is given above, but we all know that the GPS satellite broadcasts the data periodically, which means that the GPS data of the satellite will be received periodically. We cannot actively request data with satellites and can only passively receive data. (China's Beidou 2 can send satellite messages to satellites) so we need to register a listener to handle the data returned by the satellite.
Edited by Mythou//http://www.cnblogs.com/mythou/private final Gpsstatus.listener Statuslistener = new Gpsstatus.listener () {public void ongpsstatuschanged (int event) {///////GPS status change callback, get current state gpsstatus status = Locationmanager.getgpsstatus (null);//Write your own method to obtain satellite status-related data Getgpsstatus (event, status);}};
   4. Access to the search satellite
//edited by mythou//http://www.cnblogs.com/m ythou/private void Getgpsstatus (int event, gpsstatus status) {LOG.D (TAG, "Enter the Updategpsstatus ()"); if (status = = NULL {}else if (event = = Gpsstatus.gps_event_satellite_status) {//Gets the maximum number of satellites (this is just a preset) 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 the Refresh 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 start}else if (event==gpsstatus.gps_event_stopped) {//Locate end}}
Above is the number of satellites retrieved from the state value, mainly through Status.getsatellites (). Gets the Gpssatellite object, which is saved to a queue and is used to refresh the interface later. Above is the acquisition of GPS status listener, in addition to GPS status, we also need to listen to a service, that is: locationlistener, positioning listeners, monitoring location changes. This is very important for the application of location services. 5. Locationlistener Listener
//edited by mythou//http://www.cnblogs.com/m Ythou/private final Locationlistener Locationlistener = new Locationlistener () {public void onlocationchanged (location Location) {//When coordinates change, this function is triggered, and if provider passes in the same coordinates, it will not be triggered updatetonewlocation (location); LOG.D (TAG, "Locationlistener  onlocationchanged");} public void onproviderdisabled (String provider) {//provider triggers this function when disable, such as the GPS is turned off LOG.D (TAG, "Locationlistener  onproviderdisabled ");} public void onproviderenabled (String provider) {////Provider triggers this function when enable, such as GPS is turned on LOG.D (TAG, "Locationlistener   Onproviderenabled ");} public void onstatuschanged (String provider, int status, Bundle extras) {log.d (TAG, "Locationlistener   Onstatuschanged ");//Provider The TURN state triggers this function when it is available, temporarily unavailable, and without a service three states direct switch if (status = = Locationprovider.out_of_service | | status = = locationprovider.temporarily_unavailable) {}}};
Position monitoring callback is used to handle the change of GPS position when the automatic callback method, we can obtain from here to the current GPS data. In addition, we can use the location parameter provided by the callback function to obtain GPS geolocation information, including latitude and longitude, speed, altitude, and so on. 6, Access to geographic information (latitude and longitude, number of satellites, altitude, positioning status)
The edited by Mythou//http://www.cnblogs.com/mythou///location object is obtained from the parameters of the location 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, access to the specified satellite information (direction angle, height angle, signal to noise ratio)
Edited by Mythou//http://www.cnblogs.com/mythou///temggpssatellite is the search satellite we saved above//direction angle float azimuth = Temggpssatellite.getazimuth ();//height angle float elevation = temggpssatellite.getelevation ();//signal to noise ratio float SNR = Temggpssatellite.getsnr ();
Using the direction angle, the height angle we can draw a two-dimensional graph, which means that the satellite is in the Earth's direction, the signal-to-noise ratio is more important. The general satellite location test software provides a signal-to-noise ratio status graph, which represents the GPS module's ability to search for stars. 8, painting two-dimensional satellite location mapHere is the GPS test I do: The following is a method based on the direction angle and height angle, calculate the location of the satellite two-dimensional map, the green dot on the left represents the satellite location. The signal-to-noise ratio histogram on the right represents the satellite's ability to receive signals.
Edited by mythou//http://www.cnblogs.com/mythou///calculated based on the direction angle and height angle, the location of the satellite display point point = new points (); int x = MEARTHHEARTX; The center position of the Left Earth Circle is x coordinate int y = Mearthhearty; The center position of the Left Earth Circle is the 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));p oint.x = X;point.y = Y;//point is the starting coordinates of the satellite map you need to draw
The signal to noise ratio painting, is a unit conversion, here does not give the code. 9. Summary:Android provides us with convenient location services, mainly through the Gpsstatus, Locationmanager, Gpssatellite and other categories to achieve related services and monitoring. Personally, however, it is convenient to read the NMEA data directly, at least for some applications to get more information.

Android Location Service (reprint)

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.