Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

動手做一個 Android Google Map 程式

編輯:Android 開發實例

本文帶你完成一個能顯示 Google 地圖、支援交通圖層與縮放按鈕的 Android 應用。

(本文所述為早期 Google Maps API v1 的做法。該版本已停止服務,後續請改用 Maps SDK for Android 並搭配 Google Cloud 的 API Key。流程觀念仍然適用,具體 API 名稱與金鑰申請方式則已不同。)

第一步:申請 API Key

舊版 Maps API 的 key 是綁定到你的簽章憑證上的,因此要先取得 debug keystore 的指紋。

在 MyEclipse 中查路徑:Window → Preferences → Android → Build → Default debug keystore,典型位置是:

C:\Documents and Settings\Administrator\.android\debug.keystore

然後在命令列執行 keytool 取得指紋:

keytool -list -alias androiddebugkey -keystore "C:\Documents and Settings\Administrator\.android\debug.keystore"

提示輸入密碼時輸入 android,就會得到類似這樣的指紋:

7A:71:D1:*:EC:40:*:18:52:0A:08:B1:05:*:*:*

把這串指紋貼到 Google 的申請頁面上登入帳號,即可取得對應的 API key。

第二步:建立專案

新增 Android 專案時,Build Target 必須選擇 Google APIs,而不是標準的 Android 版本。少了這個, you will 找不到地圖相關的類別。

若 Build Target 清單裡沒有 Google APIs,代表 SDK 安裝時沒有勾選對應的 add-on,回到 SDK Manager 補裝即可。

新增一個類別並繼承 MapActivity。

第三步:撰寫程式碼

package com.android.google.tank;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

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 MyGoogleMap extends MapActivity {

    private MapView mapView;
    private MapController mc;

    @Override
    protected boolean isRouteDisplayed() {
        // MapActivity 的抽象方法,此處不使用路線顯示
        return false;
    }

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);

        mapView = (MapView) findViewById(R.id.map);
        mapView.setTraffic(true);    // 交通模式
        mapView.setStreetView(false);// 街道模式
        mapView.setSatellite(false); // 衛星模式

        mc = mapView.getController();

        // 地理座標:上海。注意 GeoPoint 的經緯度需乘以 1000000 轉為整數
        GeoPoint gp = new GeoPoint((int) (31.132259 * 1000000), (int) (121.180762 * 1000000));
        mc.animateTo(gp);
        mc.setZoom(10);   // 縮放比例

        // 放大
        Button btnBig = (Button) findViewById(R.id.btnBig);
        btnBig.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mapView.getController().setZoom(mapView.getZoomLevel() + 1);
            }
        });

        // 縮小
        Button btnSmall = (Button) findViewById(R.id.btnSmall);
        btnSmall.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mapView.getController().setZoom(mapView.getZoomLevel() - 1);
            }
        });
    }
}

這裡最容易出錯的是 GeoPoint 的建構方式:它接受的是整數,而且單位是「度 × 10⁶」。直接傳入小數會編譯失敗,忘了乘 1000000 則會定位到錯誤的地方。

第四步:設定 AndroidManifest.xml

需要三樣東西:網路權限、位置權限,以及 uses-library 宣告。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.android.google.tank"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <!-- 載入 google 套件 -->
        <uses-library android:name="com.google.android.maps" />

        <activity android:name="com.android.google.tank.MyGoogleMap" android:label="MapsDemo">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

<uses-library> 這行是關鍵。它必須放在 <application> 內部,少了它,執行時會直接丟出 ClassNotFoundException。

第五步:版面配置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:id="@+id/LinearLayout01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/btnBig"
            android:text="放大"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/btnSmall"
            android:text="縮小"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <com.google.android.maps.MapView
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:enabled="true"
        android:clickable="true"
        android:apiKey="你申請的 google key" />
</LinearLayout>

執行後就能看到地圖了。

三個必須先確認的前提

一、模擬器要能上網。地圖是從網路載入圖磚的,沒有網路就是一片空白。請確認 AVD 的網路設定正常,並用瀏覽器驗證過。

二、AVD 的 target 必須是 Google APIs。用標準 Android target 建立的模擬器裡沒有 Maps 相關的實作,程式會在啟動時崩潰。

三、API key 與簽章必須配對。debug keystore 換了機器或重裝系統後指紋會改變,此時舊 key 會失效,症狀是地圖區域顯示空白或灰白方格。

後續可以延伸的方向

把基本地圖跑通之後,還有幾個自然的下一步:在指定座標上放置自訂標記(Overlay 與 OverlayItem)、依裝置的 GPS 定位取得目前經緯度並移動地圖中心、以及依縮放層級動態顯示不同密度的標記。

另外提醒:定位相關的 API 需要在執行期動態請求權限,且 GPS 在室內往往取不到訊號,實測時記得先確認是權限問題還是訊號問題。

轉自:https://www.cnblogs.com/tankaixiong/archive/2010/10/21/1857741.html

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved