180 likes | 391 Views
期末專題報告. MyPosition - 我在何處報你知. 組員 : 曾哲浩 蔡承翰. GPS GOOGLE 地圖. protected void onCreate(Bundle icicle) { // TODO Auto-generated method stub super.onCreate(icicle); setContentView(R.layout. main); mTextView01 = (TextView)findViewById(R.id. myTextView1);
E N D
期末專題報告 MyPosition - 我在何處報你知 組員:曾哲浩 蔡承翰
protected void onCreate(Bundle icicle) { // TODO Auto-generated method stub super.onCreate(icicle); setContentView(R.layout.main); mTextView01 = (TextView)findViewById(R.id.myTextView1); /* 建立MapView物件 */ mMapView01 = (MapView)findViewById(R.id.myMapView1); /* 建立LocationManager物件取得系統LOCATION服務 */ mLocationManager01 = (LocationManager)getSystemService(Context.LOCATION_SERVICE); /* 第一次執行向Location Provider取得Location */ mLocation01 = getLocationPrivider(mLocationManager01); if(mLocation01!=null) { processLocationUpdated(mLocation01); } else { mTextView01.setText ( getResources().getText(R.string.str_err_location).toString() ); }
/* 建立LocationManager物件,監聽Location變更時事件,更新MapView */ mLocationManager01.requestLocationUpdates(strLocationPrivider, 2000, 10, mLocationListener01); } public final LocationListener mLocationListener01 = new LocationListener() { @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub /* 當手機收到位置變更時,將location傳入取得地理座標 */ processLocationUpdated(location); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub /* 當Provider已離開服務範圍時 */ }
@Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub } @Override public void onStatusChanged(String provider, int status, Bundle extras) {} }; public String getAddressbyGeoPoint(GeoPoint gp) { String strReturn = ""; try { /* 當GeoPoint不等於null */ if (gp != null) { /* 建立Geocoder物件 */ Geocoder gc = new Geocoder(EX09_05.this, Locale.getDefault()); /* 取出地理座標經緯度 */ double geoLatitude = (int)gp.getLatitudeE6()/1E6; double geoLongitude = (int)gp.getLongitudeE6()/1E6;
/* 自經緯度取得地址(可能有多行地址) */ List<Address> lstAddress = gc.getFromLocation(geoLatitude, geoLongitude, 1); StringBuilder sb = new StringBuilder(); /* 判斷地址是否為多行 */ if (lstAddress.size() > 0) { Address adsLocation = lstAddress.get(0); for (int i = 0; i < adsLocation.getMaxAddressLineIndex(); i++) { sb.append(adsLocation.getAddressLine(i)).append("\n"); } sb.append(adsLocation.getLocality()).append("\n"); sb.append(adsLocation.getPostalCode()).append("\n"); sb.append(adsLocation.getCountryName()); } /* 將擷取到的地址,組合後放在StringBuilder物件中輸出用 */ strReturn = sb.toString(); } }
catch(Exception e) { e.printStackTrace(); } return strReturn; } public Location getLocationPrivider(LocationManager lm) { Location retLocation = null; try { Criteria mCriteria01 = new Criteria(); mCriteria01.setAccuracy(Criteria.ACCURACY_FINE); mCriteria01.setAltitudeRequired(false); mCriteria01.setBearingRequired(false); mCriteria01.setCostAllowed(true); mCriteria01.setPowerRequirement(Criteria.POWER_LOW); strLocationPrivider = lm.getBestProvider(mCriteria01, true); retLocation = lm.getLastKnownLocation(strLocationPrivider); }
catch(Exception e) { mTextView01.setText(e.toString()); e.printStackTrace(); } return retLocation; } private GeoPoint getGeoByLocation(Location location) { GeoPoint gp = null; try {/* 當Location存在 */ if (location != null) { double geoLatitude = location.getLatitude()*1E6; double geoLongitude = location.getLongitude()*1E6; gp = new GeoPoint((int) geoLatitude, (int) geoLongitude); } } catch(Exception e) { e.printStackTrace(); }return gp;}
public static void refreshMapViewByGeoPoint(GeoPoint gp, MapView mv, int zoomLevel, boolean bIfSatellite) { try { mv.displayZoomControls(true); /* 取得MapView的MapController */ MapController mc = mv.getController(); /* 移至該地理座標位址 */ mc.animateTo(gp); /* 放大地圖層級 */ mc.setZoom(zoomLevel); /* 延伸學習:取得MapView的最大放大層級 */ //mv.getMaxZoomLevel() /* 設定MapView的顯示選項(衛星、街道)*/ if(bIfSatellite) {mv.setSatellite(true); mv.setStreetView(true); } else {mv.setSatellite(false);}} catch(Exception e) {e.printStackTrace(); } }
/* 當手機收到位置變更時,將location傳入更新當下GeoPoint及MapView */ private void processLocationUpdated(Location location) { /* 傳入Location物件,取得GeoPoint地理座標 */ currentGeoPoint = getGeoByLocation(location); /* 更新MapView顯示Google Map */ refreshMapViewByGeoPoint(currentGeoPoint, mMapView01, intZoomLevel, true); mTextView01.setText (getResources().getText(R.string.str_my_location).toString()+"\n"+ /* 延伸學習:取出GPS地理座標: */ getResources().getText(R.string.str_longitude).toString()+ String.valueOf((int)currentGeoPoint.getLongitudeE6()/1E6)+"\n"+ getResources().getText(R.string.str_latitude).toString()+ String.valueOf((int)currentGeoPoint.getLatitudeE6()/1E6)+"\n"+ getAddressbyGeoPoint(currentGeoPoint) ); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
LAYOUT_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/white" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/myTextView1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="@drawable/blue" android:text="@string/hello"/> <com.google.android.maps.MapView android:id="@+id/myMapView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true" android:clickable="true" android:apiKey="086L9PGjlb6XQprGQVXCe1NvaXl6NJC5g0fh4Aw" /> </LinearLayout>
Color.xml Strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="darkgray">#808080</drawable> <drawable name="white">#FFFFFF</drawable> <drawable name="blue">#0000FF</drawable> </resources> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, EX09_05</string> <string name="app_name">EX09_05</string> <string name="str_longitude">經度(Longitude):</string> <string name="str_latitude">緯度(Latitude):</string> <string name="str_err_address">地址不存在</string> <string name="str_err_location">無法取得Location</string> <string name="str_my_location">目前所在地址:</string> </resources>
曾經遇到跟別人在電話裡怎麼說,都說不清楚自己的確切位置嗎?曾經遇到跟別人在電話裡怎麼說,都說不清楚自己的確切位置嗎?
是一款結合Google Maps來定位出目前手機的位置,並且轉換成各種資訊分享出去的小程式。
除了利用GPS、Wi-Fi或是基地台定位出目前位置,還能直接在地圖上畫出路徑,方便告知對方怎麼到達。可以擷圖存檔,並且用簡訊或是其他方式分享。
如果是透過簡訊,可以選擇發送Google Maps或者圖檔的連結。