Robert Ding
Robert Ding

Reputation: 31

GoogleMap.moveCamera not working after coming back from child activity

I have a GoogleMap in a SupportMapFragment. It works fine in the initial display. The camera is moved, and a circle is added. But the moveCamera and addCircle stop working after coming back from its child activity.

@Override
public void onConnected(Bundle arg0) {
    Log.d("Main Activity",
            "onConnected called");      

        mMap.setOnMarkerClickListener(this);
        mMap.setOnCameraChangeListener(this);
        mMap.setOnMyLocationChangeListener(this);
    // Display the connection status
        Location loc = mLocationClient.getLastLocation();


        mGeolocation = new LatLng(loc.getLatitude(),loc.getLongitude());
        Log.d("GeoLocation",
                "latitue:"+mGeolocation.latitude+" longitude:" + mGeolocation.longitude);   

        if(mMap.getMyLocation()==null)
            Log.d("GeoLocation",
                    "mMap location not set yet");   
        else    
            Log.d("GeoLocation",
                "mMap location-----latitue:"+ mMap.getMyLocation().getLatitude()+" longitude:" + mMap.getMyLocation().getLongitude());   

        CameraPosition targetPosition = new CameraPosition.Builder().target(mGeolocation)
        .zoom(12.0f)
        .bearing(0)
        .tilt(25)
        .build();


        mMap.moveCamera(CameraUpdateFactory.newCameraPosition(targetPosition));

        CircleOptions circleParam = new CircleOptions()
                                    .center(mGeolocation)
                                    .radius(MAX_DISTANCE)
                                    .strokeColor(Color.BLUE)
                                    .strokeWidth(5)
                                    .visible(true);

        mMap.addCircle(circleParam);

}
@Override
public boolean onMarkerClick(Marker marker) {
    Vendor v = listedVendors.get(marker.getId());
    if( v!=null){
        Intent intent = new Intent(this, MenuActivity.class);
        intent.putExtra(EXTRA_VENDOR, v);
        intent.putExtra(EXTRA_GEO, mGeolocation);
        intent.putExtra(EXTRA_CURRENTADDRESS, mCurrentAddress);
        startActivity(intent);
    }
    return false;
}

@Override
public void onPause() {
    Log.d("Main Activity",
            "OnPause called");      
    super.onPause();
    if (mLocationClient != null) {
        mLocationClient.disconnect();
    }`
}

The onCameraChange event is not called either.

Thanks in advance for any help.

Upvotes: 2

Views: 164

Answers (1)

Robert Ding
Robert Ding

Reputation: 31

OK! Finally, I figured out the problem by myself. Somehow, I declared the mLocationClient as a static member of the activity

private static LocationClient mLocationClient;

The problem is caused by the "static" (I don't know why. Since it is private, it should be the same except the way they are accessed). After removing the 'static' attribute from the above statement, everything works fine now.

Upvotes: 1

Related Questions