---
title: "如何在Android Tab页面中添加MapView"
author: "guowee"
date: "2010-04-30T15:20:01Z"
tags: []
categories: ["移动开发"]
slug: "android-tab-mapview"
draft: false
lastmod: "2010-04-30T15:20:01Z"
id: 657071
url: "https://guowee.iteye.com/blog/657071"
---

&lt;script type="text/javascript"&gt;&lt;!-- google\_ad\_client = &quot;pub-8045578586221443&quot;; /* 200x200, 创建于 10-4-14 \*/ google\_ad\_slot = &quot;5344497132&quot;; google\_ad\_width = 200; google\_ad\_height = 200; //--&gt; &lt;/script&gt;&lt;script src="http://pagead2.googlesyndication.com/pagead/show\_ads.js" type="text/javascript"&gt; &lt;/script&gt;&lt;script src="http://pagead2.googlesyndication.com/pagead/expansion\_embed.js"&gt;&lt;/script&gt;&lt;script src="http://googleads.g.doubleclick.net/pagead/test\_domain.js"&gt;&lt;/script&gt;&lt;script&gt;google\_protectAndRun(&quot;ads\_core.google\_render\_ad&quot;, google\_handleError, google\_render\_ad);&lt;/script&gt;

使用google map要这样搞。  
step1)创建一个keystore.在debug模式下，使用系统默认的debug.keystore就行了，如果是完成了，要publish出去， 就得自己创建一个keystore,并用它来签名你的project.具体的参见以前的《签名你的android项目》那篇帖子。在debug模式下，我 们可以通过查看Eclipse的Window-&gt;Preferences-&gt;Android-&gt;Build，我的是在C:\\Users \\seya\\AppData\\Local\\Android\\下的debug.keystore文件。这个文件有如下默认属性：  
Keystore name — "debug.keystore"  
Keystore password — "android"  
Key alias — "androiddebugkey"  
Key password — "android"  
CN — "CN=Android Debug,O=Android,C=US"  
然后，我们使用keytool来获取MD5指纹验证码，在cmd中，到android sdk目录下的tools下，用如下命令：  
keytool -list -keystore C:\\Users\\seya\\AppData\\Local\\Android\\debug.keystore  
要求输入密码，输入：android. 接着，MD5出来了：F9:0B:EF:70:D5:0F:71:A1:62:5F:E1:58:C3:B9:2B:5C  
用这串数字去http://code.google.com/intl/zh-CN/android/maps-api-signup.html 上， 输入到My certificate's MD5 fingerprint 那里，然后点Generate API Key,在新的页面里就得到了你的apiKey:0VYn9qrD1aANK5V8UVCb0YPy76f-Ixg\_roc0Fig

Step2)创建一个简单的SimpleMapViewTest project.  
\-----main.xml  
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android "  
    android:orientation="vertical"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="fill\_parent"  
    &gt;  
&lt;view class = "com.google.android.maps.MapView"    
android:id = "@+id/my\_map"  
android:enabled = "true"  
android:apiKey="0VYn9qrD1aANK5V8UVCb0YPy76f-Ixg\_roc0Fig"  
    android:layout\_width="fill\_parent"  
    android:layout\_height="wrap\_content"  
    /&gt;  
&lt;/LinearLayout&gt;  
红色地方就用到了step1里得到的apiKey.

--------AndroidManifest.xml  
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android "  
      package="com.sm.test"  
      android:versionCode="1"  
      android:versionName="1.0.0"&gt;  
    &lt;application android:icon="@drawable/icon" android:label="@string/app\_name"&gt;  
        &lt;uses-library  android:name="com.google.android.maps" /&gt;  
        &lt;activity android:name=".SMTest"  
                  android:label="@string/app\_name"&gt;  
            &lt;intent-filter&gt;  
                &lt;action android:name="android.intent.action.MAIN" /&gt;  
                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;  
            &lt;/intent-filter&gt;  
        &lt;/activity&gt;  
    &lt;/application&gt;  
&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;  
&lt;uses-permission android:name="android.permission.ACCESS\_COARSE\_LOCATION"&gt;&lt;/uses-permission&gt;  
&lt;uses-permission android:name="android.permission.ACCESS\_FINE\_LOCATION"&gt;&lt;/uses-permission&gt;  
&lt;/manifest&gt;

红色部分是必须的。

--代码部分:  
package com.sm.test;  
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.Activity;  
import android.os.Bundle;  
public class SMTest extends MapActivity {  
    /\** Called when the activity is first created. \*/  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        //get map view  
        MapView mv = (MapView) findViewById(R.id.my\_map);  
        MapController controller  = (MapController)mv.getController();  
          
        GeoPoint p = new GeoPoint((int) (24.7 * 1000000),(int)(-238.8 * 1000000));  
        controller.animateTo(p);  
        controller.setZoom(9);  
          
    }  
@Override  
protected boolean isRouteDisplayed() {  
  // TODO Auto-generated method stub  
  return false;  
}  
}

这样，一运行，终于成功了。