GoogleMap系の覚え書きメモ(1)
v1.5 => v2互換
結構ググるとv1.5ベースなサンプルが多かったりして混乱してくるので情報を整理しておく
- v1 => Google APIsを使うやつ
- v1.5 => ほぼ同等な機能(?)で置き直したレベル(5.+)
LocationClientが使えるバージョン
compile 'com.google.android.gms:play-services:5.+'
分割指定が使えるバージョン(from 6.5)
分割自体の話は
compile 'com.google.android.gms:play-services-location:6.5.+' compile 'com.google.android.gms:play-services-maps:6.5.+'
compile 'com.google.android.gms:play-services-base:6.5.+'
はdependency参照にはされているようなので、build.gradleでは指定が要らない
カスタムLibraryProjectを作ってADTコンパチにする場合、
6.5まではaarをzipに拡張子変えてコピーしてそのまま解凍すればOK。
それ以降はそれぞれのaarをzipにして解凍して導入してやる必要があり*1
LocationClientが廃止されているバージョン
こっから大幅な変更が入っている。
自分が2年前に調べた時は、2系端末でLocationClientを使わなくなった時点で、まともに動かなくなった記憶があり
compile 'com.google.android.gms:play-services:6.5.+'
またコード的に下記の修正が必要。
とりあえず LocationClientをつかってるコードでビルドを通したいなら、下記の形で指定
compile 'com.google.android.gms:play-services:5.+'
辺りのバージョンになるみたい
exception-think.hatenablog.com
import文の変更
//import com.google.android.gms.common.GooglePlayServicesClient; //import com.google.android.gms.location.LocationClient; => import com.google.android.gms.common.GooglePlayServicesUtil; import com.google.android.gms.location.LocationServices; import com.google.android.gms.common.api.GoogleApiClient;
リスナーの変更
//GooglePlayServicesClient.ConnectionCallbacks, //GooglePlayServicesClient.OnConnectionFailedListener => GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
インスタンス生成の変更
//private LocationClient mLocationClient; // mLocationClient = new LocationClient(this, this, this); => private GoogleApiClient mLocationClient; mLocationClient = new GoogleApiClient.Builder(this) .addApi(LocationServices.API) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .build();
LocationUpdates関連の変更
//mLocationClient.removeLocationUpdates(this); => LocationServices.FusedLocationApi.removeLocationUpdates(mLocationClient,this);
// mLocationClient.requestLocationUpdates(mLocationRequest, this); => LocationServices.FusedLocationApi.requestLocationUpdates(mLocationClient,mLocationRequest, this);
onConnectionSuspendedの新規追加
下記のサンプルみたいにログだけ出していたり、実装がないサンプルが多い気がする
@Override public void onConnectionSuspended(int i) { if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_NETWORK_LOST) { Log.v(TAG, "Connection lost. Cause: Network Lost." ) ; } else if (i == GoogleApiClient.ConnectionCallbacks.CAUSE_SERVICE_DISCONNECTED) { Log.v(TAG, "Connection lost. Reason: Service Disconnected" ) ; } }
UiSettings関係
デフォルトが全部OFFになったので、必要なのは自分でONしないとダメみたい。
ただONとかにしても、Zoomボタンとか位置が変更できるわけじゃないから*3
Material Design ライクにしようとすると、
- Floating Button
- Bottom navigation
で自前で実装する形になりそう
getMapがduplicatedなバージョン
compile 'com.google.android.gms:play-services:8.+'
8.4 あたりから下記の修正も入る
GoogleMap gMap = null; //GoogleMap gMap = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap() => SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(new OnMapReadyCallback(){ @Override public void onMapReady(GoogleMap map) { gMap = map; } });
ASのテンプレートだと FragmentActivity/MapFragment になってて
の話っぽく使えない??別Activity必須??とか思いがちですが
のサンプルで AppCompatActivity/SupportMapFragment を使っているので問題は無いよう
GooglePlayServiceUtil関連
private final int REQUEST_GOOGLE_PLAY_SERVICES = 5000;//適当な定義値 /* int code = GooglePlayServicesUtil.isGooglePlayServicesAvailable(context); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (GooglePlayServicesUtil.isUserRecoverableError(code)) { GooglePlayServicesUtil.getErrorDialog(code, this, REQUEST_GOOGLE_PLAY_SERVICES).show(); } else { Toast.makeText(this, GooglePlayServicesUtil.getErrorString(code), Toast.LENGTH_LONG).show(); } */ => GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); if (code == ConnectionResult.SUCCESS) { onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null); } else if (api.isUserResolvableError(code) && api.showErrorDialogFragment(this, code, REQUEST_GOOGLE_PLAY_SERVICES); } else { Toast.makeText(this, api.getErrorString(code), Toast.LENGTH_LONG).show(); }
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode) { case REQUEST_GOOGLE_PLAY_SERVICES: if (resultCode == Activity.RESULT_OK) { Intent i = new Intent(this, RegistrationService.class); startService(i); // OK, init GCM } break; default: super.onActivityResult(requestCode, resultCode, data); } }
showErrorDialogFragment 自体は下記のようにも書けるみたいですね
/* GooglePlayServicesUtil.getErrorDialog( errorCode, this, ERROR_DIALOG_REQUEST_CODE, new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // 操作がキャンセルされたので、Activity実行中止等の処理 } }).show(); */ GoogleApiAvailability api = GoogleApiAvailability.getInstance(); int code = api.isGooglePlayServicesAvailable(this); api.showErrorDialogFragment( this, code, ERROR_DIALOG_REQUEST_CODE, new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { // 操作がキャンセルされたので、Activity実行中止等の処理 } } );
getMapが廃止されているバージョン
compile 'com.google.android.gms:play-services:9.+'
最新のASを使っていると指定されるバージョン