Android: GoogleMap v2 ignores animate/move calls

2019-08-31 07:02发布

I have a FragmentActivity with a GoogleMap inside. It correctly receives the user location in onLocationChanged, where I try to use the user location to center the map:

@Override
public void onLocationChanged(Location loc)
{
  if (centerMap && mMap != null)
  {
    LatLng location = new LatLng(loc.getLatitude(), loc.getLongitude());
    CameraPosition pos = new CameraPosition.Builder().target(location).zoom(12).build();
    CameraUpdate cu = CameraUpdateFactory.newCameraPosition(pos);
    mMap.animateCamera(cu);

    // centerMap = false;
    Log.e(getClass().getSimpleName(), "lat = " + location.latitude + " long = " + location.longitude);
  }
}

This code actually worked now and then (maybe only once), and only during a debug session where I put a breakpoint inside the if statement. I don't understand what's wrong. The onLocationChanged method gets called regularly, it logs the position it received, that implies it entered the if condition, but the map does not move an inch from the lat=0,long=0 initial position (Africa).

Any clues?

EDIT: I must have something badly broken in my code: even markers do not show up, here is how I add them to the map

mMap.addMarker(new MarkerOptions()
                      .position(new LatLng(lat, lng))
                      .title("title")
                      .draggable(false)
                      .visible(true));

and the "My location" icon is not showing up either, even if I called

mMap.setMyLocationEnabled(true);

in onCreate(). Trying to exclude martians and such, I've already updated Eclipse, ADT and all the rest to the latest available versions.

1条回答
【Aperson】
2楼-- · 2019-08-31 07:03

For some reason (unknown to me), moving the map initialization from onCreate() into the first invocation of onLocationChange() did the trick. Now my onCreate() is simply

@Override
protected void onCreate(Bundle savedInstanceState)
{
  super.onCreate(savedInstanceState);

  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN);

  setContentView(R.layout.map_activity);

}

my onStart() is:

@Override
protected void onStart()
{
  super.onStart();

  // Create the LocationRequest object
  mLocationRequest = LocationRequest.create();
  // Use high accuracy
  mLocationRequest.setPriority(
        LocationRequest.PRIORITY_HIGH_ACCURACY);
  // Set the update interval to 5 seconds
  mLocationRequest.setInterval(UPDATE_INTERVAL);
  // Set the fastest update interval to 1 second
  mLocationRequest.setFastestInterval(FASTEST_INTERVAL);

   mLocationClient = new LocationClient(this, this, this);
   mLocationClient.connect();
}

@Override
public void onConnected(Bundle arg0)
{
  mLocationClient.requestLocationUpdates(mLocationRequest, this);  
}

and my onLocationChange() is

@Override
public void onLocationChanged(Location loc)
{
  if (mMap == null)
  {
    mapFragment = SupportMapFragment.newInstance();
    FragmentTransaction fragmentTransaction = getSupportFragmentManager()
        .beginTransaction();
    fragmentTransaction.add(R.id.mapcontainer, mapFragment);
    fragmentTransaction.commit();

    mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

    mMap.getUiSettings().setAllGesturesEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.setMyLocationEnabled(false);

    LatLng location = new LatLng(loc.getLatitude(), loc.getLongitude());
    CameraPosition pos = new CameraPosition.Builder().target(location).zoom(12).build();
    CameraUpdate cu = CameraUpdateFactory.newCameraPosition(pos);
    mMap.animateCamera(cu);

    if (mLocationClient.isConnected())
      mLocationClient.removeLocationUpdates(this);
    mLocationClient.disconnect();

    Log.e(getClass().getSimpleName(), "lat = " + location.latitude + " long = " + location.longitude);
  }
}

It works, but now I have a different problem (the map seems to ignore touch events). I'm going to create a separate question for that.

查看更多
登录 后发表回答