SupportMapFragment with ViewPager: unable to move camera or change map settings

I have a problem with SupportMapFragment

and moving the camera to a location on the map.

public class MapPositionFragment extends SupportMapFragment implements LocationListener{

private GoogleMap map = null;
private Button lockButton = null;
private Location currentLocation = null;
private LocationManager locationManager = null;
private View mapView = null;

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mapView = super.onCreateView(inflater, container, savedInstanceState);
    View v = inflater.inflate(R.layout.fragment_route_map, container, false);

    SetUpMap(v);
    return v;
  }

@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
}

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {

    if (isVisibleToUser){
        SetUpLocationService();
    }
}   

private void SetUpLockButton(){
    lockButton = (Button)getActivity().findViewById(R.id.lock_screen_button);
    lockButton.setOnClickListener(new OnClickListener() {           
        public void onClick(View v) {
            ToggleLockButton();
        }
    });     
}

private void ToggleLockButton(){
    BaseSwipeActivity baseActivity = (BaseSwipeActivity)getActivity();
    boolean swipeEnabled = baseActivity.TogglePaging();

    if (swipeEnabled){
        lockButton.setBackgroundResource(R.drawable.stock_lock_open);
    }
    else{
        lockButton.setBackgroundResource(R.drawable.stock_lock);
    }
}

private void SetUpLocationService(){
    boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean networkIsEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (networkIsEnabled){
        // Register the listener with the Location Manager to receive location updates
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
    } else if (gpsIsEnabled){
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
    }

}

private void SetCurrentLocation(final Location location){

    if (map != null){
        getActivity().runOnUiThread(new Runnable() {
              public void run() {
                LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
                CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 10);
                map.moveCamera(cameraUpdate);
              }
        });

    }
}

private void SetUpMap(View view) {
    if (map == null){
        getActivity().runOnUiThread(new Runnable() {
              public void run() {
                map = getMap();
                map.getUiSettings().setMyLocationButtonEnabled(true);
                map.setIndoorEnabled(true);
                map.getUiSettings().setZoomControlsEnabled(false);
                locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
              }
        });
    }
}

@Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    SetUpLockButton();
  }


public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
      SetCurrentLocation(location);
}

public void onProviderDisabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
    // TODO Auto-generated method stub

}   

      

}

It SupportMapFragment

, which is located inside a ViewPager

. The map is displayed, but I cannot change the map settings, and I cannot change the position of the camera. I thought maybe because on another thread I should be using the UI thread, but that doesn't change anything.

So maybe someone has an idea, that would be great!

thanks Manuel

+3


source to share


1 answer


I faced the same problem today. Here's my solution for it.

Just like you:

// @ SetUpMap()
map = getMap();

      

Replace it:



// Remeber to change the map id (R.id.map) to what you have in the layout .xml
map = ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.map))
        .getMap();

      


It looks like this solution doesn't work with Android 21.0.x support library (I tested for .2 and .3). Since this method works with old Android support libraries, there might be a bug, etc.

+5


source







All Articles