671 likes | 1k Views
經濟部工業局 Android 智慧型手機程式設計實務應用班 . Android Google Maps. 建國科技大學 資管系 饒瑞佶 2012/4. Google Maps. Reference : http://developer.android.com/resources/tutorials/views/hello-mapview.html. 取得 Google Map API. Step1: 先進入 JDK 目錄. Google Maps. 透過 keytool.exe 建立認證指紋 需要 debug_keystore 路徑. Google Maps.
E N D
經濟部工業局 Android智慧型手機程式設計實務應用班 AndroidGoogle Maps 建國科技大學 資管系 饒瑞佶 2012/4
Google Maps Reference: http://developer.android.com/resources/tutorials/views/hello-mapview.html • 取得Google Map API Step1:先進入JDK目錄
Google Maps • 透過keytool.exe建立認證指紋 • 需要debug_keystore路徑
Google Maps • debug_keystore路徑
Google Maps • 輸入keytool–list –alias androiddebugkey –keystore “C:\Documents and Settings\Administrator\.android\debug.keystore” –storepass android –keypass android
Google Maps • 產生認證指紋
Google Maps • 進入Google Map API Key申請頁面 • http://code.google.com/intl/zh-Tw/android/add-ons/google-apis/maps-api-signup.html 輸入認證指紋碼
Google Maps • 進入Google Map API Key申請頁面 • http://code.google.com/intl/zh-Tw/android/add-ons/google-apis/maps-api-signup.html
Google Maps • 產生出API金鑰 0O7bGO2vQxBc90bpYbJ8PnSjJapePPkrSGRvP3A <com.google.android.maps.MapViewandroid:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0O7bGO2vQxBc90bpYbJ8PnSjJapePPkrSGRvP3A" /> 加入main.xml
Google Maps main.xml
Google Maps • 建立新專案: HelloMaps 沒有Google APIs target
Google Maps • 如果沒有Google APIs target • 請選擇Available packages進行安裝
Google Maps • 安裝完Google APIs target畫面
Google Maps • 建立新專案: HelloMaps
顯示Google Maps地圖 加入 改成MapActivity 加入
Google Maps • AVD也要對應具備Google APIs功能
Google Maps • 增加 <uses-permission android:name="android.permission.INTERNET" /> • 在 <application> 裡頭增加 <uses-library android:name="com.google.android.maps" /> 資訊
在Google Maps上加標示 • 需要一個overlay class,做堆疊圖層之用
MapsOverlay import java.util.ArrayList; import android.app.AlertDialog; import android.content.Context; import android.graphics.drawable.Drawable; import com.google.android.maps.ItemizedOverlay; import com.google.android.maps.OverlayItem; public class MapsOverlay extends ItemizedOverlay<OverlayItem> { private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>(); private Context context; public MapsOverlay(DrawabledefaultMarker) { super(boundCenterBottom(defaultMarker)); } public MapsOverlay(DrawabledefaultMarker, Context context) { this(defaultMarker); this.context = context; } @Override protected OverlayItemcreateItem(inti) { return mapOverlays.get(i); } @Override public int size() { return mapOverlays.size(); } @Override protected booleanonTap(int index) { OverlayItem item = mapOverlays.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(context); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } public void addOverlay(OverlayItem overlay) { mapOverlays.add(overlay); this.populate(); } }
Main.xml 決定地圖能不能互動 決定View顯示方式 需要與layout_width或layout_height一起使用 加入這段來切換地圖種類
layout_weight 改變看看 改變看看 改變看看 改變看看 改變看看 改變看看
HelloMaps 給定一個座標(未來可以來自於GPS) 坐標 圖示 在給定座標上 加圖示 圖示按下時顯示的資訊 切換成衛星圖 切換成標準地圖
地址轉座標 try { inputaddress=(EditText)findViewById(R.id.editText1); Geocodergeocoder = new Geocoder(Maps1Activity.this, Locale.getDefault()); List<Address> geoResults = geocoder.getFromLocationName(inputaddress.getText().toString(), 5); while (geoResults.size()==0) { geoResults= geocoder.getFromLocationName(inputaddress.getText().toString(), 5); } if (geoResults.size()>0) { Address addr = geoResults.get(0); Double latitude = addr.getLatitude() * 1E6; Double longitude = addr.getLongitude() * 1E6; tv1.setText(latitude + "/" + longitude); showlocation((int) (addr.getLatitude() * 1E6),(int) (addr.getLongitude() * 1E6)); } } catch (Exception e) { tv1.setText("convert error"); }
AVD多數執行有問題, 最好使用手機測試 1600 Amphitheatre Parkway, Mountain View, CA Android 2.2
輸入地址 轉換地址成座標 座標 顯示座標點
利用Intent 玩Google Maps • 建立新專案: IntentMaps
顯示Google Maps地圖 要取消
利用Intent 玩Google Maps • AVD也要對應具備Google APIs功能
利用Intent 玩Google Maps • 增加 <uses-permission android:name="android.permission.INTERNET" />
更多Google Maps • Google MapsURI 格式:geo:latitude,longitude<< 沒有小圖示 geo:0,0?q=latitude,longitude << 出現圖示geo:latitude,longitude?z=zoom<<目前沒作用geo:0,0?q=my+street+address<< 出現有註冊的位置(my已取消)geo:0,0?q=business+near+city • Google StreetviewURI 格式:google.streetview:cbll=lat,lng&cbp=1,yaw,,pitch,zoom&mz=mapZoom Uri uri=Uri.parse("google.streetview:cbll=46.813812,-71.207378&cbp=1,99.56,,1,-5.27&mz=21"); 只支援美加地區
更多Google Maps • 顯示地圖Uri uri = Uri.parse("geo:38.899533,-77.036476"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); • 路徑規劃Uri uri = Uri.parse("http://maps.google.com/maps? f=d&saddr=startLat%20startLng&daddr=endLat%20endLng&hl=en"); Intent it = new Intent(Intent.ACTION_VIEW, uri); startActivity(it); //where startLat, startLng, endLat, endLng are a long with 6 decimals like: 50.123456
AndroidManiFest.xml 建立一個新專案HelloGPS • 需要開放以下權限 • <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> • <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
偵測是否開啟GPS //如果沒有開啟GPS--------------------- LocationManager status=(LocationManager)(this.getSystemService(Context.LOCATION_SERVICE)); if(status.isProviderEnabled(LocationManager.GPS_PROVIDER) || status.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){ }else{ // 到系統開啟GPS與WIFI服務的畫面 startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); } //--------------------如果沒有開啟GPS
Android GPS運作方式 • 取得LocationManager:判定是否有提供定位服務(硬體GPS或WIFI) • 建立LocationProvider,設定定位參數,並同時透過LocationManager取得座標(硬體軟體,GPS或是WiFi) • 設定LocationManager的Listener事件,偵測事件的改變 • MapController負責控制Google Maps
GPS訊號抓取主體 宣告 GPS訊號擷取主體 沒有GPS時觸發
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 /* Provider 初始化 */ getLocationPrivider(); /* 設定事件的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"); } 啟動GPS 如果沒有啟動 使用GPS服務, 將跳至設定畫面 要求GPS提供服務 設定GPS監聽 服務 如果有GPS訊號, 顯示到畫面上 TextViewtv_show_gps; private ProgressDialogMyDialog; private LocationManagermLocationManager; private String mLocationPrivider=""; private Location mLocation; 宣告物件
// 取得LocationProvider public void getLocationPrivider() { Criteria mCriteria01 = new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(true); //需要高度 mCriteria01.setBearingRequired(false); mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); mLocationPrivider = mLocationManager.getBestProvider(mCriteria01, true); mLocation = mLocationManager.getLastKnownLocation(mLocationPrivider); }
// 偵測位置改變 public final LocationListenermLocationListener = new LocationListener() { public void onLocationChanged(Location location) { //如果記錄進行中,就畫路線並更新移動距離 // 取得速度 double speed=location.getSpeed()/1000*60*60; //原單位是m/s double altitude = location.getAltitude(); tv_show_gps.setText("緯度:"+ formatgeo(location.getLatitude()) + " 經度:"+ formatgeo(location.getLongitude()) + " 海拔:"+ altitude + " m 速度:"+ formatspeed(speed) + "km/h"); MyDialog.dismiss();//結束進度 } public void onProviderDisabled(String provider) { } public void onProviderEnabled(String provider) { } public void onStatusChanged(String provider,intstatus,Bundle extras) { } }; //-----------------偵測位置改變
// 按BACK按鍵時關閉程式 @Override public void onBackPressed() { android.os.Process.killProcess(android.os.Process.myPid()); HelloGPSActivity.this.finish(); } // 產生定位中視窗 private void createCancelProgressDialog(String title, String message, String buttonText) { MyDialog = new ProgressDialog(this); MyDialog.setTitle(title); MyDialog.setMessage(message); MyDialog.setButton(buttonText, new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which){ // Use either finish() or return() to either close the activity or just the dialog android.os.Process.killProcess(android.os.Process.myPid()); HelloGPSActivity.this.finish(); return; } }); MyDialog.show();// 顯示進度 }
// format GPS座標的方法 public String formatgeo(double num) { NumberFormat formatter = new DecimalFormat("###.####"); String s=formatter.format(num); return s; } // format speed的方法 public String formatspeed(double num) { NumberFormat formatter = new DecimalFormat("###.##"); String s=formatter.format(num); return s; }
透過ddms送出模擬GPS座標到AVD <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
GPS • 取得GPS訊號後,將其顯示到Google Maps上,移動時,該點會移動 • 抹除舊圖層,放上新的點 • 顯示成一個動態路徑 • 將新的點加入原有圖層中,保留所有的點 • 將GPS座標記錄到SQLite資料庫,之後可以重新取回