當前位置:編程學習大全網 - 源碼下載 - Android開發怎麽利用Google 地圖

Android開發怎麽利用Google 地圖

自google 06年進入中國,在地圖、移動領域的發展速度基本上都是每年幾倍的增長。在最新的Android平臺開發相關應用程序,如果能深入了解google map 將會對我們Android開發提供很大的幫助.以下是我總結的在Android開發中對google map的理解。

1. 首先先要獲取妳的debug keystore位置:

打開Eclipse--->Windows---> preferences--->Android--->Build

查看默認的debug keystore位置,我的是C:\Documents and Settings\sdhbk\.android\debug.keystore

2.

D:\android-sdk-windows-1.5_r1\tools>keytool -list -alias androiddebugkey -keysto

re "C:\Documents and Settings\sdhbk\.android\debug.keystore" -storepass android

-keypass android

androiddebugkey, 2009-7-25, PrivateKeyEntry,

認證指紋 (MD5): DA:D5:6E:C2:80:D1:0F:0D:F8:2A:58:6A:74:7C:CE:2D

3.

  打開

填入妳的認證指紋(MD5)即可獲得apiKey了,結果顯示如下:

感謝您註冊 Android 地圖 API 密鑰!

您的密鑰是:

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

4.    使用得到的apiKey:

在layout中加入MapView

<com.google.android.maps.MapView

   android:id="@+id/mapview"

   android:layout_width="fill_parent"

   android:layout_height="fill_parent"

   android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g" />

或者

<view android:id="@+id/mv"

  class="com.google.android.maps.MapView"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:layout_weight="1"

  android:apiKey="0jOkQ80oD1JL9C6HAja99uGXCRiS2CGjKO_bc_g"

  />

5.Android在sdk1.5的預裝的add-on中提供了壹個Map擴展庫com.google.android.maps,利用它妳就可以給妳的android應用程序增加上強大的地圖功能了。這個庫的位置在Your-SDK_DIR\add-ons\google_apis-3\libs.

壹定要在manifest.xml中設置全相應的權限,比如:

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

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

要在manifest.xml中加上要用的maps庫:

<manifest xmlns:android=""

package="com.example.package.name">

...

<application android:name="MyApplication" >

  <uses-library android:name="com.google.android.maps" />

  ...

</application>

...

</manifest>

需要說明的是這個庫不是標準的android sdk的內容,妳也可以自己從這裏這裏下載並放到妳的sdk裏面。

Maps庫分析

Maps庫提供了十幾個類,具體可以參考這裏, 包括Mapview,MapController,MapActivity等。

Mapview是用來顯示地圖的view, 它也是派生自android.view.ViewGroup。

地圖可以以不同的形式來顯示出來,如街景模式,衛星模式等,具體方法可以參考:

setSatellite(boolean), setTraffic(boolean), and setStreetView(boolean)

MapView只能被MapActivity來創建,這是因為mapview需要通過後臺的線程來連接網絡或者文件系統,而這些線程要由mapActivity來管理。

需要特別說明的壹點是,android 1.5中,map的zoom采用了built-in機制,可以通過setBuiltInZoomControls(boolean)來設置是否在地圖上顯示zoom控件。

MapActivity是壹個抽象類,任何想要顯示MapView的activity都需要派生自MapActivity。並且在其派生類的onCreate()中,都要創建壹個MapView實例,可以通過MapView constructor (then add it to a layout View with ViewGroup.addView(View)) 或者通過layout XML來創建。

實例分析

最後,按照慣例,還是用壹個小程序來演示壹下android中地圖功能的開發。主要功能是實現了地圖的縮放,添加了菜單,從而可以手動選擇地圖的顯示模式等。

Step 1: 新建壹個android project, 註意這裏要選擇的build target為"Google APIs"

Step 2: 修改menifest文件:

< xml version="1.0" encoding="utf-8" >

<manifest xmlns:android=""

package="com.map.prac"

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

  <uses-library android:name="com.google.android.maps" />

  <activity android:name=".MapViewPrac2"

android:label="@string/app_name">

  <intent-filter>

  <action android:name="android.intent.action.MAIN" />

  <category android:name="android.intent.category.LAUNCHER" />

  </intent-filter>

  </activity>

  </application>

  <uses-sdk android:minSdkVersion="3" />

</manifest>

Step 3: 修改layout文件,main.xml

< xml version="1.0" encoding="utf-8" >

<LinearLayout xmlns:android=""

  android:id="@+id/main"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent">

  <com.google.android.maps.MapView

  android:id="@+id/map"

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

  android:enabled="true"

  android:clickable="true"

  android:apiKey="                  "

  />

</LinearLayout>

這裏需要將api key中的      改成妳自己申請到的api key.

Step 4: 修改代碼:

package com.map.prac;

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.app.AlertDialog;

import android.app.Dialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.util.Log;

import android.view.KeyEvent;

import android.view.Menu;

import android.view.MenuItem;

public class MapViewPrac2 extends MapActivity {

  private final String TAG = "MapPrac";

  private MapView mapView = null;

  private MapController mc;

  //Menu items

  final private int menuMode = Menu.FIRST;

  final private int menuExit = Menu.FIRST+1;

  final private int menuCommandList = Menu.FIRST + 2;

  private int chooseItem = 0;

  /** Called when the activity is first created. */

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  mapView = (MapView)findViewById(R.id.map);

  mc = mapView.getController();

  mapView.setTraffic(true); //

  mapView.setSatellite(false);

  mapView.setStreetView(true);

  //GeoPoint gp = new GeoPoint((int)(39.269259 * 1000000), (int)(115.255762 * 1000000));//yixian

  GeoPoint gp = new GeoPoint((int)(39.95 * 1000000), (int)(116.37 * 1000000));//beijing

  //mc.animateTo(gp);

  //mc.setZoom(12);

  mc.setCenter(gp);

  //to display zoom control in MapView

  mapView.setBuiltInZoomControls(true);

  }

  @Override

  public boolean onKeyDown(int keyCode, KeyEvent event) {

  // TODO Auto-generated method stub

  Log.i(TAG,"enter onKeyDown");

  return super.onKeyDown(keyCode, event);

  }

  @Override

  public boolean onCreateOptionsMenu(Menu menu) {

  menu.add(0, menuMode, 0, "Map Mode");

  menu.add(0, menuCommandList, 1, "Command List");

  menu.add(0, menuExit, 2, "Exit");

  return super.onCreateOptionsMenu(menu);

  }

  @Override

  public boolean onMenuItemSelected(int featureId, MenuItem item) {

  // TODO Auto-generated method stub

  switch(item.getItemId())

  {

  case menuMode:

  Dialog dMode = new AlertDialog.Builder(this)

  //.setIcon(R.drawable.alert_dialog_icon)

  .setTitle(R.string.alert_dialog_single_choice)

  .setSingleChoiceItems(R.array.select_dialog_items2, chooseItem, new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int whichButton) {

  Log.i(TAG, "choose button is "+ whichButton);

  chooseItem = whichButton;

  /* User clicked on a radio button do some stuff */

  }

  })

  .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int whichButton) {

  /* User clicked Yes so do some stuff */

  Log.i(TAG,"item = "+chooseItem);

  switch(chooseItem)

  {

  case 0:

    mapView.setSatellite(false);

    break;

  case 1:

    mapView.setSatellite(true);

    break;

  case 2:

    mapView.setTraffic(true);

    break;

  case 3:

    mapView.setStreetView(true);

    break;

  default:

    break;

  }

  }

  })

  .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int whichButton) {

  /* User clicked No so do some stuff */

  }

  })

  .create();

  dMode.show();

  break;

  case menuCommandList:

  //create the dialog

  Dialog d = new AlertDialog.Builder(this)

  .setTitle(R.string.select_dialog)

  .setItems(R.array.select_dialog_items, new DialogInterface.OnClickListener() {

  public void onClick(DialogInterface dialog, int which) {

  /* User clicked so do some stuff */

  String[] items = getResources().getStringArray(R.array.select_dialog_items);

  /*new AlertDialog.Builder(this)

      .setMessage("You selected: " + which + " , " + items[which])

      .show();*/

  Log.i(TAG,"you choose is: " + items[which]);

  }

  })

  .create();

  //show the dialog

  d.show();

  break;

  case menuExit:

  finish();

  break;

  default:

  break;

  }

  return super.onMenuItemSelected(featureId, item);

  }

  @Override

  protected boolean isRouteDisplayed() {

  // TODO Auto-generated method stub

  return false;

  }

}

關於google map的用法還有待各位Android開發人員在實際開發中的總結!

  • 上一篇:求排列的公式有哪些?
  • 下一篇:電磁學的物理公式
  • copyright 2024編程學習大全網