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.
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
my onStart() is:
and my onLocationChange() is
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.