下圖Google MapView程式範例執行結果圖,其程式使用Google Map View元件
所撰寫的程式。(與Google Map相似)
圖一-遊戲畫面
以下為Google MapView範例程式碼:
專案下載:MapViewDemo.rar
主程式: MapViewDemo.java
package ccc.MapViewDemo; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; public class MapViewDemo extends MapActivity { // GoogleMapView元件 MapView mapView; // GoogleMapView控制器 MapController mapController; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // GoogleMapAPI Key String ApiKey = "03rGqE_CW0JtuagXBUsB8Zxijve-Y131pYz1jqA"; mapView = new MapView(this,ApiKey); // 設定衛星圖 mapView.setSatellite(true); mapController = mapView.getController(); // 設定經緯度中心、縮放 GeoPoint point = new GeoPoint(24448541, 118322206); mapController.setCenter(point); mapController.setZoom(19); setContentView(mapView); } // 選單放大跟縮小索引 protected static final int MENU_ZoomIn = Menu.FIRST; protected static final int MENU_ZoomOut = Menu.FIRST + 1; // 選單初始化 @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, MENU_ZoomIn, 0, "放大"); menu.add(0, MENU_ZoomOut, 0, "縮小"); return super.onCreateOptionsMenu(menu); }; // 選單項目選擇 @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ZoomIn: mapController.zoomIn(); break; case MENU_ZoomOut: mapController.zoomOut(); break; } return super.onOptionsItemSelected(item); } // 觸控事件 @Override public boolean onTouchEvent(MotionEvent event) { int pointCount = event.getHistorySize(); if (pointCount > 0) { float dx = event.getHistoricalX(pointCount - 1) - event.getHistoricalX(0); float dy = event.getHistoricalY(pointCount - 1) - event.getHistoricalY(0); mapController.scrollBy((int) -dx, (int) -dy); } return true; } // 鍵盤事件 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { mapController.scrollBy(-10, 0); } if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { mapController.scrollBy(10, 0); } if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { mapController.scrollBy(0, -10); } if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { mapController.scrollBy(0, 10); } return super.onKeyDown(keyCode, event); } @Override protected boolean isRouteDisplayed() { return false; } }
AndroidManifest.xml為使用權限:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="ccc.MapViewDemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MapViewDemo" android:label="@string/app_name"> <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.ACCESS_COARSE_LOCATION" /--> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="4" /> </manifest>
金鑰申請:
此程式的google APIs金鑰申請到網頁Android Maps API Key Signup申請,MD5取得如下圖2-6步驟在cmd中先到Java\jre6\bin目錄下輸入keytool -list -keystore "debug.keystore所在位置(位置在圖2-7中紅色框框中)"之後輸入密碼為"android"就能取得MD5,輸入MD5申請後結果會如圖2-8取得金鑰到此步驟模擬器就能使用金鑰,而手機的金鑰安裝檔為圖2-9到圖2-13其中2-10的Location為debug.keystore所在位置、password輸入"android",圖2-11中的password如同上述,圖2-12為安裝檔存放位置。
圖2-6
圖2-7
圖2-8
圖2-9
圖2-10
圖2-11
圖2-12
程式手機執行影片
參考文獻
http://developer.android.com/resources/tutorials/views/hello-mapview.html
http://about-android.blogspot.com/2010/02/map-implementation.html
http://about-android.blogspot.com/2010/03/sample-google-map-driving-direction.html
http://jjnnykimo.pixnet.net/blog/post/29807920
http://q-and-g.blogspot.com/2009/07/androidkeystore.html
http://blog.yslifes.com/archives/671
Post preview:
Close preview