android GPS JAVA應用程式編程-------獲得經緯度,衛星資訊等

來源:互聯網
上載者:User

Java代碼 
package android.test; 
 
import java.util.Iterator; 
 
import android.app.Activity; 
import android.location.Criteria; 
import android.location.GpsSatellite; 
import android.location.GpsStatus; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.location.GpsStatus.Listener; 
import android.location.GpsStatus; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 
 
public class TestActivity extends Activity implements LocationListener   

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        Log.i(tag, "on Create"); 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        btn=(Button)findViewById(R.id.btn); 
        tv1=(TextView)findViewById(R.id.tv1); 
        tv2=(TextView)findViewById(R.id.tv1); 
        listener = new GpsStatus.Listener() { 
            public void onGpsStatusChanged(int event) { 
                gpsstatus=mgr.getGpsStatus(null); 
                switch(event) 
                { 
                case GpsStatus.GPS_EVENT_FIRST_FIX:gpsstatus.getTimeToFirstFix();   
                case GpsStatus.GPS_EVENT_SATELLITE_STATUS: 
                   //得到所有收到的衛星的資訊,包括 衛星的高度角、方位角、信噪比、和偽隨機號(及衛星編號) 
            Iterable<GpsSatellite> allSatellites; 
            allSatellites = gpsstatus.getSatellites(); 
            Iterator it=allSatellites.iterator(); 
            String msg=""; 
            while(it.hasNext()) 
            { 
                GpsSatellite oSat = (GpsSatellite) it.next() ;  
                 msg="azimuth:"+oSat.getAzimuth(); 
                msg+="\nprn:"+oSat.getPrn(); 
                msg+="\nsnr:"+oSat.getSnr(); 
            } 
            tv2.setText(msg); 
            break; 
             
           case GpsStatus.GPS_EVENT_STARTED: 
                   //Event sent when the GPS system has started. 
           break; 
             
           case GpsStatus.GPS_EVENT_STOPPED: 
                  //Event sent when the GPS system has stopped.  
            break; 
             
           default : 
            break; 
                } 
                 
            } 
     }; 
        btn.setOnClickListener(new Button.OnClickListener() 
        { 
 
            @Override 
            public void onClick(View arg0) { 
                // TODO Auto-generated method stub 
                Log.i(tag, "on button click"); 
                getdata(); 
                show(); 
            } 
             
        }); 
          
    } 
   void show() 
   { 
       String msg=""; 
       msg+="latitude:"+latitude; 
       msg+="\nlongtitude:"+longtitude; 
       msg+="\naltitude:"+altitude; 
       msg+="\naccuracy:"+accuracy; 
       msg+="\nbearing:"+bearing; 
       msg+="\nspeed:"+speed; 
       msg+="\ntime:"+time; 
       tv1.setText(msg); 
       //Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); 
   } 
   GpsStatus.Listener listener; 
   private GpsStatus gpsstatus; 
    private final String tag="TEST_MAY"; 
    private LocationManager mgr; 
    private Button btn; 
    private TextView tv1,tv2; 
    private String best; 
    private double latitude; 
    private double longtitude; 
    private double altitude; 
    private float accuracy; 
    private float bearing; 
    private float speed; 
    private long time; 
    private void getdata() 
    { 
         
        try 
        {mgr=(LocationManager)getSystemService(LOCATION_SERVICE); 
        mgr.addGpsStatusListener(listener); 
        Criteria criteria= new Criteria(); 
        best=mgr.getBestProvider(criteria, true); 
        Location location= mgr.getLastKnownLocation("gps"); 
         
        if(location.hasAccuracy()) accuracy=location.getAccuracy(); 
        Log.d(tag, "getdata"); 
        if(location.hasAltitude()) altitude=location.getAltitude(); 
        if(location.hasBearing()) bearing=location.getBearing(); 
        if(location.hasSpeed()) speed=location.getSpeed(); 
        Log.d(tag, "getsomedata"); 
         
            latitude=location.getLatitude(); 
            longtitude=location.getLongitude(); 
            time=location.getTime(); 
             
        } 
        catch( NullPointerException e) 
        { 
             
        } 
         
    } 
    
    @Override 
    public void onLocationChanged(Location location) { 
        // TODO Auto-generated method stub 
        Toast.makeText(this, location.toString(), Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onProviderDisabled(String provider) { 
        // TODO Auto-generated method stub 
        Toast.makeText(this, provider+" disable", Toast.LENGTH_SHORT).show(); 
    } 
    @Override 
    public void onProviderEnabled(String provider) { 
        // TODO Auto-generated method stub 
        Toast.makeText(this, provider+" enable", Toast.LENGTH_LONG).show(); 
    } 
    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) { 
        // TODO Auto-generated method stub 
        Toast.makeText(this, status+" changed", Toast.LENGTH_SHORT).show(); 
    } 
 

作者“極客網路”
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.