星期日, 10月 16, 2011

實作Location的取得與LocationListener的設置

實作Location的取得與LocationListener的設置


1.在main.xml新增一個TextView

  1. <TextView 
  2. android:text="TextView" 
  3. android:id="@+id/location" 
  4. android:layout_width="wrap_content" 
  5. android:layout_height="wrap_content">
  6. </TextView>
2.編輯activity
  1. package test.location;

  2. import java.util.List;

  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.location.Location;
  6. import android.location.LocationListener;
  7. import android.location.LocationManager;
  8. import android.os.Bundle;
  9. import android.widget.TextView;

  10. public class HelloLocationActivity extends Activity {
  11.     /** Called when the activity is first created. */
  12. //創建新專案,注意創建時,target build選擇Google APIs,這是為了添加jar文件maps.jar。
  13. //引入LocationManager實例。
  14. //在main文件中宣告TextView。
  15. //在Androidmanifest文件中宣告權限。
  16. private LocationManager locationManager;
  17.     @Override
  18.     public void onCreate(Bundle savedInstanceState) {
  19.         super.onCreate(savedInstanceState);
  20.         setContentView(R.layout.main);
  21.         //呼叫getSystemService()取得LocationManager物件。
  22.         locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
  23.         locate();
  24.     }
  25.     
  26.     private void locate(){
  27.      TextView tv = (TextView)findViewById(R.id.location);
  28.      StringBuilder builder = new StringBuilder("可利用的provider");
  29.      //呼叫getProviders(boolean EnableOnly)取得一組可用的location provider名稱,true表示為可用的;false表示停用的。
  30.      List<String> providers = locationManager.getProviders(true);
  31.      //定義位置監聽器,接收來自LocationManager的通知。
  32.      LocationListener locationlistener = new LocationListener(){
  33.      //位置管理服務利用reuqestLocationUpdates(String , long , float , LocationListener)方法註冊位置監聽器後,這些方法就會被呼叫。
  34.      //Provider狀態改變時呼叫此方法。
  35.      public void onStatusChanged(String provider , int status , Bundle extras){
  36.     
  37.      }
  38.      //位置改變時呼叫此方法。
  39. @Override
  40. public void onLocationChanged(Location location) {
  41. // TODO Auto-generated method stub
  42. System.out.println("onLcationChanged");
  43. }

  44. //用戶關閉provider時呼叫此方法。
  45. @Override
  46. public void onProviderDisabled(String provider) {
  47. // TODO Auto-generated method stub
  48. System.out.println("onProviderDisabled");
  49. }

  50. //用戶啟動provider時呼叫此方法。
  51. @Override
  52. public void onProviderEnabled(String provider) {
  53. // TODO Auto-generated method stub
  54. System.out.println("onProviderEnabled");
  55. }
  56.      };
  57.     
  58.      for (String provider:providers){
  59.      //註冊目前的activity定期由provider通知位置更新。
  60.      locationManager.requestLocationUpdates(provider, 0, 1000, locationlistener);
  61.     
  62.      builder.append("\n").append(provider).append(":");
  63.      //回傳從provider最後所知的位置。
  64.      Location location = locationManager.getLastKnownLocation(provider);
  65.      if(location != null){
  66.      double lat = location.getLatitude();
  67.      double lng = location.getLongitude();
  68.      builder.append("(");
  69.      builder.append(lat);
  70.      builder.append(",");
  71.      builder.append(lng);
  72.      builder.append(")");
  73.     
  74.      }else{
  75.      builder.append("沒有訊息");
  76.      }
  77.      }
  78.      tv.setText(builder);
  79.     }
  80. }
3.記得在Androidmanifest文件中</application>後宣告權限,此宣告為採用GPS_PROVIDER。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
 4.在真實的設備當中要獲得定位服務需要有GPS硬體支援,但在開發或測試,使用模擬器是最方便的。將Eclipse切換到DDMS模式,在Emulator control中可以找到定位服務選項,選項可以手動發送經緯度、GPX、KML格式數據來測試定位服務。如果發現選項無法調整及發送(呈現灰色),原因可能是尚未選擇裝置,在Devices中選擇裝置。

          沒有留言:

          張貼留言