First, Locationmanager
Locationmangager, location Manager. To operate the device, you must first define a locationmanager.
[Java]View Plaincopy
- Locationmanger locationmanager= (Locationmanager)this.getsystemservice (Context.location_service);
Second, Locationlistener
Locationlistener, position monitoring, monitoring position change, monitoring device switch and status.
[Java]View Plaincopy
- Defined
- Locationlistener locationlistener=New Locationlistener () {
- //Trigger when position information changes
- public void onlocationchanged (location location) {
- System.out.println ("Time:" +location.gettime ());
- System.out.println ("Longitude:" +location.getlongitude ());
- System.out.println ("Latitude:" +location.getlatitude ());
- System.out.println ("Elevation:" +location.getaltitude ());
- }
- //gps triggered when disabled
- public void onproviderdisabled (String provider) {
- System.out.println ("Current GPS Status: Disable \ n");
- }
- //gps triggered when open
- public void onproviderenabled (String provider) {
- System.out.println ("Current GPS status: on \ n");
- }
- Trigger when//gps state changes
- public void Onstatuschanged (String provider, int status,bundle extras) {
- if (status==locationprovider.available) {
- System.out.println ("Current GPS status: Visible \ n");
- }Else if (status==locationprovider.out_of_service) {
- System.out.println ("Current GPS status: Outside service area \ n");
- }Else if (status==locationprovider.temporarily_unavailable) {
- System.out.println ("Current GPS status: Suspend service \ n");
- }
- }
- };
- Binding monitoring, with 4 parameters
- Parameter 1, equipment: There are gps_provider and network_provider two kinds, we choose GPS, the network does not discuss
- Parameter 2, location information update period:
- Parameter 3, Position change minimum distance: location information is updated when the location distance changes beyond this value
- Parameter 4, monitoring
- 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
- 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
- Location Location=locationmanager.getlastknownlocation (locationmanager.gps_provider);
- System.out.println ("Time:" +location.gettime ());
- 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
- Defined
- Gpsstatus.listener gpsstatuslistener=New Gpsstatus.listener () {
- public void ongpsstatuschanged (int event) {
- if (event==gpsstatus.gps_event_first_fix) {
- //First time positioning
- }Else if (event==gpsstatus.gps_event_satellite_status) {
- //Satellite status change
- Gpsstatus gpsstauts= locationmanager.getgpsstatus (null); //Take current status
- int maxsatellites = Gpsstauts.getmaxsatellites (); //Get the default maximum number of satellites
- Iterator<gpssatellite> it = Gpsstatus.getsatellites (). Iterator (); //Create an iterator to save all satellites
- int count = 0;
- While (It.hasnext () && count <= maxsatellites) {
- count++;
- Gpsstatellite s = it.next ();
- }
- System.out.println ("Search to:" +count+"satellite");
- }Else if (event==gpsstatus.gps_event_started) {
- //Positioning start
- }Else if (event==gpsstatus.gps_event_stopped) {
- //end of position
- }
- }
- };
- Binding
- Locationmanager.addgpsstatuslistener (Gpsstatuslistener);
Wu, Gpsstatus
Gpsstatus,gps state information, we use the gpsstatus when the satellite state changes.
[Java]View Plaincopy
- //instantiate
- Gpsstatus gpsstatus = locationmanager.getgpsstatus (null); // take the current state
- //get the default maximum number of satellites
- int maxsatellites = gpsstatus.getmaxsatellites ();
- //get the first time to locate (boot to first position)
- int costtime=gpsstatus.gettimetofirstfix ();
- //acquiring satellite
- iterable<gpssatellite> iterable= Gpsstatus.getsatellites ();
- //General conversion to iterator
- 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
- Get satellite
- Iterable<gpssatellite> iterable=gpsstatus.getsatellites ();
- Convert to iterator again
- Iterator<gpssatellite> Itrator=iterable.iterator ();
- Reorganize to ArrayList by traversing
- arraylist<gpssatellite> satellitelist=New arraylist<gpssatellite> ();
- int count=0;
- int maxsatellites=gpsstatus.getmaxsatellites ();
- while (Itrator.hasnext () && count <= maxsatellites) {
- count++;
- Satellite = Itrator.next ();
- Statellitelist.add (satellite);
- }
- System.out.println ("Total search for" +count+"satellites");
- Output satellite information
- for (int i=0;i<satellitelist.size (); i++) {
- //Azimuth of the satellite, floating-point data
- System.out.println (Satellitelist.get (i). Getazimuth ());
- //satellite height, floating-point data
- System.out.println (Satellitelist.get (i). Getelevation ());
- //Satellite pseudo-random noise code, shaping data
- System.out.println (Satellitelist.get (i). GETPRN ());
- //satellite signal-to-noise ratio, floating-point data
- System.out.println (Satellitelist.get (i). Getsnr ());
- //Whether the satellite has a calendar table, Boolean data
- System.out.println (Satellitelist.get (i). Hasalmanac ());
- //Whether the satellite has a Ephemeris table, Boolean data
- System.out.println (Satellitelist.get (i). Hasephemeris ());
- //Whether the satellite was used for recent GPS correction calculations
- System.out.println (Satellitelist.get (i). Hasalmanac ());
- }
Android Introduction GPS positioning