GeoFencing (サンプルコード)

GeoFencingのサンプルコードを記載しまーす

↓Geofencingが分からない人は


Geofencingは予め登録した場所に出たり入ったりすることで
PendingIntentを発行してくれます。

Geofencingはとても便利ですが、もし登録した場所を消したい場合は
明示的に削除しなければなりません。

以下に登録と削除のサンプルコードを記載します。
なお今回はLocationClientの説明は省きます。

ジオフェンスを登録


      // ジオフェンスを登録する
 private void addFence(double latitude, double longitude, float radius,
   String requestId) {
  if (!servicesConnected()) {
   return;
  }

  Geofence.Builder builder = new Geofence.Builder();
  builder.setRequestId(requestId);
  builder.setCircularRegion(latitude, longitude, radius);
  builder.setExpirationDuration(Geofence.NEVER_EXPIRE); // 無期限
  builder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_ENTER);
  builder.setTransitionTypes(Geofence.GEOFENCE_TRANSITION_EXIT);

  // フェンスのListを作成する。
  List fenceList = new ArrayList();
  fenceList.add(builder.build());
  
  
  // フェンス内に入った時に、指定のURIを表示するインテントを投げるようにする。
  Intent intent = new Intent(this, MainActivity.class);
  PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
    intent, PendingIntent.FLAG_UPDATE_CURRENT);

  mLocationClient.addGeofences(fenceList, pendingIntent, this);
 }

ジオフェンスを削除
      // 指定されたIDのジオフェンスを削除
 private void removeFence(String requestId) {
  if (!servicesConnected()) {
   return;
  }

  List fenceIdList = new ArrayList();
  fenceIdList.add(requestId);
  mLocationClient.removeGeofences(fenceIdList, this);
 }

コメント

このブログの人気の投稿

レベルアップに必要なXP一覧 Pokemon GO ポケモンGO

[初心者向け]AndroidStudioで地図アプリを作ってみよう![2015年8月最新]

自動で良い感じの色を抽出してくれるPalette Library がすごい! Android Support Library