google map api v2的使用詳細過程,圖文並茂(原創)


上一篇中說到怎么獲取key,下面來介紹怎么使用key來顯示google地圖

步驟1:eclipse上打開android SDK Manager,安裝google play services.

 

 

步驟2:eclipse上建立google-play-services-lib,File->new->others->Android->Android project from existing code,然后將你剛剛安裝的google play services包導入進去(在你sdk文件下),創建完成!

 

創建好后你的eclipse上就會多一個google-play-services_lib項目包。

 

步驟3:創建一個你自己的google map項目,這個怎么創建地球人都知道了。創建好后就開始導入google map 了。

    右擊你的項目名,選擇屬性,在彈出來的對話框中點擊android->add->選定google—play-services_lib,添加進去就ok了。

 

步驟4.添加key到你的AndroidManifest.xml中。

    將下面的代碼放到<application>中,就插入到</application>前面。注意,這里的API_KEY要替換成你之前申請的秘鑰,另外對於你的不同項目,如果包名不一樣,得申請不同的key,因為在申請的時候需要你輸入你的包名,也就是說不同包的項目對應不同的key,一個key不用應用不同包的項目。還需要注意的是在一台電腦上申請的key不能在另外一台電腦上使用,切記!

<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="API_KEY"/>

然后將下面代碼放入AndroidManifest.xml中,其中記得把com.example.mapdemo替換成你的包名。

<permission
        android:name="com.example.mapdemo.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
<uses-permission android:name="com.example.mapdemo.permission.MAPS_RECEIVE"/>

 

下面接着就是添加一系列權限了,什么網絡訪問權限,地理位置訪問權限什么的,下面是一系列權限,大家根據自己的需要添加,盡量不要全部復制進去,以免對程序安全性造成威脅。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>


步驟5. google map api第二版本中用到OpenGL ES version2,所以必須在<manifest>節點里面添加以下代碼。

<uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>

整體AndroidManifest.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />
    <uses-feature android:name="android.hardware.location.gps" />

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="10" />

    <permission
        android:name="com.example.googlemap.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <!--
     The following two permissions are not required to use
     Google Maps Android API v2, but are recommended.
    -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.googlemap.MainActivity"
            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"
            android:required="true" />

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="這里填寫你的key" />
    </application>

</manifest>


步驟6.剩下就是布局了。代碼如下(這是基本布局,如有需要可以自己添加)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <fragment 
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        class="com.google.android.gms.maps.SupportMapFragment" />


</LinearLayout>


步驟7.在主程序中最基本的代碼如下(正常顯示地圖),依據自己需要添加代碼。

package com.example.mapdemo;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

好了,就到這了!如果有什么錯誤的地方或者不明白的可以在下面留言^^
轉載請注明原處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM