2011年11月23日 星期三

使用google地圖

    在使用google地圖前我們須先了解Android同時接受KML及GPX等多種地圖座標的資訊格式
※KML(Keyhole Makeup Language):是google地圖和google Earth所使用的座標格式
※GPX:是GPS上常見的紀錄格式
  如何取得KML值:http://www.mygeoposition.com/網站輸入目標值即可取得經緯度


取得金鑰的認證指紋(MD5)
在向Google申請API Key之前,我們必需要先取得自己金鑰的MD5。這裡的金鑰有兩種,一種是開發中的金鑰,另一種則是要發佈用的金鑰。每一台電腦都有屬於自己的金鑰。如果是在不同電腦中開發就要個別取得,要找到你開發金鑰的位置,可以開啟Eclipse > Windows > Preferences > Android > Build裡面有個debug.keystore檔案位置,即是開發用的金鑰。

1.執行cmd,找到Javabin資料夾(若你也是使用XP,位置是:C:\Program Files\Java\jre6\bin)
2.輸入取得MD5的指令,可以取得MD5的編碼
    C:\>cd C:\Program Files\Java\jdk1.7.0\bin

    C:\Program Files\Java\jdk1.7.0\bin>C:\Users\USER\.android\debug.keystore

 C:\Program Files\Java\jdk1.7.0\bin>keytool -list -v -alias androiddebugkey -keystore        "C:\Users\USER\.android\debug.keystore" -storepass android -keypass android
3.就會出現
    別名名稱: androiddebugkey
    建立日期: 2011/10/7
    項目類型: PrivateKeyEntry
    憑證鏈長度: 1
    憑證 [1]:
    擁有者: CN=Android Debug, O=Android, C=US
    發出者: CN=Android Debug, O=Android, C=US
    序號: 53bed02f
    有效期自: Fri Oct 07 10:33:19 CST 2011 : Sun Sep 29 10:33:19 CST 2041
   憑證指紋:
         MD5:  
         SHA1:
         SHA256: 
         簽章演算法名稱: SHA256withRSA
         版本: 3
4.進入GoogleMap API Key的申請網站(http://code.google.com/intl/zh-TW/android/maps-api-signup.html),並且輸入剛剛取得的MD5編碼

5.取得Android Maps API的金鑰
6.將取得的Map API Key貼到

   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" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    <com.google.android.maps.MapView
        android:id="@+id/map"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:apiKey=""//金鑰
        android:clickable="true"        />   
</LinearLayout>

   建立Google API專案
       請建立一個新的專案,在建立新專案的過程中,記得「Build Target」要選擇「Google APIs」。   
   使用地圖
    google地圖的組成元素:
       地圖模型(Moudel)
            地點資訊(GeoPoint):地圖上的座標  
            路徑:兩點之間的關係
       地圖展示(View)
            展示區域(MapView): 座標的週邊區域
            圖層(OverLay):地圖上的標示
       地圖控制(Controller)
           控制檢視圖類型:包含衛星、地圖、街景
           控制縮放尺度(Zoom):最大可以全球,最小就街道或街景地圖
佈屬MapView物件
  
      開啟androidManifest.xml
      <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.demo.android.MapA"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" />
    <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"/>"
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MapAActivity" >
            <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>
</manifest>
加入以上那4行,各行的存取解釋如下:
 ACCESS_COARSE_LOCATION是有關位置資訊獲取,比如說MyLocation API擷取的Cellid等定位資訊必需加上android.permission.ACCESS_COARSE_LOCATION這個聲明

ACCESS_FINE_LOCATION
ACCESS_LOCATION_EXTRA_COMMANDS ACCESS_MOCK_LOCATION是有關GPS定位擷取的資訊使用GPS LocationProvider類的相關定位資訊必需聲明
android.permission.INTERNET 因為MapView類別與google地圖一樣需要連上網路
定義XML檔(請參照佈屬MapView物件處的程式碼)

產生地圖
  package com.demo.android.MapA;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;

import android.os.Bundle;

public class MapAActivity extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        findViews();
        setupMap();
    }
    private MapView mv;//宣告google map物件
    private MapController mc;//宣告google map控制物件
   
    private void setupMap() {
        // TODO Auto-generated method stub
        GeoPoint point_pintung=new GeoPoint((int)(25.047192*1000000),(int)(121.516981*1000000));//設定地圖座標值 經度&緯度
        mv.setTraffic(true);//地圖檢視模式
                      //.setTraffic         一般檢視模式
                 //.setStatellite       衛星檢視模式
                 //.setStreetView    街景檢視模式
        mc.setZoom(17);
        mc.animateTo(point_pintung);
    }

    private void findViews() {
        // TODO Auto-generated method stub
        mv=(MapView)findViewById(R.id.map);//抓取google map物件
        mc=mv.getController();//控制map物件
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
}




1 則留言: