【android學習之十六】——特色功能1:GoogleMap手機地圖

來源:互聯網
上載者:User

 聲明:學習的書籍《Android應用開發揭秘》,這裡記錄學習該書籍的日誌,引用的相關代碼與總結描述,沒有商業的用途,完全是自我學習的一個記錄,剛剛學習不可避免會出現很多問題,若是有錯誤還請大家多多批評。

一、GoogleMap手機地圖

             GoogleMap是我們平時在PC上找地址的一個重要工具,同樣在android系統中,也提供了手機上得Map實現。

 

第一步、申請使用Android Map的認認證

     下面我們就來實現手機版GoogleMap,需要使用到Android Map API,在使用Android Map API之前,我們需要申請一個Android Map API Key,必須要準備Google的帳號,Gmail帳號也可以。申請步驟如下:

     步驟1、找到你的debug.keystore檔案

     該檔案的路徑一般為:

     C:\Documents and Settings\Administrator.NJC-11-0000(目前使用者)\.android\debug.keystore。當然最簡單的方式是,開啟Eclipse,選擇Windows->Preference->Android->Build,其中Default debug keystore的值便是debug.keystore的路徑。

     步驟2、取得debug.keystore的MD5值

     命令提示字元下進入,debug.keystore檔案所在的路徑,執行:keytool –list –keystore debug.keystore 。這時會提示你輸入密碼。輸入預設密碼:android,機會獲得該MD5值。如:

     步驟3、申請Android Map的API Key

     使用瀏覽器登陸網址:

http://code.google.com/intl/zh-CN/android/maps-api-signup.html,登陸Google帳號,並且在申請頁面上輸入步驟2中得到的MD5認證指紋,點擊”Generate API Key”來擷取我們得到的API Key

如,頁面出現亂碼,但是不影響。

 

    以上三個步驟就擷取的對應的API Key值,這裡儲存一下,以便後面程式中使用,相當於Google給你這個賬戶使用Map的一個認認證。

 

第二步、建立基於Google API是的AVD,以及建立基於Google APIs的工程

 

     這一步很關鍵,因為我們之前項目啟動並執行AVD的target是Anrdoid 2.2,而現在我們建立AVD的target是Google APIs

這就是建立之後的AVD列表,

同樣建立Android項目時,也要選擇對應Google APIs項目

 

肯定大家會問為什麼要這樣,或者與之前選擇的Android 2.2有什麼差異,這裡需要說明一下,Google API類型會包含maps.jar這個google 地圖相關jar包,這樣在項目內部或者運行在AVD中才會有這個功能的支援,否則會報錯。

 

【遇到問題1】我自己嘗試了一下,到我本地安裝目錄下:D:\android-sdk-windows\add-ons\addon_google_apis_google_inc_8\libs找到maps.jar

然後加進Android 2.2這種項目類型裡面進行過開發,安裝與運行,安裝到AVD中時會報錯。

[2011-11-16 09:57:12 - GoogleMaps] Installing GoogleMaps.apk...[2011-11-16 09:57:14 - GoogleMaps] Installation error: INSTALL_FAILED_MISSING_SHARED_LIBRARY[2011-11-16 09:57:14 - GoogleMaps] Please check logcat output for more details.[2011-11-16 09:57:14 - GoogleMaps] Launch canceled!

 

第三步,使用Google Map API完成執行個體開發

 

【必備學習】在真正完成執行個體之前,我們需要對com.google.android.maps包下的幾個關鍵類瞭解一下,其他作用:

MapActivity,MapView :要是想完成GoogleMap的顯示與相關操作,這個兩個類必須要用到,MapView是用顯示地圖的View組建,派生自ViewGroup。而必須和MapActivity配合使用,而且只能被MapActivity建立,這是因為MapView需要通過後台線程串連網路或者檔案系統,而MapActivity是一個抽象類別提供了底層網路的串連,任何想要顯示的MapView的activity都需要派生自MapActivity。

MapController: 用於控制地圖的移動、縮放等

Overlay:可顯示於地圖之上的可繪製的對象。

GeoPoint:包含經緯度位置的對象

 

執行個體分析:

由於我們要使用Google Map API ,所以必須在AndroidManifest.xml檔案中配相關使用Map的library以及相關許可權:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.jercy.android.GoogleMap" android:versionCode="1"  android:versionName="1.0" >    <!-- 使用許可權 ,不可少--><uses-permission android:name="android.permission.INTERNET" />    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" >        <!-- 使用Library說明,不可少 -->        <uses-library android:name="com.google.android.maps"/>        <activity android:label="@string/app_name" android:name=".GoogleMapActivity" >            <intent-filter >                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

對應main.xml的UI配置比較簡單,但是注意這裡就是我們申請的APIKey了

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <com.google.android.maps.MapView        android:id="@+id/MapView01"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:apiKey="0l3YQSTUctypL1EJVBlhjJi4uol-4zK2ZuJB0wg"    /></RelativeLayout>

 

最後最關鍵的就是實現了MapActivity的GoogleMapActivity類:

public class GoogleMapActivity extends MapActivity {private MapView mMapView;private MapController mMapController;private GeoPoint mGeoPoint;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);mMapView = (MapView) findViewById(R.id.MapView01);//mMapView.setTraffic(true);//設定為交通模式mMapView.setStreetView(false);           //設定為街景模式,這個暫不支援,為true時,介面會有叉號方格mMapView.setSatellite(false);//若為true,則就是設定為衛星模式mMapController = mMapView.getController();//取得MapController對象(控制MapView)mMapView.setEnabled(true);//設定為啟用狀態mMapView.setClickable(true);mMapView.setBuiltInZoomControls(true);        //設定地圖支援縮放//設定起點為成都mGeoPoint = new GeoPoint((int) (30.659259 * 1000000), (int) (104.065762 * 1000000));mMapController.animateTo(mGeoPoint);          //定位到成都mMapController.setZoom(12);//設定倍數,必須在1-21之間//添加Overlay,用於顯示標註資訊MyLocationOverlay myLocationOverlay = new MyLocationOverlay();List<Overlay> list = mMapView.getOverlays();list.add(myLocationOverlay);}@Overrideprotected boolean isRouteDisplayed() {// TODO Auto-generated method stubreturn false;}class MyLocationOverlay extends Overlay{@Overridepublic boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when){super.draw(canvas, mapView, shadow);Paint paint = new Paint();Point myScreenCoords = new Point();// 將經緯度轉換成實際螢幕座標mapView.getProjection().toPixels(mGeoPoint, myScreenCoords);paint.setStrokeWidth(1);paint.setARGB(255, 255, 0, 0);paint.setStyle(Paint.Style.STROKE);Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.home);canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);canvas.drawText("天府廣場", myScreenCoords.x, myScreenCoords.y, paint);return true;}}}

執行個體效果:

【遇到問題2】

在設定mMapView.setStreetView(true);設定街景模式為真是,Map中會出現叉號方格,後來在網上問了一下朋友,在國內街景模式不支援的,汗啊。

以上就是Map的學習。

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.