920 likes | 930 Views
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.
E N D
Android GPS軌跡紀錄器 建國科技大學 資管系 饒瑞佶 2013/7 V1
Android GPS軌跡紀錄器 • 需要 • Google Maps • SQLite • GPS • Camera
WindowPreferences 取出SHA1碼 下一步驟用
進入申請頁面 • https://developers.google.com/maps/documentation/javascript/tutorial#api_key 需要登入Google
Google Maps • 進入Google Map API Key申請頁面 Reference:這裡有做法 https://developers.google.com/maps/documentation/android/start#creating_an_api_project
Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project
使用API Key Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project
首先將API key加入AndroidManifest.xml <meta-dataandroid:name="com.google.android.maps.v2.API_KEY"android:value="your_api_key"/>
加入使用權限 Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project
加入使用權限到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
加入使用權限到AndroidManifest.xml Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project
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"/>
uses-feature 放在<application>之外 <uses-featureandroid:glEsVersion="0x00020000"android:required="true"/>
最後加入地圖 Reference: https://developers.google.com/maps/documentation/android/start#creating_an_api_project
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板以上
JAVA程式 publicclass Map extends FragmentActivity { @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); } }
執行 結果不能跑,有錯誤!
打開SDK Manager,找到Extras並安裝Android Support Library及 Google Play Services兩個項目
匯入Google Play Services函式庫File > Import > Android > Existing Android Code Into Workspace 路徑: android-sdk\extras\google\google_play_services\libproject\google-play-services_lib
執行 • 需要實體手機 • 過程中可能需要更新Google Play • 模擬器的顯示方式請見上一期講義
Google Maps 實體手機
加入按鈕選項與顯示框 <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"/>
Android vs. SQLite • 透過SQLiteOpenHelper類別來操作 • 建立資料庫 (建構子) • 建立資料表 (onCreate方法) • 更新資料庫 (onUpgrade方法) • 從SQLiteOpenHelper類別建立物件,同時配合SQL指令來操作資料庫
Android vs. SQLite運作流程 執行建構子 是否有資料庫? 無 建立資料庫 有 進入onCreate 建立資料表 檢查版本 開啟或更新資料庫 使用資料庫
Extends SQLiteOpenHelper • 產生新class-MySQLite
這裡有修正 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 } }
加入對應的xml 用以顯示GPS座標
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>
@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);
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(); } } }
// 產生定位中視窗 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); }