Google的地圖服務在全世界來說也是最搶眼的,在Android中也可以使用地圖服務,使用MapView可以實現。
1創建新項目,在Build Target時選擇“Google APIs”,也就是要添加Google的API jar文件map.jar

2創建AVD時,target同樣選擇“Google APIs”

3獲得“Google Map API Key”,使用jdk的keytool生成MD5 key,Keytool這個文件一般位於%JAVA_HOME/bin目錄下。在開發Android程序時,一般是在debug模式下進行的,這時的SDK的build tools會自動使用debug模式下的證書對應用進行簽名,為了產生debug模式下證書的MD5 key,我們必須找到keystore密鑰庫,在Eclipse中,選擇菜單window->Preferences->Android->Build查看keystore路徑

打開CMD,使用 keytool -list -keystore 'debug kestore路徑'生成MD5 key,密碼默認為android

獲取MD5 key以后,前往https://developers.google.com/android/maps-api-signup申請API key

點擊“Generate API Key”
Your key is: 0qFZTnChETSXYodY7eQ_OsScp1HHUUByWyorriA This key is good for all apps signed with your certificate whose fingerprint is: DB:02:A6:CC:77:72:5F:2F:44:F2:E9:78:98:C3:ED:01 Here is an example xml layout to get you started on your way to mapping glory: <com.google.android.maps.MapView android:layout_width="fill_parent" android:layout_height="fill_parent" android:apiKey="0qFZTnChETSXYodY7eQ_OsScp1HHUUByWyorriA" />
4 在AndroidManifiest.xml中在Application節添加配置<uses-library android:name="com.google.android.maps" />
在AndroidManifiest.xml中manifest節添加權限
<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"/>
完整的AndroidManifiest.xml如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.synvata.maps" android:versionCode="1" android:versionName="1.0" > <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" > <uses-library android:name="com.google.android.maps" /> <activity android:name=".MapViewsActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar"> > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
5 在layout下Main.xml中添加MapView,如下
<?xml version="1.0" encoding="utf-8"?> <com.google.android.maps.MapView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mapview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:clickable="true" android:apiKey="0qFZTnChETSXYodY7eQ_OsScp1HHUUByWyorriA" />
6 Activity要繼承MapActivity,代碼如下
public class MapViewsActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
此時,運行程序,可以看到Google Map。
7 通常使用地圖時,要增加一些自定義功能,比如標注。Android中Overlay這個類提供了在地圖上添加圖層的基本功能,
新建一個類,命名為HelloItemizedOverlay,繼承於ItemizedOverlay。
聲明一個OverlayItem 類型的ArrayList,用於保存所有的圖層
private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
定義默認構造函數,構造函數中必須為每一個圖層定義一個默認的標注,並且使用bound方法定義標注的位置
public HelloItemizedOverlay(Drawable defaultMarker) { super(boundCenterBottom(defaultMarker)); }
定義方法將圖層添加至ArrayList
public void addOverlay(OverlayItem overlay) { mOverlays.add(overlay); populate(); }
重寫createItem()和size()方法
@Override protected OverlayItem createItem(int i) { return mOverlays.get(i); } @Override public int size() { return mOverlays.size(); }
為了能夠處理標注的觸碰事件,我們需要一個新的構造函數,為每一個圖層指定應用的上下文,首先添加類成員 Context mContext,構造函數如下
public HelloItemizedOverlay(Drawable defaultMarker, Context context) { super(boundCenterBottom(defaultMarker)); mContext = context; }
然后重寫onTap(int)處理圖標的點擊事件
@Override protected boolean onTap(int index) { OverlayItem item = mOverlays.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; }
至此,我們的layer類編寫完畢
8 返回MapViewsActivity ,在onCreate()中初始化
List<Overlay> mapOverlays = mapView.getOverlays(); Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker); HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(drawable, this);
創建 GeoPoint ,它定義了經緯度坐標,其值為經緯度數值*1000,000,然后根據位置創建圖層。
GeoPoint point = new GeoPoint(36075402,120413824);
OverlayItem overlayitem = new OverlayItem(point, "Hello!", "I'm in Qingdao City!");
將新圖層添加到Map中
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
使用mapView.getController().animateTo(point);將地圖移動到指定區域,setZoom()指定放大級別。
完整的MapViewsActivity如下:
public class MapViewsActivity extends MapActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MapView mapView = (MapView) findViewById(R.id.mapview); mapView.setBuiltInZoomControls(true); List<Overlay> mapOverlays = mapView.getOverlays(); Drawable drawable = this.getResources().getDrawable(R.drawable.ic_launcher); HelloItemizedOVerlay itemizedoverlay = new HelloItemizedOVerlay(drawable, this); GeoPoint point = new GeoPoint(36075402,120413824); OverlayItem overlayitem = new OverlayItem(point, "Hello!", "I'm in Qingdao City!"); itemizedoverlay.addOverlay(overlayitem); mapOverlays.add(itemizedoverlay); mapView.getController().animateTo(point); mapView.getController().setZoom(17); } @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } }
附HelloItemizedOverlay.java
public class HelloItemizedOVerlay extends ItemizedOverlay { private ArrayList<OverlayItem> mOverlay = new ArrayList<OverlayItem>(); private Context mContext; public HelloItemizedOVerlay(Drawable defaultMarker) { // TODO Auto-generated constructor stub super(boundCenterBottom(defaultMarker)); } public HelloItemizedOVerlay(Drawable defaultMarker,Context context){ super(boundCenterBottom(defaultMarker)); mContext=context; } @Override protected OverlayItem createItem(int i) { // TODO Auto-generated method stub return mOverlay.get(i); } @Override public int size() { // TODO Auto-generated method stub return mOverlay.size(); } public void addOverlay(OverlayItem overlay){ mOverlay.add(overlay); populate(); } @Override protected boolean onTap(int index) { OverlayItem item = mOverlay.get(index); AlertDialog.Builder dialog = new AlertDialog.Builder(mContext); dialog.setTitle(item.getTitle()); dialog.setMessage(item.getSnippet()); dialog.show(); return true; } }
運行結果如下:
|
|
|


