Android GPS Example

摘要:Android GPS Example

Implement LocationListener interface

 


package com.kent.gps;
 
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
 
public class GpsTestActivity extends Activity implements LocationListener { 
    private LocationManager mLocationManager;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setContentView(R.layout.main);
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, this);
        TextView mTextView00 = (TextView)findViewById(R.id.TextView00);  
        mTextView00.setText("GPS Information");
    }
 
    @Override
    public void onResume(){
        if (mLocationManager != null) {
     
        }               
        super.onResume();
    }
 
    @Override   
    protected void onPause() {        
        if (mLocationManager != null) {            
            mLocationManager.removeUpdates((LocationListener) this);        
        }                
        super.onPause();    
    }  
 
    public void onLocationChanged(Location location) {        
        TextView mTextView01 = (TextView)findViewById(R.id.TextView01);
        TextView mTextView02 = (TextView)findViewById(R.id.TextView02);
        TextView mTextView03 = (TextView)findViewById(R.id.TextView03);
        TextView mTextView04 = (TextView)findViewById(R.id.TextView04);
        TextView mTextView05 = (TextView)findViewById(R.id.TextView05);
        TextView mTextView06 = (TextView)findViewById(R.id.TextView06);
        TextView mTextView07 = (TextView)findViewById(R.id.TextView07);

        mTextView01.setText("Latitude:  " + String.valueOf(location.getLatitude()));
        mTextView02.setText("Longitude:  " + String.valueOf(location.getLongitude()));
        mTextView03.setText("Accuracy:  " + String.valueOf(location.getAccuracy()));
        mTextView04.setText("Latitude:  " + String.valueOf(location.getAltitude()));
        mTextView05.setText("Time:  " + String.valueOf(location.getTime()));
        mTextView06.setText("Speed:  " + String.valueOf(location.getSpeed()));
        mTextView07.setText("Bearing:  " + String.valueOf(location.getBearing()));   
    }
 
    public void onProviderDisabled(String provider) {
 
    }
    public void onProviderEnabled(String provider) {
 
    }
    public void onStatusChanged(String provider, int status, Bundle extras) {
 
    }   
}

 

Framework:

GpsLocation結構: 表示定位相關資訊

typedef struct {
    // set to sizeof(GpsLocation)
    size_t             size;
    //旗標位元
    uint16_t           flags;
    //緯度
    double          latitude;
    //經度
    double          longitude;
    //高度
    double          altitude;
    //速度
    float              speed;
    //方位
    float              bearing;
    //精準度
    float              accuracy;
    //時間戳記
    GpsUtcTime            timestamp;
} GpsLocation;
 
 
GpsStatusValue定義: 表示GPS晶片狀態
typedef uint16_t GpsStatusValue; //儲存GPS狀態 下面狀態的 0~4
#define GPS_STATUS_NONE             0
#define GPS_STATUS_SESSION_BEGIN    1//啟動導航
#define GPS_STATUS_SESSION_END      2//停止導航
#define GPS_STATUS_ENGINE_ON        3//未啟動導航
#define GPS_STATUS_ENGINE_OFF       4//電源關閉
 
 
GpsSvInfo結構: 描述衛星狀態
typedef struct {
    size_t  size;
    int       prn; //偽亂碼 即衛星編號
    float    snr;//衛星訊號
    float    elevation;//高度
    float   azimuth;//方位角
} GpsSvInfo;
 
 
GpsInterface結構: 連通上下層 為主要GPS的架構
typedef struct {
    size_t          size;
    int   (*init)( GpsCallbacks* callbacks );//初始化 並設定回調函數
    int   (*start)( void );//啟動導航
    int   (*stop)( void ); //關閉導航
    void  (*cleanup)( void );
    int   (*inject_time)(GpsUtcTime time, int64_t timeReference, int uncertainty);//插入目前時間
    int  (*inject_location)(double latitude, double longitude, float accuracy);//插入位置資訊
    void  (*delete_aiding_data)(GpsAidingData flags);//刪除輔助資訊
    int   (*set_position_mode)(GpsPositionMode mode, GpsPositionRecurrence recurrence, uint32_t   
           min_interval, uint32_t preferred_accuracy, uint32_t preferred_time);//設定位置模式
    const void* (*get_extension)(const char* name);//取得擴充介面
} GpsInterface;