1 / 92

Android GPS 軌跡紀錄器

Android GPS 軌跡紀錄器. 建國科技大學 資管系 饒瑞佶 2013/7 V1. Android GPS 軌跡紀錄器. 需要 Google Maps SQLite GPS Camera. Google Maps v2. Window Preferences. 取出 SHA1 碼. 下一步驟用. 進入申請頁面. https://developers.google.com/maps/documentation/javascript/tutorial#api_key. 需要登入 Google. Google Maps.

pross
Télécharger la présentation

Android GPS 軌跡紀錄器

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Android GPS軌跡紀錄器 建國科技大學 資管系 饒瑞佶 2013/7 V1

  2. Android GPS軌跡紀錄器 • 需要 • Google Maps • SQLite • GPS • Camera

  3. Google Mapsv2

  4. WindowPreferences 取出SHA1碼 下一步驟用

  5. 進入申請頁面 • https://developers.google.com/maps/documentation/javascript/tutorial#api_key 需要登入Google

  6. Google Maps • 進入Google Map API Key申請頁面 Reference:這裡有做法 https://developers.google.com/maps/documentation/android/start#creating_an_api_project

  7. 還有要開google Maps Android API v2 不是這個喔!

  8. Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project

  9. API Access

  10. 使用SHA1與package Name

  11. result

  12. 使用API Key Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project

  13. 首先將API key加入AndroidManifest.xml <meta-dataandroid:name="com.google.android.maps.v2.API_KEY"android:value="your_api_key"/>

  14. 加入使用權限 Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project

  15. 加入使用權限到AndroidManifest.xml 改成自己的package name <permissionandroid:name="ctu.rcjao.helloandroid.permission.MAPS_RECEIVE" android:protectionLevel="signature"/> <uses-permission android:name="ctu.rcjao.helloandroid.permission.MAPS_RECEIVE"/> 改成自己的package name

  16. 加入使用權限到AndroidManifest.xml Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project

  17. Use permission <uses-permissionandroid:name="android.permission.INTERNET"/><uses-permissionandroid:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroid:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/><!-- The following two permissions are not required to useGoogle Maps Android API v2, but are recommended. --><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION"/>

  18. 再加入use-feature

  19. uses-feature 放在<application>之外 <uses-featureandroid:glEsVersion="0x00020000"android:required="true"/>

  20. 最後加入地圖 Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project

  21. Xml檔案 加入下列code <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/> 這裡我們做了更改,讓其可以支援2.2板以上

  22. JAVA程式 publicclass Map extends FragmentActivity { @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); } }

  23. 執行 結果不能跑,有錯誤!

  24. 打開SDK Manager,找到Extras並安裝Android Support Library及 Google Play Services兩個項目

  25. 匯入Google Play Services函式庫File > Import > Android > Existing Android Code Into Workspace 路徑: android-sdk\extras\google\google_play_services\libproject\google-play-services_lib

  26. 匯入完成

  27. 專案要加入這個library

  28. 執行 • 需要實體手機 • 過程中可能需要更新Google Play • 模擬器的顯示方式請見上一期講義

  29. Google Maps 實體手機

  30. 加入按鈕選項與顯示框 <HorizontalScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:scrollbars="horizontal" android:id="@+id/hscr1"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/myButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始記錄" /> <Button android:id="@+id/myButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="結束記錄" /> <Button android:id="@+id/myButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的路徑" /> <Button android:id="@+id/myButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="重新定位" /> </LinearLayout> </HorizontalScrollView> <TextView android:id="@+id/showgeo" android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content"/>

  31. SQLite

  32. Android vs. SQLite • 透過SQLiteOpenHelper類別來操作 • 建立資料庫 (建構子) • 建立資料表 (onCreate方法) • 更新資料庫 (onUpgrade方法) • 從SQLiteOpenHelper類別建立物件,同時配合SQL指令來操作資料庫

  33. Android vs. SQLite運作流程 執行建構子 是否有資料庫? 無 建立資料庫 有 進入onCreate 建立資料表 檢查版本 開啟或更新資料庫 使用資料庫

  34. 實作SQLiteOpenHelper類別

  35. Extends SQLiteOpenHelper • 產生新class-MySQLite

  36. 繼承SQLIiteOpenHelper

  37. 尚需要加入建構子

  38. 建構子

  39. 建立資料庫與資料表

  40. 這裡有修正 publicclass MySQLite extends SQLiteOpenHelper { SQLiteDatabase db; // 資料庫物件 public MySQLite(Context context) { super(context, "/sdcard/gpslogger.db", null, 1); db=this.getWritableDatabase(); //將db對應到 /sdcard/gpslogger.db } @Override publicvoid onCreate(SQLiteDatabase db) { // 建立資料表 String DATABASE_story = "story"; //故事 String DATABASE_CREATE_story = "create table " + DATABASE_story + "(_id text,createday text,title text,desc text);"; db.execSQL(DATABASE_CREATE_story); //新增故事資料表 } @Override publicvoid onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO自動產生的方法 Stub } }

  41. 使用資料庫

  42. 透過class MySQLite建立資料

  43. GPS

  44. 加入對應的xml 用以顯示GPS座標

  45. AndroidManiFest.xml 前面在Google Maps已經加入 • 需要開放以下權限 • <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> • <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>

  46. Main.java

  47. @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //建立dbHelper物件 dbHelper = new MySQLite(this); //判斷是否可以上網 ConnectivityManager cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo info = cManager.getActiveNetworkInfo(); if (info == null || !info.isAvailable()){ //不能上網 new AlertDialog.Builder(Main.this) .setTitle("系統訊息") .setMessage("目前無法上網,所以無法使用系統!") .setPositiveButton("確認",new DialogInterface.OnClickListener() { publicvoid onClick(DialogInterface dialog, int which) { findviews_nonet(); } }) .show(); }else{ tv_show_gps=(TextView)findViewById(R.id.showgeo); createCancelProgressDialog("定位中","定位中..請稍待!","取消"); try{ //如果沒有開啟GPS或WiFi--------------------- mLocationManager =(LocationManager)(this.getSystemService(Context.LOCATION_SERVICE)); if(mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ }else{ // 到系統開啟GPS與WIFI服務的畫面 startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } //--------------------如果沒有開啟GPS mLocationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); // Provider 初始化 getLocationPrivider(); // 設定GPS的Listener mLocationManager.requestLocationUpdates(mLocationPrivider, 2000, 0, mLocationListener);

  48. if(mLocation!=null) //第一次顯示 { // 取得速度 double speed=mLocation.getSpeed()/1000*60*60; //原單位是m/s double altitude = mLocation.getAltitude(); tv_show_gps.setText("緯度:"+ formatgeo(mLocation.getLatitude()) + " 經度:"+ formatgeo(mLocation.getLongitude()) + " 海拔:"+ altitude + " m 速度:"+ formatspeed(speed) + "km/h"); } }catch(Exception e){ new AlertDialog.Builder(Main.this) .setTitle("系統訊息") .setMessage("無法取得GPS座標") .setPositiveButton("確認",new DialogInterface.OnClickListener() { publicvoid onClick(DialogInterface dialog, int which) { MyDialog.dismiss(); } }) .show(); } } }

  49. // 產生定位中視窗 privatevoidcreateCancelProgressDialog(String title, String message, String buttonText) { MyDialog = newProgressDialog(this); MyDialog.setTitle(title); MyDialog.setMessage(message); MyDialog.setButton(buttonText, newDialogInterface.OnClickListener(){ publicvoidonClick(DialogInterface dialog, int which){ } }); MyDialog.show(); // 顯示進度 } // 取得LocationProvider publicvoidgetLocationPrivider() { Criteria mCriteria01 = new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(true); //需要高度 mCriteria01.setBearingRequired(false); mCriteria01.setSpeedRequired(true);//速度 mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); mLocationPrivider = mLocationManager.getBestProvider(mCriteria01, true); mLocation = mLocationManager.getLastKnownLocation(mLocationPrivider); }

More Related