カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

現在地を取得する LocationClient

GoogleMapに限らず、位置情報を利用することは多いと思います。
LocationClientクラスを利用して位置情報を取得することができます。

GooglePlay開発者サービス(GooglePlayServices)を利用しますので、
プロジェクトにライブラリを組み込む必要があります。
導入方法は以下に書いていますのでご覧ください。
[Google Maps Android API v2]

今回、必要なインポートの抜粋。

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.common.GooglePlayServicesUtil;

import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;

Activityを継承したメインクラスにコールバックのクラスを実装します。

public class MainActivity extends Activity 
    implements 
    ConnectionCallbacks,
    OnConnectionFailedListener,
    LocationListener {

LocationRequestの設定をします。

private static final LocationRequest REQUEST = LocationRequest.create()
        .setInterval(5000)         // 5 seconds
        .setFastestInterval(16)    // 16ms = 60fps
        //.setNumUpdates(3)
        .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
LocationRequestのパラメータ設定
setInterval (long millis) ミリ秒単位で位置情報更新の間隔を設定します。
setFastestInterval (long millis) ミリ秒単位で位置情報更新の正確な間隔を設定します。
setNumUpdates (int numUpdates) 位置情報の更新数を設定します。
setPriority (int priority) 位置情報取得要求の優先順位を設定します。

続きを読む…»

10,077 views

2点間の距離を取得 – Location.distanceBetween

LocationクラスのdistanceBetweenメソッドを使って、2点間の距離を取得することができます。

書式

Location.distanceBetween(
        double startLatitude,
        double startLongitude,
        double endLatitude,
        double endLongitude,
        float[] results);
引数
startLatitude 開始地点の緯度
startLongitude 開始地点の経度
endLatitude 終了地点の緯度
endLongitude 終了地点の経度
results 結果を格納する変数

続きを読む…»

9,137 views

Google Maps Android API v2

Google Maps Android API v2を使ってみました。
v2はv1とは互換性がありませんが、ベクター地図になり、視点の変更や屋内の地図表示等、色々便利になったようです。

Google Play Servicesライブラリ

まず、v2ではGoogle Play Servicesが必要になるので、Android SDK Managerで最新版をダウンロードし、インストールします。
1

Eclipseにて新規プロジェクトを作成します。
「既存コードからのAndroidプロジェクト」を選択し[次へ]
2
続きを読む…»

4,109 views

カメラを扱うまとめ – Android

Androidでカメラを扱うまとめです。
単純に撮った写真を保存するパターンと、パラメーターを指定して保存するパターン、
撮影した画像を圧縮して保存するパターンの3パターンを紹介します。

まずは、カメラを使用するために必要なパーミッションをAndroidManifest.xmlに記述します。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-feature android:name="android.hardware.camera.flash" />

撮影処理をするSurfaceViewを継承したクラスを作成します。

まず、簡単に保存するパターン 続きを読む…»

4,641 views

GPS機能設定の状態を取得(ネットワーク)

GPS機能についてはこちら1828

端末自身の[設定]-[位置情報とセキュリティ]より、「Wi-Fi/モバイルネットワークで位置を検出」がONに設定されているかを取得します。
OFFに設定されている場合にダイアログを表示して設定画面を呼び出す処理をしています。

// 位置情報設定の状態を取得
String GpsStatus = android.provider.Settings.Secure.getString(getContentResolver(), Secure.LOCATION_PROVIDERS_ALLOWED);

if (GpsStatus.indexOf("network", 0) < 0) {
  //Wi-Fi/モバイルネットワークで位置を検出が無効だった場合
  AlertDialog.Builder ad = new AlertDialog.Builder(this);
  ad.setMessage("Wi-Fi/モバイルネットワークで\n位置を検出する機能がOFFになっています。\n設定画面を開きますか?");
  ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
      //設定画面を呼び出す
      Intent intent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
      startActivity(intent);
    }
  });
  ad.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int whichButton) {
      //何もしない
      }
  });
  ad.create();
  ad.show();
} else if {
} else {
  //有効だった場合
}

API Level3(Android 1.5)以降、セキュリティ上の問題で設定を強制的に変更することはできません。

3,882 views