C/C++プログラマの管理者が, Androidプログラムにチャレンジ. AndroidプログラミングのTipsをメモっていく予定です.

Google Maps Android API v2

複数の地図(Googleマップ)を表示するには, MapFragmentクラスを利用する.

複数の地図表示

Googleマップを表示する」を参照し, Googleマップが表示できるようにする.

  • res/layout/activity_main.xml
    • 地図表示のためのフラグメントを配置する.
    • android:layout_weight属性に, オブジェクトの占有比率を指定する.
    • Android 3.0 (API 12 HonyComb) 以上を対象とする場合は MapFragment, サポートパッケージを利用する場合は SupportMapFragment を指定する.
   class="com.google.android.gms.maps.MapFragment"
         or
   class="com.google.android.gms.maps.SupportMapFragment"
<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"
  tools:context=".MainActivity" >

  <fragment
    android:id="@+id/map1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    class="com.google.android.gms.maps.SupportMapFragment"
  />
  <fragment
    android:id="@+id/map2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="0.5"
    class="com.google.android.gms.maps.SupportMapFragment"
  />
</LinearLayout>
  • MainActivity.java
Android 3.0 (API 12 HonyComb) 以上を対象とする場合は Activity, サポートパッケージを利用する場合は FragmentActivity を使用する.
    • android.support.v4.app.FragmentActivityをインポートする.
    • FragmentActivityを継承したActivityクラスを作成する.
package com.moonlight_aska.android.web.googlemap001;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity {
  private GoogleMap mMap1 = null;
  private GoogleMap mMap2 = null;
  private static final LatLng OSAKA_STATION = new LatLng(34.702177, 135.495114);
  private static final LatLng TOKYO_STATION = new LatLng(35.681382, 139.766084);

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

    mMap1 = ( (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map1) ).getMap();
    if (mMap1 != null) {
      CameraPosition cameraPos = new CameraPosition.Builder()
        .target(OSAKA_STATION)
        .zoom(10.0f)
        .bearing(0).build();
      mMap1.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos));
    }
    mMap2 = ( (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map2) ).getMap();
    if (mMap2 != null) {
      CameraPosition cameraPos = new CameraPosition.Builder()
        .target(TOKYO_STATION)
        .zoom(10.0f)
        .bearing(0).build();
      mMap2.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPos));
    }
  }
}
  • 動作例
Nexus 7 / Android 4.4




コメントをかく


「http://」を含む投稿は禁止されています。

利用規約をご確認のうえご記入下さい

Menu


逆引き(基礎編)

画面表示/操作(49)

フラグメント(1)

逆引き(応用編)

Firebase(2)

AD



管理人/副管理人のみ編集できます