SyntaxHighlighter

About Me

2012年7月1日 星期日

GPS實作(非使用google map 純使用android library)

最近需要寫有關利用手機去偵測現在的location(經度、緯度)
但不要透過Google map API實現

以下機型測試成功:

HTC Sencation XE(android 4.0.3)
Sony Xperia Arc(android 4.0.4)
HTC One X(android 4.0.3)



android.app.Activity;
android.content.Context;
android.location.Location;  //可以在指定的時間提供location
android.location.LocationListener;  //為自訂億的class android library沒有這個class
android.location.LocationManager;  //提供可以存取系統location的service
android.os.Bundle;  //傳送參數會使用
android.view.View;
android.view.View.OnClickListener;
android.widget.Button;
android.widget.Toast;  //快顯訊息

public class GPS extends Activity {
 
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
    //設定超過一個距離(尺)他就會做更新location的動作
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000;
    //設定超過一個時間(毫秒)他就會做更新location的動作
 
    protected LocationManager locationManager;
 
    protected Button retrieveLocationButton;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
     
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        retrieveLocationButton = (Button)findViewById(R.id.retrieve_location_button);
        
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        //get location service
        
        //requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)
        //Criteria: 訂定尋找location provider的標準
        //PendingIntent: 處理即將發生的事件
        //PendingIntent與Intent的差異:Intent會隨著Activiy的消失而消失,PendingIntent不會
        locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,  //Name of the GPS provider
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,
            new MyLocationListener()
        );
        
 retrieveLocationButton.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
         showCurrentLocation();
     }
 });        
        
    }    

 protected void showCurrentLocation() {

     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
     //取得最後得知道provider資訊
     if (location != null) {
         String message = String.format(
                    "Current Location \n Longitude: %1$s \n Latitude: %2$s",
      location.getLongitude(), location.getLatitude());
  Toast.makeText(GPS.this, message,Toast.LENGTH_LONG).show();
     }

 }   

 private class MyLocationListener implements LocationListener {

     public void onLocationChanged(Location location) {
         String message = String.format(
             "New Location \n Longitude: %1$s \n Latitude: %2$s",
      location.getLongitude(), location.getLatitude());
      //將想要印出的資料用string.format的方法存入string
      Toast.makeText(GPS.this, message, Toast.LENGTH_LONG).show();
      //Toast.LENGTH_LONG:以最長的時間週期呈現文字
     }

     public void onStatusChanged(String s, int i, Bundle b) {
         Toast.makeText(GPS.this, "Provider status changed",
      Toast.LENGTH_LONG).show();
         //當provider cheange的時候會顯示
     }

     public void onProviderDisabled(String s) {
  Toast.makeText(GPS.this,
                    "Provider disabled by the user. GPS turned off",
      Toast.LENGTH_LONG).show();
  //當device的GPS沒有開啟的時候他會顯示
     }

     public void onProviderEnabled(String s) {
  Toast.makeText(GPS.this,
      "Provider enabled by the user. GPS turned on",
      Toast.LENGTH_LONG).show();
  //當device將GPS打開的時候他會顯示
     }

 }
    
}

最後別忘了需要一些permission:
在AndroidManifest.xml的檔案裡面的< /application >後面加上


< uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
//允許一個程式存取精確位置
< uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
//允許程式建立模組使用
< uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
//允許程式存取CellID或WiFi熱點來取得粗略的位置
參考網址:
http://hejp.co.uk/android/android-gps-example/
http://www.javacodegeeks.com/2010/09/android-location-based-services.html
http://www.moke.tw/wordpress/computer/advanced/279

4 則留言:

  1. nice posting.. thanks for sharing.

    回覆刪除
  2. 幫我問看看 android 4.0 以上
    要實作 google API 該怎麼做???

    回覆刪除
  3. 請問如果要把抓到的GPS訊號內容直接write data在output.txt裡要怎麼寫?

    回覆刪除