Question

I have a map v2 in a FrameLayout. If I don't animate it from code, it responds to touch/swipe/pinch events in the usual way. If I animate it after a onLocationChanged() call, the camera moves and zooms, but then the map stops responding to user input (or at least that is the feeling). You swipe but the map doesn't pan. You pinch but the map doesn't zoom. You tap... you get the idea.

However I discovered by chance that something is actually happening, but it's not being shown for some reason. If, for example, I swipe, the map does not move but somehow records the swipe. If I touch the home button, which brings the system back to the home screen, and then bring the app again in foreground, I can see my map in the panned position, with the farthest zoom possible, without any markers I eventually added before. It seems that the swipe gesture was caught by a overlaid invisible map centered in lat=0, lng=0 and applied to that map, and it seems that now that map is the one being shown.

Except that, using various breakpoints, it's clear that there aren't any other maps being created in my activity, let alone transparent and overlaid ones... my description serves only to let you understand what's going on, I'm not suggesting that there actually is another map above mine.

Here are the relevant pieces of code, if you need more, just ask:

my onCreate()

@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 onLocationChanged() 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);
  }
}

Please note that I deferred map configuration until the first onLocationChanged() event because otherwise the map refused to animate to the requested location (see my other question for that: Android: GoogleMap v2 ignores animate/move calls ).

Était-ce utile?

La solution

You seem to be adding two SupportMapFragments to your layout. The one user sees is not the one they interact with. Make sure you add only a single fragment.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top