我正在尝试让 Google 地图从用户当前位置开始。现在它开始一直放大。我有正确的代码来查找相机并将其动画到用户的位置,但只有当我把它放在一个按钮中并自己触摸它时,这个代码才有效。我怎样才能让它在活动开始时移动?
我一直在尝试很多不同的东西。这是其中之一。
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
}
}