今天參照Android Api里的Hello Views來學(xué)習(xí)GMap,不想,Api里好多錯(cuò)誤,囧
          so,我在這里貼出正確的代碼,供大家參考

          TODO(1):顯示Map(Creating a Map Activity)
          step1:創(chuàng)建一個(gè)新的工程
          step2:由于Maps library不是標(biāo)準(zhǔn)庫里的東東,SO,要在AndroidManifest.xml文件中加上一個(gè)library:
          <uses-library android:name="com.google.android.maps" />
          step3:Map功能需要有網(wǎng)絡(luò)連接,SO,權(quán)限不能少:
          <uses-permission android:name="android.permission.INTERNET" />
          step4:隱藏titleBar(隨意):
          <activity android:name=".GMapTest" android:label="@string/app_name"
               
          android:theme="@android:style/Theme.NoTitleBar">
          step5:在res/layout/main.xml里加一個(gè)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
          ="Your Maps API Key goes here"
          />
          PS:關(guān)于如何得到Key,請(qǐng)參看http://www.aygfsteel.com/crazycoding/archive/2011/10/11/360937.html
          step6:打開自動(dòng)創(chuàng)建的Activity (GMap.java)文件
          修改之,使其繼承自MapActivity
          public class GMapTest extends MapActivity {
          step7:實(shí)現(xiàn)相應(yīng)方法:
          @Override
              
          protected boolean isRouteDisplayed() {
                  
          // TODO Auto-generated method stub
                  return false;
              }
          step8:完善onCreate方法:
          setContentView(R.layout.main);
          此時(shí),地圖已經(jīng)可以顯示出來了,不過,我們可以給它加上縮放按鈕:
          MapView mapView = (MapView) findViewById(R.id.mapview);
          mapView.setBuiltInZoomControls(
          true);
          哦了。
          現(xiàn)整理相關(guān)文件完整代碼如下:
          AndroidManifest.xml:
          <?xml version="1.0" encoding="utf-8"?>
          <manifest xmlns:android="http://schemas.android.com/apk/res/android"
                package
          ="com.yinger"
                android:versionCode
          ="1"
                android:versionName
          ="1.0">
              
          <uses-sdk android:minSdkVersion="7" />

              
          <application android:icon="@drawable/icon" android:label="@string/app_name">
                  
          <activity android:name=".GMapTest"
                            android:label
          ="@string/app_name">
                      
          <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" />
          </manifest>
          main.xml:
          <?xml version="1.0" encoding="utf-8"?>
          <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:layout_width
          ="fill_parent"
                           android:layout_height
          ="fill_parent"
                           android:enabled 
          = "true"
                           android:clickable 
          = "true"
                           android:apiKey
          ="055ZV_999VhK6Zcr-8P7tyGzeRwTnWV_JET9kKg"
                           
          />
          </LinearLayout>
          GMapTest.java:
          package com.yinger;

          import com.google.android.maps.MapActivity;
          import android.os.Bundle;

          /**
           * HelloWorld Google Map
           * 
           * 
          @author Ying_er
           * @Email melody.crazycoding@gmail.com
           * @time 2011/10/11 14:12:15
           * 
          @version 1.00
           
          */
          public class GMapTest extends MapActivity     {
          /** Called when the activity is first created. */
              @Override
              
          public void onCreate(Bundle savedInstanceState) {
                  
          super.onCreate(savedInstanceState);
                  setContentView(R.layout.main);
              }

              @Override
              
          protected boolean isRouteDisplayed() {
                  
          // TODO Auto-generated method stub
                  return false;
              }
          }


          TODO(2):在Map上顯示標(biāo)記,效果如圖:

          首先,我先簡(jiǎn)單的說一嘴在Map上顯示標(biāo)記的基本原理:一個(gè)圖層顯示地圖,一個(gè)圖層顯示標(biāo)記,然后兩個(gè)圖層羅列。
          SO,也就是Adding Overlay Items
          (假設(shè)已經(jīng)按TODO(1)的步驟完成了相應(yīng)代碼)
          step1:創(chuàng)建一個(gè)新的Java類:HelloItemizedOverlay,讓他繼承ItemizedOverlay<OverlayItem>:
          public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {
          step2:創(chuàng)建一個(gè)List對(duì)象,存儲(chǔ)該圖層中所有的標(biāo)記對(duì)象
          private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
          step3:構(gòu)造方法:
              /**
               * 參數(shù)用于指定顯示標(biāo)記的默認(rèn)圖片
               * 
          @param arg0
               
          */
              
          public HelloItemizedOverlay(Drawable arg0) {
                  
          super(boundCenterBottom(arg0));
                  
          // TODO Auto-generated constructor stub
              }
          step4:定義方法,將生成好的OverlayItem對(duì)象添加到List當(dāng)中
          public void addOverlay(OverlayItem overlay) {
                  mOverlays.add(overlay);
                  populate();
              }
          step5:執(zhí)行populate方法時(shí),會(huì)執(zhí)行createItem方法,來創(chuàng)建一個(gè)OverlayItem對(duì)象
          protected OverlayItem createItem(int i) {
                  
          // TODO Auto-generated method stub
                  return mOverlays.get(i);
              }
          step6:重寫方法,返回當(dāng)前Overlay中的OverlayItem對(duì)象個(gè)數(shù)
          @Override
              
          public int size() {
                  
          // TODO Auto-generated method stub
                  return mOverlays.size();
              }
          step7:為了能相應(yīng)點(diǎn)擊事件,需要一個(gè)Context的引用,SO,增加如下構(gòu)造函數(shù):
          public HelloItemizedOverlay(Drawable arg0, Context context) {
                  
          super(boundCenterBottom(arg0));
                  
          // TODO Auto-generated constructor stub
                  this.context = context;
              }
          當(dāng)用戶點(diǎn)擊標(biāo)記時(shí)所執(zhí)行的操作:
          @Override
              
          protected boolean onTap(int index) {
                  OverlayItem item 
          = mOverlays.get(index);
                  AlertDialog.Builder dialog 
          = new AlertDialog.Builder(context);
                  dialog.setTitle(item.getTitle());
                  dialog.setMessage(item.getSnippet());
                  dialog.show();
                  
          return true;
              }
          step8:回到MapActivity的onCreate方法
          得到所有圖層對(duì)象:
                  List<Overlay> mapOverlays = mapView.getOverlays();
                  Drawable drawable 
          = this.getResources().getDrawable(
                          R.drawable.androidmarker);
                  HelloItemizedOverlay itemizedoverlay 
          = new HelloItemizedOverlay(
                          drawable, 
          this);
          創(chuàng)建GeoPoint對(duì)象,通過經(jīng)緯度,指定地圖上的一個(gè)點(diǎn)
          GeoPoint point = new GeoPoint(19240001-99120000);
          創(chuàng)建一個(gè)OverlayItem對(duì)象
          OverlayItem overlayitem = new OverlayItem(point, "Hola, Mundo!",
                          
          "I'm in Mexico City!");
          將創(chuàng)建好的OverlayItem對(duì)象放到HelloItemizedOverlay當(dāng)中
          itemizedoverlay.addOverlay(overlayitem);
          將HelloItemizedOverlay放到mapOverlays當(dāng)中
          mapOverlays.add(itemizedoverlay);
          當(dāng)然,也可以多創(chuàng)建幾個(gè)標(biāo)記。
          部分文件完整代碼:
          MarkPoint.java
          package com.yinger;

          import java.util.List;

          import android.graphics.drawable.Drawable;
          import android.os.Bundle;

          import com.google.android.maps.GeoPoint;
          import com.google.android.maps.MapActivity;
          import com.google.android.maps.MapView;
          import com.google.android.maps.Overlay;
          import com.google.android.maps.OverlayItem;

          /**
           * 顯示標(biāo)記
           * 
           * 
          @author Ying_er
           * @Email melody.crazycoding@gmail.com
           * @time 2011/10/12 15:23:42
           * 
          @version 1.00
           
          */
          public class MarkPoint extends MapActivity {
              MapView mapView 
          = null;

              
          /** 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.mapview);
                  mapView.setBuiltInZoomControls(
          true);

                  
          /**
                   * 得到所有圖層對(duì)象
                   
          */
                  List
          <Overlay> mapOverlays = mapView.getOverlays();
                  Drawable drawable 
          = this.getResources().getDrawable(
                          R.drawable.androidmarker);
                  HelloItemizedOverlay itemizedoverlay 
          = new HelloItemizedOverlay(
                          drawable, 
          this);

                  
          /**
                   * 創(chuàng)建GeoPoint對(duì)象,通過經(jīng)緯度,指定地圖上的一個(gè)點(diǎn)
                   
          */
                  GeoPoint point 
          = new GeoPoint(19240001-99120000);
                  
          /**
                   * 創(chuàng)建一個(gè)OverlayItem對(duì)象
                   
          */
                  OverlayItem overlayitem 
          = new OverlayItem(point, "Hola, Mundo!",
                          
          "I'm in Mexico City!");
                  
                  GeoPoint point2 
          = new GeoPoint(35410000139460000);
                  OverlayItem overlayitem2 
          = new OverlayItem(point2, "Sekai, konichiwa!""I'm in Japan!");

                  
          /**
                   * 將創(chuàng)建好的OverlayItem對(duì)象放到HelloItemizedOverlay當(dāng)中
                   
          */
                  itemizedoverlay.addOverlay(overlayitem);
                  itemizedoverlay.addOverlay(overlayitem2);
                  
          /**
                   * 將HelloItemizedOverlay放到mapOverlays當(dāng)中
                   
          */
                  mapOverlays.add(itemizedoverlay);
              }

              @Override
              
          protected boolean isRouteDisplayed() {
                  
          // TODO Auto-generated method stub
                  return false;
              }
          }
          HelloItemizedOverlay.java:
          package com.yinger;

          import java.util.ArrayList;

          import android.app.AlertDialog;
          import android.content.Context;
          import android.graphics.drawable.Drawable;

          import com.google.android.maps.ItemizedOverlay;
          import com.google.android.maps.OverlayItem;

          /**
           * 在MapView之上,創(chuàng)建一個(gè)圖層(OverlayItem) 生成該類對(duì)象,并將該對(duì)象添加到MapView.getOverlays()里
           * 一個(gè)OverlayItem對(duì)象就代表了一個(gè)在地圖上顯示的標(biāo)記
           * 
           * 
          @author Ying_er
           * @Email melody.crazycoding@gmail.com
           * @time 2011/10/12 14:53:17
           * 
          @version 1.00
           
          */
          public class HelloItemizedOverlay extends ItemizedOverlay<OverlayItem> {

              
          /**
               * 創(chuàng)建一個(gè)List對(duì)象,存儲(chǔ)該圖層中所有的標(biāo)記對(duì)象
               
          */
              
          private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
              
          private Context context;

              
          /**
               * 參數(shù)用于指定顯示標(biāo)記的默認(rèn)圖片
               * 
               * 
          @param arg0
               
          */
              
          public HelloItemizedOverlay(Drawable arg0) {
                  
          super(boundCenterBottom(arg0));
                  
          // TODO Auto-generated constructor stub
              }

              
          public HelloItemizedOverlay(Drawable arg0, Context context) {
                  
          super(boundCenterBottom(arg0));
                  
          // TODO Auto-generated constructor stub
                  this.context = context;
              }

              
          /**
               * 創(chuàng)建一個(gè)OverlayItem對(duì)象
               
          */
              @Override
              
          protected OverlayItem createItem(int i) {
                  
          // TODO Auto-generated method stub
                  return mOverlays.get(i);
              }

              
          /**
               * 返回當(dāng)前Overlay中的OverlayItem對(duì)象個(gè)數(shù)
               
          */
              @Override
              
          public int size() {
                  
          // TODO Auto-generated method stub
                  return mOverlays.size();
              }

              
          /**
               * 將生成好的OverlayItem對(duì)象添加到List當(dāng)中
               * 
               * 
          @param overlay
               
          */
              
          public void addOverlay(OverlayItem overlay) {
                  mOverlays.add(overlay);
                  populate();
              }

              
          /**
               * 當(dāng)用戶點(diǎn)擊標(biāo)記時(shí)所執(zhí)行的操作
               
          */
              @Override
              
          protected boolean onTap(int index) {
                  OverlayItem item 
          = mOverlays.get(index);
                  AlertDialog.Builder dialog 
          = new AlertDialog.Builder(context);
                  dialog.setTitle(item.getTitle());
                  dialog.setMessage(item.getSnippet());
                  dialog.show();
                  
          return true;
              }
          }


          posted on 2011-10-12 15:47 Ying-er 閱讀(1926) 評(píng)論(1)  編輯  收藏 所屬分類: Google MapAndroid

          評(píng)論:
          # re: Step by Step——Google Map View 2011-10-12 17:31 | @joe
          恩,挺詳細(xì)  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 锡林浩特市| 同德县| 木兰县| 油尖旺区| 资阳市| 兴海县| 广元市| 泸溪县| 潍坊市| 芜湖县| 九江县| 怀来县| 咸阳市| 常熟市| 米泉市| 桂东县| 闸北区| 灵台县| 太湖县| 阿坝| 汪清县| 洪江市| 澄城县| 平湖市| 海口市| 彰化市| 家居| 孝义市| 靖江市| 延川县| 财经| 台州市| 环江| 临西县| 江达县| 新化县| 宝清县| 和政县| 修武县| 曲阜市| 大港区|