GeoFencing (サンプルコード)
GeoFencingのサンプルコードを記載しまーす
↓Geofencingが分からない人は
↓Geofencingが分からない人は
Geofencingは予め登録した場所に出たり入ったりすることで
PendingIntentを発行してくれます。
Geofencingはとても便利ですが、もし登録した場所を消したい場合は
明示的に削除しなければなりません。
以下に登録と削除のサンプルコードを記載します。
なお今回はLocationClientの説明は省きます。
ジオフェンスを登録
ジオフェンスを削除
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を作成する。 ListfenceList = 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; } ListfenceIdList = new ArrayList (); fenceIdList.add(requestId); mLocationClient.removeGeofences(fenceIdList, this); }
コメント
コメントを投稿