step1:

在eclipse中window>preferences>Android>Build

這時在右邊會看到你的Default debug keystore

Default debug keystore  

把他抄下來

然後打開cmd 在DOS畫面下

輸入下面整行(不能有空白鍵)

keytool -list -alias androiddebugkey -keystore 剛抄下的檔案位置 -storepass android -keypass android -v

之後會跑出一堆東西 其中有一個憑證指紋下的 MD5 把他抄下來(總共有16組雙位數)

之後在這個網址 http://code.google.com/intl/zh-TW/android/maps-api-signup.html

確定之後打勾下面會要你輸入MD5 就把你剛剛抄下來的MD5輸入上去 就可以取得key啦~~

==============================================================

然後要注意的是

1.在android裡建立專案 一般我們Build Target 都選擇Android2.2 2.3之類的

這裡要選擇Google APIs

2.

必須在 AndroidManifest.xml 裡宣告使用 Google Maps Library,否則會出現 ClassNotFoundException。

<application .........>
        <activity........>
            <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" />

這樣就可以在 XML 裡使用 MapView 了。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <com.google.android.maps.MapView
        android:id="@+id/gmap"
        android:apiKey="剛剛申請成功的Googlemap key"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clickable="true"/>
</LinearLayout>

再來點範例程式

程式中

public class MyMapView extends MapActivity{
private MapView mapView;
private MapController mapController;
private GeoPoint geoPoint;

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.boarding_place);

//設定程式全螢幕 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mapView = (MapView) findViewById(R.id.map);
        mapController = mapView.getController();
//設定衛星圖關閉
        mapView.setSatellite(false);
//設定街道圖開啟
        mapView.setStreetView(true);
//設定地圖級距1-21
        mapController.setZoom(18);

//設定台北101的座標
        geoPoint = new GeoPoint((int)(25.035527 * 1E6), (int)(121.56415 * 1E6));

    mapView.displayZoomControls(true);
    /* 將Map的中點移至GeoPoint */
    mapController.animateTo(geoPoint);
    }
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}

tip:在網路的googlemap選好地方後在地標上點右鍵 選擇"這裡有什麼" 就可以得到此地點的經緯度囉

如果要取得目前位置就用

geoPoint.getLatitudeE6()/1E6

geoPoint.getLongitudeE6()/1E6

之後就可以開啟AVD來測試看看囉 因為剛剛我們Build Target選了Google APIs

所以跟之前的Android2.2模擬器不一樣  要重新建立

googlemap  

 

實作參考:http://cookiesp.pixnet.net/blog/post/77354500


arrow
arrow

    cookiesp 發表在 痞客邦 留言(0) 人氣()