GOOGLEMAP02.jpg       

主要分成兩大部分:  

Part1 建立最基本Google Map Application

Part2 加入Zoom元件

 

[ Part1 ] 基本Google Map Application

Step1申請Google Map API Key

Step2新增可以RunGoogle Map Application的模擬器

Step3因為Google Map API是額外加入,故必須在AndroidManifest.xml中加入自訂的函式庫,加上使用Google Map服務時必須使用網路,所以也要AndroidManifest.xml內加入使用網路之權限。

AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.sam.test"

      android:versionCode="1"

      android:versionName="1.0">

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

 

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".GpsGmap"

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

    <!--增加Google Map 函式庫-->

    </application>

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

    <!--允許程式使用網路-->

</manifest>

 

Step4當申請API Key成功後不只會拿到金鑰,其網頁也提供一段xml程式碼提供使用者使用,此段程式碼須放在res/layout/main.xml內,透過MapView來使用Google Map服務。

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"

    >

    <!--MapView放置 Map API Key-->

 <com.google.android.maps.MapView

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:apiKey="0xyi8X88f0aIOwwylqKNbxDs9CYznxaMnBKAZgw"         

    />

</LinearLayout>

 

Step5Java檔之主程式部分必須將Activity改為MapActivity,必須import的部分eclipse會協助。

GPStest001.java

package com.sam.test;

import com.google.android.maps.MapActivity;

import android.app.Activity;

import android.os.Bundle;

publicclass GPStest001 extends MapActivity {

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

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

    }

 

    @Override

    protectedboolean isRouteDisplayed() {

        // TODO Auto-generated method stub

        returnfalse;

    }

}

Note 到此,完成Part1部分,可以Run看看,順利出現地圖為成功。

GOOGLEMAP.jpg    

 

 

 

[Part2]Zoom元件

 

Step1修改res/layout/main.xml版面

(1)將最外層的LinearLayout改成RelativeLayout,目的為將Zoom元件放在Map下方且邊緣上方。

(2)RelativeLayout內加入一個LinearLayout元件,用此原件顯示Zoom元件。

(3)Zoom元件使用alignBottom方式,指定放置位置在MapView底部之上。

 

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:id="@+id/mainlayout"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    >

    <com.google.android.maps.MapView

        android:id="@+id/mapview"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:clickable="true"  

        android:apiKey="0xyi8X88f0aIOwwylqKNbxDs9CYznxaMnBKAZgw"

    />

    <!-- Zoom元件 -->

    <LinearLayout

    android:id="@+id/zoomview"

    android:layout_alignBottom="@id/mapview"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_centerHorizontal="true"

    />

</RelativeLayout>

 

程式碼語法注解:

android:clickable="true"  //使用者是否能用觸控方式來移動視窗上的地圖

android:layout_alignBottom="@id/mapview"    //指定放在MapView的底部之上

android:layout_centerHorizontal="true"   //水平居中

 

Step2修改主程式碼,產生Zoom物件。

GPStest001.java

package com.sam.test;

import com.google.android.maps.MapActivity;

import com.google.android.maps.MapView;

import android.widget.ZoomControls;

import android.app.Activity;

import android.os.Bundle;

import android.widget.LinearLayout;

publicclass GPStest001 extends MapActivity {

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

    @Override

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        LinearLayout linearlayout01;

        MapView mapview01;

        ZoomControls myZoom;

       

        linearlayout01 = (LinearLayout)findViewById(R.id.zoomview);

        mapview01 = (MapView)findViewById(R.id.mapview);

        myZoom = (ZoomControls)mapview01.getZoomControls()

        //MapView加上ZoomControl功能

        linearlayout01.addView(myZoom);

    }

 

    @Override

    protectedboolean isRouteDisplayed() {

        // TODO Auto-generated method stub

        returnfalse;

    }

}

GOOGLEMAP02.jpg  

Zoom in and Zoom out 元件出現啦 !

arrow
arrow
    文章標籤
    android google map
    全站熱搜

    S 發表在 痞客邦 留言(0) 人氣()