Android Location-based應用開發分享

5,850 views

Published on

2008/05/17 TWJUG slides

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
5,850
On SlideShare
0
From Embeds
0
Number of Embeds
526
Actions
Shares
0
Downloads
106
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Android Location-based應用開發分享

  1. 1. Location-based 應用開發分享 koji
  2. 2. Agenda <ul><li>一個基本的 Android App </li></ul><ul><li>前置工作 - 製造假路徑 </li></ul><ul><li>寫一個 Location-based 的程式所需的 API </li></ul><ul><li>落選作品介紹 </li></ul>
  3. 3. 一個基本的 Android App <ul><li>public class BomberMan extends Activity { </li></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public void onCreate(Bundle icicle) { </li></ul></ul><ul><ul><ul><li>super .onCreate(icicle); </li></ul></ul></ul><ul><ul><ul><li>setContentView(R.layout. main ); </li></ul></ul></ul><ul><ul><ul><li>Button joinButton = (Button) findViewById(R.id. join ); </li></ul></ul></ul><ul><ul><ul><li>joinButton.setOnClickListener( new View.OnClickListener() { </li></ul></ul></ul><ul><ul><ul><li>public void onClick(View view) { </li></ul></ul></ul><ul><ul><ul><ul><li>startActivity( new Intent(BomberMan. this , UserConfig. class )); </li></ul></ul></ul></ul><ul><ul><ul><li>}}); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul>
  4. 4. 一個基本的 Android App <ul><li><? xml version = &quot;1.0&quot; encoding = &quot;utf-8&quot; ?> </li></ul><ul><li>< LinearLayout ... > </li></ul><ul><ul><li>< RelativeLayout ... > </li></ul></ul><ul><ul><ul><li>< Button android:id = &quot;@+id/join&quot; ... /> </li></ul></ul></ul><ul><ul><li></ RelativeLayout > </li></ul></ul><ul><li></ LinearLayout > </li></ul>
  5. 5. 前置工作 - 製造假路徑 <ul><li>現在的模擬器有個資料夾 </li></ul><ul><ul><li>/data/misc/location/gps </li></ul></ul>
  6. 6. 前置工作 - 製造假路徑 <ul><li>底下可以放四種檔案 ( 越上面越優先 ) </li></ul><ul><ul><li>Class </li></ul></ul><ul><ul><li>Kml </li></ul></ul><ul><ul><ul><li>Google Earth </li></ul></ul></ul><ul><ul><li>nmea </li></ul></ul><ul><ul><ul><li>NMEA 0183 </li></ul></ul></ul><ul><ul><li>Track </li></ul></ul><ul><ul><ul><li><time> <longitude> <latitude> <altitude> </li></ul></ul></ul>
  7. 7. 前置工作 - 設定存取權限 <ul><li>< uses-permission </li></ul><ul><li>android:name = &quot;android.permission. </li></ul><ul><ul><li>ACCESS_LOCATION&quot; /> </li></ul></ul><ul><li>< uses-permission </li></ul><ul><li>android:name = &quot;android.permission. </li></ul><ul><ul><li>ACCESS_GPS&quot; /> </li></ul></ul>
  8. 8. 所需的 API- 取得座標 <ul><li>LocationManager locationManager = (LocationManager) getSystemService(Context. LOCATION_SERVICE ); </li></ul><ul><li>LocationProvider myProvider = null ; </li></ul><ul><li>for (LocationProvider provider : locationManager.getProviders()) { </li></ul><ul><ul><li>myProvider = provider; </li></ul></ul><ul><li>} </li></ul><ul><li>final Location location = locationManager. </li></ul><ul><ul><li>getCurrentLocation(myProvider.getName()); </li></ul></ul>
  9. 9. 所需的 API-Google Map <ul><li>public class BomberManMap extends MapActivity{ </li></ul><ul><li>..... </li></ul><ul><ul><li>this . mapView = new MapView( this ) { </li></ul></ul><ul><ul><li>@Override </li></ul></ul><ul><ul><li>public boolean onTouchEvent(MotionEvent ev) { </li></ul></ul><ul><ul><ul><li>return false ;}}; </li></ul></ul></ul><ul><ul><li>this .setContentView( mapView ); </li></ul></ul><ul><li>} </li></ul>
  10. 10. 所需的 API- 註冊 listener <ul><li>寫一個IntenetReceiver </li></ul><ul><li>class MyIntentReceiver extends IntentReceiver { </li></ul><ul><ul><li>@Override public void onReceiveIntent(Context context, Intent intent) { </li></ul></ul><ul><ul><ul><li>Location location = (Location) intent.getExtras().get( &quot;location&quot; ); </li></ul></ul></ul><ul><ul><ul><li>double lat = location.getLatitude() * 1E6; </li></ul></ul></ul><ul><ul><ul><li>double lon = location.getLongitude() * 1E6; </li></ul></ul></ul><ul><ul><ul><li>final Point point = new Point(( int ) lat, ( int ) lon); </li></ul></ul></ul><ul><ul><ul><li>mapView .getController().centerMapTo(point, true ); </li></ul></ul></ul><ul><ul><ul><li>mapView .invalidate(); </li></ul></ul></ul><ul><ul><li>}} </li></ul></ul>
  11. 11. 所需的 API- 註冊 listener <ul><li>註冊 </li></ul><ul><li>String MY_LOCATION_CHANGED_ACTION = new String( &quot;android.intent.action.LOCATION_CHANGED&quot; ); </li></ul><ul><li>IntentFilter myIntentFilter = new IntentFilter( MY_LOCATION_CHANGED_ACTION ); </li></ul><ul><li>..... </li></ul>
  12. 12. 所需的 API- 註冊 listener <ul><li>註冊 </li></ul><ul><li>this . locationManager .requestUpdates( </li></ul><ul><ul><li>myProvider, // 剛剛的 provider </li></ul></ul><ul><ul><li>MINIMUM_TIME_BETWEEN_UPDATE , // 最少所需更新的時間 </li></ul></ul><ul><ul><li>MINIMUM_DISTANCECHANGE_FOR_UPDATE , // 所需更新的距離 </li></ul></ul><ul><ul><li>new Intent( MY_LOCATION_CHANGED_ACTION )); </li></ul></ul><ul><ul><li>... </li></ul></ul><ul><li>this .registerReceiver( this . myIntentReceiver , this . myIntentFilter ); </li></ul>
  13. 13. 落選作品介紹 <ul><li>搭便車系統 </li></ul><ul><li>實體炸彈超人 </li></ul>

×