Welcome 微信登录

首页 / 操作系统 / Linux / Android GPS JAVA应用程序编程:获得经纬度,卫星信息等

Android GPS JAVA应用程序编程:获得经纬度,卫星信息等
  1. package android.test;  
  2.   
  3. import java.util.Iterator;  
  4.   
  5. import android.app.Activity;  
  6. import android.location.Criteria;  
  7. import android.location.GpsSatellite;  
  8. import android.location.GpsStatus;  
  9. import android.location.Location;  
  10. import android.location.LocationListener;  
  11. import android.location.LocationManager;  
  12. import android.os.Bundle;  
  13. import android.location.GpsStatus.Listener;  
  14. import android.location.GpsStatus;  
  15. import android.util.Log;  
  16. import android.view.View;  
  17. import android.widget.Button;  
  18. import android.widget.TextView;  
  19. import android.widget.Toast;  
  20.   
  21. public class TestActivity extends Activity implements LocationListener    
  22. {  
  23.     /** Called when the activity is first created. */  
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         Log.i(tag, "on Create");  
  27.         super.onCreate(savedInstanceState);  
  28.         setContentView(R.layout.main);  
  29.         btn=(Button)findViewById(R.id.btn);  
  30.         tv1=(TextView)findViewById(R.id.tv1);  
  31.         tv2=(TextView)findViewById(R.id.tv1);  
  32.         listener = new GpsStatus.Listener() {  
  33.             public void onGpsStatusChanged(int event) {  
  34.                 gpsstatus=mgr.getGpsStatus(null);  
  35.                 switch(event)  
  36.                 {  
  37.                 case GpsStatus.GPS_EVENT_FIRST_FIX:gpsstatus.getTimeToFirstFix();    
  38.                 case GpsStatus.GPS_EVENT_SATELLITE_STATUS:  
  39.                    //得到所有收到的卫星的信息,包括 卫星的高度角、方位角、信噪比、和伪随机号(及卫星编号)   
  40.             Iterable<GpsSatellite> allSatellites;  
  41.             allSatellites = gpsstatus.getSatellites();  
  42.             Iterator it=allSatellites.iterator();  
  43.             String msg="";  
  44.             while(it.hasNext())  
  45.             {  
  46.                 GpsSatellite oSat = (GpsSatellite) it.next() ;   
  47.                  msg="azimuth:"+oSat.getAzimuth();  
  48.                 msg+=" prn:"+oSat.getPrn();  
  49.                 msg+=" snr:"+oSat.getSnr();  
  50.             }  
  51.             tv2.setText(msg);  
  52.             break;  
  53.               
  54.            case GpsStatus.GPS_EVENT_STARTED:  
  55.                    //Event sent when the GPS system has started.   
  56.            break;  
  57.               
  58.            case GpsStatus.GPS_EVENT_STOPPED:  
  59.                   //Event sent when the GPS system has stopped.    
  60.             break;  
  61.               
  62.            default :  
  63.             break;  
  64.                 }  
  65.                   
  66.             }  
  67.      };  
  68.         btn.setOnClickListener(new Button.OnClickListener()  
  69.         {  
  70.   
  71.             @Override  
  72.             public void onClick(View arg0) {  
  73.                 // TODO Auto-generated method stub   
  74.                 Log.i(tag, "on button click");  
  75.                 getdata();  
  76.                 show();  
  77.             }  
  78.               
  79.         });  
  80.            
  81.     }  
  82.    void show()  
  83.    {  
  84.        String msg="";  
  85.        msg+="latitude:"+latitude;  
  86.        msg+=" longtitude:"+longtitude;  
  87.        msg+=" altitude:"+altitude;  
  88.        msg+=" accuracy:"+accuracy;  
  89.        msg+=" bearing:"+bearing;  
  90.        msg+=" speed:"+speed;  
  91.        msg+=" time:"+time;  
  92.        tv1.setText(msg);  
  93.        //Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();   
  94.    }  
  95.    GpsStatus.Listener listener;  
  96.    private GpsStatus gpsstatus;  
  97.     private final String tag="TEST_MAY";  
  98.     private LocationManager mgr;  
  99.     private Button btn;  
  100.     private TextView tv1,tv2;  
  101.     private String best;  
  102.     private double latitude;  
  103.     private double longtitude;  
  104.     private double altitude;  
  105.     private float accuracy;  
  106.     private float bearing;  
  107.     private float speed;  
  108.     private long time;  
  109.     private void getdata()  
  110.     {  
  111.           
  112.         try  
  113.         {mgr=(LocationManager)getSystemService(LOCATION_SERVICE);  
  114.         mgr.addGpsStatusListener(listener);  
  115.         Criteria criteria= new Criteria();  
  116.         best=mgr.getBestProvider(criteria, true);  
  117.         Location location= mgr.getLastKnownLocation("gps");  
  118.           
  119.         if(location.hasAccuracy()) accuracy=location.getAccuracy();  
  120.         Log.d(tag, "getdata");  
  121.         if(location.hasAltitude()) altitude=location.getAltitude();  
  122.         if(location.hasBearing()) bearing=location.getBearing();  
  123.         if(location.hasSpeed()) speed=location.getSpeed();  
  124.         Log.d(tag, "getsomedata");  
  125.           
  126.             latitude=location.getLatitude();  
  127.             longtitude=location.getLongitude();  
  128.             time=location.getTime();  
  129.               
  130.         }  
  131.         catch( NullPointerException e)  
  132.         {  
  133.               
  134.         }  
  135.           
  136.     }  
  137.      
  138.     @Override  
  139.     public void onLocationChanged(Location location) {  
  140.         // TODO Auto-generated method stub   
  141.         Toast.makeText(this, location.toString(), Toast.LENGTH_SHORT).show();  
  142.     }  
  143.     @Override  
  144.     public void onProviderDisabled(String provider) {  
  145.         // TODO Auto-generated method stub   
  146.         Toast.makeText(this, provider+" disable", Toast.LENGTH_SHORT).show();  
  147.     }  
  148.     @Override  
  149.     public void onProviderEnabled(String provider) {  
  150.         // TODO Auto-generated method stub   
  151.         Toast.makeText(this, provider+" enable", Toast.LENGTH_LONG).show();  
  152.     }  
  153.     @Override  
  154.     public void onStatusChanged(String provider, int status, Bundle extras) {  
  155.         // TODO Auto-generated method stub   
  156.         Toast.makeText(this, status+" changed", Toast.LENGTH_SHORT).show();  
  157.     }  
  158.   
  159. }