GoogleMap系の覚え書きメモ(2.0)
はじめに
前回までのまとめは下記
内容的には、GoogleMap系の覚え書きメモ(1.0) にかぶるかも。
v1.5辺りのLocationClientを使う頃にGeofenceが導入されていて、
現在はGoogleApiClientで使うイメージになっている
Geofence関係
登録・削除操作周り
リスナーの変更
今回はリスナーをインナークラスとして記載してみた場合の時
//宣言は変更なし private MyGooglePlayServicesClientListener _gpListener; //private class MyGooglePlayServicesClientListener // implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener { => private class MyGooglePlayServicesClientListener implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { //public void onDisconnected() { => public void onConnectionSuspended(int i) { if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { Log.d(TAG, "Connection lost. Cause: Network Lost." ) ; } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { Log.d(TAG, "Connection lost. Reason: Service Disconnected" ) ; } Log.d(TAG, "GooglePlayServicesClient disconnected."); //onDisconnected で書いていた処理があれば追記 }
インスタンス生成の変更
//private LocationClient _locationClient; //_locationClient = new LocationClient(context, _gpListener, _gpListener); private GoogleApiClient _locationClient; _locationClient = new GoogleApiClient.Builder(context) .addApi(LocationServices.API) .addConnectionCallbacks(_gpListener) .addOnConnectionFailedListener(_gpListener) .build();
geofenceの登録
//_locationClient.addGeofences(geofences, pendingIntent, listener); => GeofencingRequest request = getGeoFenceRequest(geofenceList); LocationServices.GeofencingApi.addGeofences(_locationClient, request, pendingIntent);
- Geofence Request取得(関数追加)
private GeofencingRequest getGeoFenceRequest(List<Geofence> geofenceList) { GeofencingRequest.Builder builder = new GeofencingRequest.Builder(); builder.setInitialTrigger(GeofencingRequest.INITIAL_TRIGGER_DWELL); builder.addGeofences(geofenceList); return builder.build(); }
現在は、Geofence Requestを作成して設定するほうが主流
geofenceの削除
//_locationClient.removeGeofences(geofenceRequestIds, listener); LocationServices.GeofencingApi.removeGeofences(_locationClient,geofenceRequestIds); =>
//_locationClient.removeGeofences(pendingIntent, listener); => LocationServices.GeofencingApi.removeGeofences(_locationClient,pendingIntent);
最後の位置を取得しているコードがあった場合
//return _locationClient.getLastLocation(); => return LocationServices.FusedLocationApi.getLastLocation(_locationClient);
受信サービス周り
サンプル例とかだと、よく GeofenceService.java とか定義される受信 IntentService
イベント情報の状態取得
//int transition = LocationClient.getGeofenceTransition(intent); => GeofencingEvent event = GeofencingEvent.fromIntent(intent); f(event == null) { onError(GEOFENCE_EVENT_NULL); return null; } if(event.hasError()){ onError(event.getErrorCode()); return null; } int transition = event.getGeofenceTransition();
- エラーコード変換関数(追加)
private static final int GEOFENCE_EVENT_NULL = -1; protected void onError(int errorCode){ final String errorMsg; switch (errorCode) { case GEOFENCE_EVENT_NULL: errorMsg = "GeofencingEvent.fromIntent is NULL"; break; case GeofenceStatusCodes.GEOFENCE_NOT_AVAILABLE: errorMsg = "GEOFENCE_NOT_AVAILABLE"; break; case GeofenceStatusCodes.GEOFENCE_TOO_MANY_GEOFENCES: errorMsg = "GEOFENCE_TOO_MANY_GEOFENCES"; break; case GeofenceStatusCodes.GEOFENCE_TOO_MANY_PENDING_INTENTS: errorMsg = "GEOFENCE_TOO_MANY_PENDING_INTENTS"; break; default: errorMsg = "unknown_geofence_error"; break; } Log.e(TAG,errorMsg); }
イベント情報のリスト取得
//List<Geofence> geofences = LocationClient.getTriggeringGeofences(intent); => List<Geofence> geofences = event.getTriggeringGeofences();