初心者用Android入門
TOP 1.はじめに 2.開発環境の構築 3.ライフサイクル 4.アプリケーション 5.GoogleMapアプリ

Google Mapを操作する

 ここでは、Google Mapの 移動やズームなどの操作が行えるようにします。 プロジェクトは先ほど作成したGoogleMapViewをそのまま利用します。

 main.xmlのMapViewタグにidを追加します。 main.xmlはプロジェクトの[res]-[layout]下にあります。 idには「@+id/MapView」を指定しています。 この場合、プログラムからは「R.id.MapView」と書くことでアクセスします。

<?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:id="@+id/MapView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
    />
</LinearLayout>

 [src]にあるGoogleMapVies.javaを修正します。

package com.first_android.android.googlemapview;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;

import android.os.Bundle;

public class GoogleMapView extends MapActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        MapView mView = (MapView)findViewById(R.id.MapView);
        // タッチ操作を可能にする
        mView.setClickable(true);
        // ズーム操作を可能にする
        mView.setBuiltInZoomControls(true);
    }

    protected boolean isRouteDisplayed(){
    	return false;
    }

}

MapView mView = (MapView)findViewById(R.id.MapView);

 MapViewクラスのインスタンスを取得します。 findViewById()メソッドはxmlファイルから指定されたidのビューを取得します。 「R.id.MapView」で先ほどmain.xmlに追加したidを指定してMapViewを取得します。

mView.setClickable(true);

 setClickableメソッドは引数にtrueを指定することで、 マップの移動や拡大縮小を可能にします。

mView.setBuiltInZoomControls(true);

 setBuiltInZoomControlsメソッドは引数にtrueを指定することで、 ズームコントローラーがマップ上に表示されます。

 [実行]ボタンから[Androidアプリケーション]を選択して実行します。 しばらくするとエミュレータが起動します。 Menuボタンを押してLockを解除すると。 アメリカを中心とした地図が表示されます。 マウスを利用して移動、+−ボタンを押すことで拡大縮小を行うことができます。

[戻る]  [TOP] 


This website is link free.
All graphics and page design, Copyright © 初心者用Android入門

Portions of this page are modifications based on work created and shared by Google and
used according to terms described in the Creative Commons 3.0 Attribution License.