Android Introduction GPS positioning

Source: Internet
Author: User
Tags iterable

First, Locationmanager

Locationmangager, location Manager. To operate the device, you must first define a locationmanager.

[Java]View Plaincopy
    1. Locationmanger locationmanager= (Locationmanager)this.getsystemservice (Context.location_service);


Second, Locationlistener

Locationlistener, position monitoring, monitoring position change, monitoring device switch and status.

[Java]View Plaincopy
  1. Defined
  2. Locationlistener locationlistener=New Locationlistener () {
  3. //Trigger when position information changes
  4. public void onlocationchanged (location location) {
  5. System.out.println ("Time:" +location.gettime ());
  6. System.out.println ("Longitude:" +location.getlongitude ());
  7. System.out.println ("Latitude:" +location.getlatitude ());
  8. System.out.println ("Elevation:" +location.getaltitude ());
  9. }
  10. //gps triggered when disabled
  11. public void onproviderdisabled (String provider) {
  12. System.out.println ("Current GPS Status: Disable \ n");
  13. }
  14. //gps triggered when open
  15. public void onproviderenabled (String provider) {
  16. System.out.println ("Current GPS status: on \ n");
  17. }
  18. Trigger when//gps state changes
  19. public void Onstatuschanged (String provider, int status,bundle extras) {
  20. if (status==locationprovider.available) {
  21. System.out.println ("Current GPS status: Visible \ n");
  22. }Else if (status==locationprovider.out_of_service) {
  23. System.out.println ("Current GPS status: Outside service area \ n");
  24. }Else if (status==locationprovider.temporarily_unavailable) {
  25. System.out.println ("Current GPS status: Suspend service \ n");
  26. }
  27. }
  28. };
  29. Binding monitoring, with 4 parameters
  30. Parameter 1, equipment: There are gps_provider and network_provider two kinds, we choose GPS, the network does not discuss
  31. Parameter 2, location information update period:
  32. Parameter 3, Position change minimum distance: location information is updated when the location distance changes beyond this value
  33. Parameter 4, monitoring
  34. Note: Parameters 2 and 3, if the parameter 3 is not 0, the parameter 3 is the same, the parameter 3 is 0, the time is updated periodically, the two is 0, is refreshed at any time
  35. Locationmanager.requestlocationupdates (Locationmanager.gps_provider, 0, Locationlistener);


Third, location

Locations, location information, location information, time, latitude and longitude, altitude and other locations can be obtained. The above uses the Locationlistener inside the onlocationchanged () to obtain the location, the following describes how to proactively obtain location.

[Java]View Plaincopy
    1. Location Location=locationmanager.getlastknownlocation (locationmanager.gps_provider);
    2. System.out.println ("Time:" +location.gettime ());
    3. System.out.println ("Longitude:" +location.getlongitude ());

Note: Each parameter value for location obtained by location Location=new location (Locationmanager.gps_provider) is 0.

Iv. Gpsstatus.listener

Gpsstatus.listener, GPS status monitoring, including GPS start, stop, first positioning, satellite changes and other events.

[Java]View Plaincopy
  1. Defined
  2. Gpsstatus.listener gpsstatuslistener=New Gpsstatus.listener () {
  3. public void ongpsstatuschanged (int event) {
  4. if (event==gpsstatus.gps_event_first_fix) {
  5. //First time positioning
  6. }Else if (event==gpsstatus.gps_event_satellite_status) {
  7. //Satellite status change
  8. Gpsstatus gpsstauts= locationmanager.getgpsstatus (null); //Take current status
  9. int maxsatellites = Gpsstauts.getmaxsatellites (); //Get the default maximum number of satellites
  10. Iterator<gpssatellite> it = Gpsstatus.getsatellites (). Iterator (); //Create an iterator to save all satellites
  11. int count = 0;
  12. While (It.hasnext () && count <= maxsatellites) {
  13. count++;
  14. Gpsstatellite s = it.next ();
  15. }
  16. System.out.println ("Search to:" +count+"satellite");
  17. }Else if (event==gpsstatus.gps_event_started) {
  18. //Positioning start
  19. }Else if (event==gpsstatus.gps_event_stopped) {
  20. //end of position
  21. }
  22. }
  23. };
  24. Binding
  25. Locationmanager.addgpsstatuslistener (Gpsstatuslistener);

Wu, Gpsstatus

Gpsstatus,gps state information, we use the gpsstatus when the satellite state changes.

[Java]View Plaincopy
    1. //instantiate   
    2. Gpsstatus  gpsstatus = locationmanager.getgpsstatus (null);  //  take the current state   
    3. //get the default maximum number of satellites   
    4. int maxsatellites = gpsstatus.getmaxsatellites ();     
    5. //get the first time to locate (boot to first position)   
    6. int costtime=gpsstatus.gettimetofirstfix ();   
    7. //acquiring satellite   
    8. iterable<gpssatellite> iterable= Gpsstatus.getsatellites ();   
    9. //General conversion to iterator  
    10. iterator<gpssatellite> itrator=iterable.iterator ();   


Liu, Gpssatellite

Gpssatellite, positioning satellite, including the satellite's azimuth, height, pseudo-random noise code, signal-to-noise ratio and other information.

[Java]View Plaincopy
    1. Get satellite
    2. Iterable<gpssatellite> iterable=gpsstatus.getsatellites ();
    3. Convert to iterator again
    4. Iterator<gpssatellite> Itrator=iterable.iterator ();
    5. Reorganize to ArrayList by traversing
    6. arraylist<gpssatellite> satellitelist=New arraylist<gpssatellite> ();
    7. int count=0;
    8. int maxsatellites=gpsstatus.getmaxsatellites ();
    9. while (Itrator.hasnext () && count <= maxsatellites) {
    10. count++;
    11. Satellite = Itrator.next ();
    12. Statellitelist.add (satellite);
    13. }
    14. System.out.println ("Total search for" +count+"satellites");
    15. Output satellite information
    16. for (int i=0;i<satellitelist.size (); i++) {
    17. //Azimuth of the satellite, floating-point data
    18. System.out.println (Satellitelist.get (i). Getazimuth ());
    19. //satellite height, floating-point data
    20. System.out.println (Satellitelist.get (i). Getelevation ());
    21. //Satellite pseudo-random noise code, shaping data
    22. System.out.println (Satellitelist.get (i). GETPRN ());
    23. //satellite signal-to-noise ratio, floating-point data
    24. System.out.println (Satellitelist.get (i). Getsnr ());
    25. //Whether the satellite has a calendar table, Boolean data
    26. System.out.println (Satellitelist.get (i). Hasalmanac ());
    27. //Whether the satellite has a Ephemeris table, Boolean data
    28. System.out.println (Satellitelist.get (i). Hasephemeris ());
    29. //Whether the satellite was used for recent GPS correction calculations
    30. System.out.println (Satellitelist.get (i). Hasalmanac ());
    31. }

Android Introduction GPS positioning

Related Article

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.