Android アプリ用の Android Maps API がバージョンアップしていて、ラスター画像による地図ではなく、ベクトルデータの地図が使えるようになってた。

開発者はこの新バージョンの API によって、Android 向け Google マップの数多くの最新機能を Android アプリに取り入れることができるようになります。この API を Google Play 開発者サービスの一部としてご提供できることを大変嬉しく思います。Froyo 以降 (API レベル8+) の端末に対応しています。

新たな API では 2D および 3D ビュー対応のベクターベースのマップを使用し、ユーザーはシンプルなジェスチャーでマップの傾斜や回転操作ができます。衛星写真やハイブリッド表示、地形や交通状況といった Google マップでおなじみの各種レイヤーに加え、新 API では主要な空港やショッピングセンターのインドア マップもアプリに追加できるようになります。

Google Maps Android API の新バージョン、Google Play開発者サービスの一部として公開 - Google Japan Developer Relations Blog

というわけで、試しにサンプルコードを書いてみる。

開発環境

- MacBook Air + Mac OS X Lion 10.7.5
- Eclipse IDE for Java Developers (Version: Juno Service Release 1)

今回の手順

  1. Eclipse から Android SDK Manager を起動して Google Play Services rev.4 をダウンロード
  2. Eclipse にて Google Play Services のライブラリ・プロジェクトを作成 (Android Project from Existing Code でプロジェクトを作成して /usr/local/android-sdks/extras/google/google_play_services/libproject/google-play-services_lib を選択してインポート)
  3. Google APIs Console にて、 Create Project し、 Google Maps Android API v2 を ON にし、メニューの API Access から Create new Android key にて API key を生成
  4. Eclipse にて、Android Application Project でプロジェクトを作成
  5. サンプルコードを書いて、API key を埋め込んで、ビルドして、 Android 端末実機 (Softbank ARROWS A 101F) で動作確認

サンプルのソースコード

修正したのは、メインのアクティビティクラス (MainActivity.java) と AndroidManifest.xml の2ファイルだけ。

MainActivity.java


package info.nilab.hogemaps;
 
import android.os.Bundle;
import android.view.Menu;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMapOptions;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
 
public class MainActivity extends FragmentActivity {
    
    private SupportMapFragment mapFragment;
    private static final String HOGE_MAP_FRAGMENT_TAG = "hoge_fragment";
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // 地図を表示するコードを追加
        FragmentManager fm = getSupportFragmentManager();
        Fragment f = fm.findFragmentByTag(HOGE_MAP_FRAGMENT_TAG);
        mapFragment = (SupportMapFragment)f;
        if (mapFragment == null) {
 
            // GoogleMapOptions
            GoogleMapOptions opt = new GoogleMapOptions();
            opt.compassEnabled(true);
            opt.rotateGesturesEnabled(true);
            opt.scrollGesturesEnabled(true);
            opt.tiltGesturesEnabled(true);
 
            // SupportMapFragment
            mapFragment = SupportMapFragment.newInstance(opt);
 
            // FragmentTransaction#add
            FragmentTransaction ft = fm.beginTransaction();
            ft.add(android.R.id.content, mapFragment, HOGE_MAP_FRAGMENT_TAG);
            ft.commit();
        }        
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    @Override
    protected void onResume(){
 
        super.onResume();
 
        // GoogleMap
        GoogleMap map = mapFragment.getMap();
        map.setIndoorEnabled(true);
        map.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        map.setMyLocationEnabled(true);
        map.setTrafficEnabled(true);
 
        // 渋谷駅: The location that the camera is pointing at.
        LatLng target = new LatLng(35.658516, 139.701773);
 
        // Zoom level near the center of the screen.
        float zoom = 18.0f;
 
        // The angle, in degrees, of the camera angle from the nadir (directly facing the Earth).
        //
        // CameraPosition.Builder | Android Developers
        // http://developer.android.com/reference/com/google/android/gms/maps/model/CameraPosition.Builder.html#tilt(float)
        //
        // Sets the angle, in degrees, of the camera from the nadir (directly facing the Earth).
        // When changing the camera position for a map, this value is restricted depending on the zoom level of the camera. The restrictions are as follows:
        // - For zoom levels less than 10 the maximum is 30.
        // - For zoom levels from 10 to 14 the maximum increases linearly from 30 to 45 (e.g. at zoom level 12, the maximum is 37.5).
        // - For zoom levels from 14 to 15.5 the maximum increases linearly from 45 to 67.5.
        // - For zoom levels greater than 15.5 the maximum is 67.5. 
        // The minimum is always 0 (directly down). If you specify a value outside this range and try to move the camera to this camera position it will be clamped to these bounds. 
        float tilt = 90.0f; // 0.0 - 90.0
 
        // Direction that the camera is pointing in, in degrees clockwise from north.
        float bearing = 0.0f;
 
        // CameraUpdate
        CameraPosition pos = new CameraPosition(target, zoom, tilt, bearing);
        CameraUpdate camera = CameraUpdateFactory.newCameraPosition(pos);
 
        map.moveCamera(camera);
 
        // UiSettings
        UiSettings ui = map.getUiSettings();
        ui.setZoomControlsEnabled(true);
        ui.setZoomGesturesEnabled(true);
    }
 
}

AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.nilab.hogemaps"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="15" />
 
    // Adding the API Key to your application - Google Maps Android API v2 — Google Developers
    // https://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application
    <permission
        android:name="info.nilab.hogemaps.permission.MAPS_RECEIVE"
        android:protectionLevel="signature"/>
    <uses-permission android:name="info.nilab.hogemaps.permission.MAPS_RECEIVE"/>
 
    // Specify settings in the Application Manifest - Google Maps Android API v2 — Google Developers
    // https://developers.google.com/maps/documentation/android/start#specify_settings_in_the_application_manifest
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true"/>
        
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        
    // Adding the API Key to your application - Google Maps Android API v2 — Google Developers
    // https://developers.google.com/maps/documentation/android/start#adding_the_api_key_to_your_application
    //
    // Google APIs Console https://code.google.com/apis/console/ で作った
    // API key (Key for Android apps) を記述
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
            android:value="abcdefghijklmnopqrstuvxyzABCDEFGH123456" />
 
        <activity
            android:name="info.nilab.hogemaps.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>
    </application>
 
</manifest>

サンプルコードによる Android アプリのスクリーンショット

ベクトルならではの3D俯瞰とインドアマップ機能など。

Google Maps Android API v2

渋谷駅。

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

南極大陸の先は無し。

Google Maps Android API v2

素早くドラッグすると読み込むのが追いつかずに灰色に。

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

アローヘッド タウン センター (Arrowhead Towne Center)。

Google Maps Android API v2

Google Maps Android API v2

Google Maps Android API v2

名古屋駅。

Google Maps Android API v2

Google Maps Android API v2

Ref.

tags: google_maps_api android

Posted by NI-Lab. (@nilab)