在android開發中地圖和定位是很多軟體不可或缺的內容,這些特色功能也給人們帶來了很多方便。
首先介紹一下地圖包中的主要類:
MapController : 主要控制地圖移動,伸縮,以某個GPS座標為中心,控制MapView中的view組件,管理Overlay,提供View的準系統。使用多種地圖模式(地圖模式(某些城市可即時對交通狀況進行更新),衛星模式,街景模式)來查看Google
Map。常用方法:animateTo(GeoPoint point) setCenter(GeoPoint point) setZoom(int zoomLevel) 等。
Mapview : 是用來顯示地圖的view, 它派生自android.view.ViewGroup。當MapView獲得焦點,可以控制地圖的移動和縮放。地圖可以以不同的形式來顯示出來,如街景模式,衛星模式等,通過setSatellite(boolean) setTraffic(boolean), setStreetView(boolean) 方法。
Overlay :
是覆蓋到MapView的最上層,可以擴充其ondraw介面,自訂在MapView中顯示一些自己的東西。MapView通過MapView.getOverlays()對Overlay進行管理。
Projection :MapView中GPS座標與裝置座標的轉換(GeoPoint和Point)。
定位系統包中的主要類:
LocationManager:本類提供訪問定位服務的功能,也提供擷取最佳定位提供者的功能。另外,臨近警報功能也可以藉助該類來實現。
LocationProvider:該類是定位提供者的抽象類別。定位提供者具備周期性報告裝置地理位置的功能。
LocationListener:提供定位資訊發生改變時的回調功能。必須事先在定位管理器中註冊監聽器對象。
Criteria:該類使得應用能夠通過在LocationProvider中設定的屬性來選擇合適的定位提供者。
Geocoder:用於處理地理編碼和反向地理編碼的類。地理編碼是指將地址或其他描述轉變為經度和緯度,反向地理編碼則是將經度和緯度轉變為地址或描述語言,其中包含了兩個建構函式,需要傳入經度和緯度的座標。getFromLocation方法可以得到一組關於地址的數組。
下面開始地圖定位執行個體的開發,在開發地圖前需要 擷取Android
地圖 API
密鑰 網上有很多資料,這裡就不再複述。
首先要在manifest.xml中設定全相應的許可權和maps庫:
<application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MyMapActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <uses-library android:name="com.google.android.maps" /> </application> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
在上面我標紅的千萬不要忘記。
layout下的main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <com.google.android.maps.MapView android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="008uu0x2a7GWlK2LzCW872afBAPLhJ-U2R26Wgw" /></LinearLayout>
下面是核心代碼,重要的地方我做了注釋:
public class MyMapActivity extends MapActivity { /** Called when the activity is first created. */private MapController mapController;private MapView mapView;private MyOverLay myOverLay; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); LocationManager locationManager=(LocationManager) getSystemService(Context.LOCATION_SERVICE); mapView=(MapView) this.findViewById(R.id.mapview); //設定交通模式 mapView.setTraffic(true); //設定衛星模式 mapView.setSatellite(false); //設定街景模式 mapView.setStreetView(false); //設定縮放控制 mapView.setBuiltInZoomControls(true); mapView.setClickable(true); mapView.setEnabled(true); //得到MapController執行個體 mapController=mapView.getController(); mapController.setZoom(15); myOverLay=new MyOverLay(); List<Overlay> overLays=mapView.getOverlays(); overLays.add(myOverLay); Criteria criteria=new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired(false); criteria.setBearingRequired(false); criteria.setCostAllowed(false); criteria.setPowerRequirement(Criteria.POWER_LOW); //取得效果最好的Criteria String provider=locationManager.getBestProvider(criteria, true); //得到Location Location location=locationManager.getLastKnownLocation(provider); updateWithLocation(location); //註冊一個周期性的更新,3秒一次 locationManager.requestLocationUpdates(provider, 3000, 0, locationListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, 1, 1, "交通模式"); menu.add(0,2,2,"衛星模式"); menu.add(0,3,3,"街景模式"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub super.onOptionsItemSelected(item); switch (item.getItemId()) {case 1://交通模式mapView.setTraffic(true);mapView.setSatellite(false);mapView.setStreetView(false);break;case 2://衛星模式mapView.setSatellite(true);mapView.setStreetView(false);mapView.setTraffic(false);break;case 3://街景模式mapView.setStreetView(true);mapView.setTraffic(false);mapView.setSatellite(false);break;default:mapView.setTraffic(true);mapView.setSatellite(false);mapView.setStreetView(false);break;} return true; } private void updateWithLocation(Location location){ if(location!=null){ //為繪製類設定座標 myOverLay.setLocation(location); GeoPoint geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)); //定位到指定的座標 mapController.animateTo(geoPoint); mapController.setZoom(15); } } private final LocationListener locationListener=new LocationListener() {@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}//當座標改變時出發此函數@Overridepublic void onLocationChanged(Location location) {// TODO Auto-generated method stubupdateWithLocation(location);}}; class MyOverLay extends Overlay{ private Location location; public void setLocation(Location location){ this.location=location; } @Override public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { // TODO Auto-generated method stub super.draw(canvas, mapView, shadow); Paint paint=new Paint(); Point myScreen=new Point(); //將經緯度換成實際螢幕的座標。 GeoPoint geoPoint=new GeoPoint((int)(location.getLatitude()*1E6), (int)(location.getLongitude()*1E6)); mapView.getProjection().toPixels(geoPoint, myScreen); paint.setStrokeWidth(1); paint.setARGB(255, 255, 0, 0); paint.setStyle(Paint.Style.STROKE); Bitmap bmp=BitmapFactory.decodeResource(getResources(), R.drawable.mypicture); //把這張圖片畫到相應的位置。 canvas.drawBitmap(bmp, myScreen.x, myScreen.y,paint); canvas.drawText("天堂沒有路", myScreen.x, myScreen.y, paint); return true; } }@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode == KeyEvent.KEYCODE_BACK) {AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setMessage("你確定退出嗎?").setCancelable(false).setPositiveButton("確定",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int id) {MyMapActivity.this.finish();android.os.Process.killProcess(android.os.Process.myPid()); android.os.Process.killProcess(android.os.Process.myTid()); android.os.Process.killProcess(android.os.Process.myUid());}}).setNegativeButton("返回",new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog,int id) {dialog.cancel();}});AlertDialog alert = builder.create();alert.show();return true;}return super.onKeyDown(keyCode, event);}}
接下來看一下運行後效果:
可以放大縮小:
可是使用menu鍵,切換不同的模式:
上面是切換到了衛星模式。由於地圖需要耗費大量的網路資源,如果網路比較慢的話會等待很長時間。