編輯:Android 開發實例
本文帶你完成一個能顯示 Google 地圖、支援交通圖層與縮放按鈕的 Android 應用。
(本文所述為早期 Google Maps API v1 的做法。該版本已停止服務,後續請改用 Maps SDK for Android 並搭配 Google Cloud 的 API Key。流程觀念仍然適用,具體 API 名稱與金鑰申請方式則已不同。)
舊版 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 則會定位到錯誤的地方。
需要三樣東西:網路權限、位置權限,以及 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
現代 Android 架構實戰:從響應式資料流到建置系統的整理術
沉寂了一段時間,收集到不少讀者回饋之後,我覺得這個話題該好好談一談了。接下來我想分享的是:在我眼中,打造一個現代化 Android 應用該有的樣子,以及支撐它背後的架構思
Android MediaPlayer 影音播放:建立簡易音樂播放器
MediaPlayer 讓 Android 開發者能輕鬆加入影音播放功能。只要把媒體檔放進 raw 或指定來源,就能透過 API 控制播放狀態。 播放與暫停 建立 Med
ViewPager實現左右無限循環滑動與滑動跳轉
前一篇講了如何用 ViewPager 高仿 launcher 的拖動效果,之後不少朋友來問:能不能實現左右循環滑動和引導頁?今天就把左右滑動做出來了。至於「滑到最後一頁再
Android 進度條教學:Spinner 的宣告、切換與常用 API
使用者按下按鈕之後,如果畫面兩三秒沒反應,多半會以為 App 當機了。所以任何耗時的背後工作,都該配一個進度提示。Android 的 ProgressBar 有兩種常見外