ユーザーの現在の場所から Google マップを開始しようとしています。現在、完全にズームアウトし始めています。カメラを見つけてユーザーの位置にアニメーション化するための正しいコードがありますが、このコードは、ボタンに入れて自分でタッチした場合にのみ機能します。アクティビティの開始時に移動するにはどうすればよいですか?
いろいろと試してきました。これはそのうちの1つでした。
public class FindBar extends Activity implements ConnectionCallbacks,
OnConnectionFailedListener, LocationListener,
OnMyLocationButtonClickListener {
private GoogleMap mMap;
private LocationClient mLocationClient;
// Button test;
boolean locationClientConnected = false;
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setUpMapIfNeeded();
setUpLocationClientIfNeeded();
mLocationClient.connect();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (mLocationClient != null) {
mLocationClient.disconnect();
Log.d("tag", "locationclient disconnected");
}
}
private void setUpMapIfNeeded() {
Log.d("tag", "entered set up map");
// Check if we were successful in obtaining the map.
if (mMap != null) {
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationButtonClickListener(this);
Log.d("tag", "map location set true and click listener set");
}
}
private void setUpLocationClientIfNeeded() {
if (mLocationClient == null) {
mLocationClient = new LocationClient(getApplicationContext(), this, // ConnectionCallbacks
this); // OnConnectionFailedListener
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// removes the title bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.fragment_find_bar);
// Get a handle to the Map Fragment
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
// test = (Button) findViewById(R.id.bTest);
// test.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// moveCameraTo(mMap.getMyLocation());
// }
//
// });
moveCameraTo(mLocationClient.getLastLocation());
}
// Takes a Location and centers camera on that location
public void moveCameraTo(Location mLocation) {
Log.d("camera", "entered moveToCamera");
LatLng camPos = new LatLng(mLocation.getLatitude(),
mLocation.getLongitude());
Log.d("camera", "created camera position");
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(camPos, 15));
Log.d("camera", "moved camera");
}
@Override
public boolean onMyLocationButtonClick() {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle connectionHint) {
// TODO Auto-generated method stub
Log.d("camera", "entered onConnected");
// moveCameraTo(mMap.getMyLocation());
locationClientConnected = true;
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
}