マップタイプの表示切替

広告

地図を表示した際にデフォルトのマップタイプは「地図」となっています。ここではマップタイプを「航空写真」に切り替える方法を確認します。「MapView」クラスで用意されている「toggleSatellite」メソッドを使います。

Toggles from satellite to map mode (and back). Note that this may change
the zoom level when switching into map mode. If the current zoom is too 
high for map mode, the zoom is switched to the maximum map zoom.

「toggleSatellite」メソッドを実行すると、現在のマップタイプが「地図」だった場合は「航空写真」に変更し、現在のマップタイプが「航空写真」だった場合には「地図」に変更します。(よってマップタイプを「地図」に設定するようなメソッドはありません)。

現在表示されているマップタイプを取得するメソッドも用意されています。「MapView」クラスで用意されている「isSatellite」メソッドを使います。


Returns:
  true if the map is in "Satellite" image mode, false otherwise

戻り値として「true」が帰ってきた場合には現在のマップタイプは「航空写真」となっています。「false」の場合はそれ以外のマップタイプです。

具体的には次のように記述します。

@Override public void onCreate(Bundle icicle) {
    super.onCreate(icicle);

    MapView map = new MapView(this);
    map.toggleSatellite();
}

サンプルプログラム

それでは実際に試してみます。プロジェクトを作成しソースコードを次のように変更しました。

Test03_01.java

package jp.javadrive.android;

import com.google.android.maps.MapActivity;
import android.os.Bundle;
import com.google.android.maps.MapView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Button;
import android.graphics.Color;
import android.view.View.OnClickListener;

public class Test03_01 extends MapActivity 
    implements OnClickListener{

    private final int FP = ViewGroup.LayoutParams.FILL_PARENT;
    private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT;

    private MapView map;
    private Button buttonSatellite;

    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setBackgroundColor(Color.BLACK);
        setContentView(linearLayout);

        buttonSatellite = new Button(this);
        buttonSatellite.setText("Satellite Off");
        buttonSatellite.setOnClickListener(this);

        map = new MapView(this);

        linearLayout.addView(buttonSatellite, createParam(WC, WC));
        linearLayout.addView(map, createParam(WC, WC));
    }

    private LinearLayout.LayoutParams createParam(int w, int h){
        return new LinearLayout.LayoutParams(w, h);
    }

    public void onClick(View v) {
        if (v == buttonSatellite){
            map.toggleSatellite();
            if (map.isSatellite()){
                buttonSatellite.setText("Satellite On");
            }else{
                buttonSatellite.setText("Satellite Off");
            }
        }
    }
}

ビルド後にエミュレーター上で実行します。

マップタイプの表示切替

デフォルトのマップタイプは「地図」です。では「Satellite Off」と書かれたボタンをクリックして下さい。

マップタイプの表示切替

地図のマップタイプが「航空写真」に変更となりボタンの表示文字列も変更されます。では「Satellite On」と書かれたボタンをクリックして下さい。

マップタイプの表示切替

地図のマップタイプが「地図」に戻ります。

( Written by Tatsuo Ikura )

Profile
profile_img

著者 / TATSUO IKURA

プログラミングや開発環境構築の解説サイトを運営しています。