2016-07-01 37 views
0

我想设置一个侦听器来捕捉摄像机移动,但它不起作用。如果我设置好,则永远不会调用由支持SupportMapFragment的片段实现的onCameraChange方法。 This similar question没有帮助我。Android的onCamera变化的片段永远不会被称为

这里的片段代码:

public class MapFragment extends Fragment implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, 
    OnMapReadyCallback, GoogleMap.OnCameraChangeListener { 
private static final String TAG = MapFragment.class.getSimpleName(); 
private static final int MY_LOCATION_REQUEST_PERMISSION = 100; 
private static final int ENABLE_LOCATION_REQUEST_PERMISSION = 200; 

private int mShortAnimationDuration; 
private ImageButton checkin; 
private LinearLayout big_checkin; 

private GoogleMap mMap; 
private GoogleApiClient mGoogleApiClient; 
private double mLatitude, mLongitude; 

private SupportPlaceAutocompleteFragment autocompleteFragment; 

public MapFragment() { 
    // Required empty public constructor 
} 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onCreate"); 
    } 
    super.onCreate(savedInstanceState); 

    buildGoogleApiClient(); 


} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onCreateView"); 
    } 
    // Inflate the layout for this fragment 
    final View view = inflater.inflate(R.layout.fragment_map, container, false); 

    final SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

    final DrawerLayout drawer = (DrawerLayout) getActivity().findViewById(R.id.drawer_layout); 

    final Toolbar toolbar = (Toolbar) view.findViewById(R.id.map_toolbar); 
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); 

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      getActivity(), drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    autocompleteFragment = (SupportPlaceAutocompleteFragment) 
      getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 

    return view; 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(getContext()) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 


private void getMyLocation() { 
    if (ContextCompat.checkSelfPermission(getContext(), 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     PermissionUtils.requestPermission((AppCompatActivity) getActivity(), MY_LOCATION_REQUEST_PERMISSION, Manifest.permission.ACCESS_FINE_LOCATION, false); 
    } else { 
     final Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (lastLocation != null) { 
      mLatitude = lastLocation.getLatitude(); 
      mLongitude = lastLocation.getLongitude(); 
      LatLng latLng = new LatLng(mLatitude, mLongitude); 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 17); 
      if (mMap != null) { 
       mMap.animateCamera(cameraUpdate); 
       mClusterManager.clearItems(); 
       addItems(); 
      } 
     } else { 
      if (mMap != null) { 
       mMap.setMyLocationEnabled(false); 
      } 
      final ImageButton myLocation = (ImageButton) getView().findViewById(R.id.my_location); //TODO migliorare 
      myLocation.setVisibility(View.GONE); 
     } 
    } 

} 


@Override 
public void onDestroy() { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onDestroy"); 
    } 
    super.onDestroy(); 
} 

@Override 
public void onDestroyView() { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onDestroyView"); 
    } 
    super.onDestroyView(); 

    mMap = null; 
    mGoogleApiClient = null; 
    autocompleteFragment = null; 
} 

@Override 
public void onStart() { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onStart"); 
    } 
    super.onStart(); 
    if (mGoogleApiClient != null) 
     mGoogleApiClient.connect(); 
} 

@Override 
public void onStop() { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onStop"); 
    } 
    mGoogleApiClient.disconnect(); 
    super.onStop(); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 
    if (requestCode == MY_LOCATION_REQUEST_PERMISSION) { 
     if (PermissionUtils.isPermissionGranted(permissions, grantResults, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 
      // Enable the my location layer if the permission has been granted. 

      getMyLocation(); 
     } else { 
      //TODO avvisare che si devono dare i permessi? 
      // Display the missing permission error dialog when the fragments resume. 
     } 
    } else if (requestCode == ENABLE_LOCATION_REQUEST_PERMISSION) { 
     if (PermissionUtils.isPermissionGranted(permissions, grantResults, 
       Manifest.permission.ACCESS_FINE_LOCATION)) { 
      // Enable the my location layer if the permission has been granted. 
      enableMyLocation(); 
     } else { 
      //TODO avvisare che si devono dare i permessi? 
      // Display the missing permission error dialog when the fragments resume. 
     } 
    } 

} 

private void enableMyLocation() { 
    if (ContextCompat.checkSelfPermission(getContext(), 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     PermissionUtils.requestPermission((AppCompatActivity) getActivity(), ENABLE_LOCATION_REQUEST_PERMISSION, Manifest.permission.ACCESS_FINE_LOCATION, false); 
    } else { 
     if (mMap != null) { 
      // Access to the location has been granted to the app. 
      mMap.setMyLocationEnabled(true); 
      //getMyLocation(); 
     } 
    } 
} 


@Override 
public void onMapReady(GoogleMap googleMap) { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "mapReady"); 
    } 
    mMap = googleMap; 
    mMap.setOnCameraChangeListener(this); 
    enableMyLocation(); 
    setUpClusterer(); 
} 

@Override 
public void onConnected(@Nullable Bundle bundle) { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onConnected"); 
    } 
    if (ContextCompat.checkSelfPermission(getContext(), 
      Manifest.permission.ACCESS_FINE_LOCATION) 
      != PackageManager.PERMISSION_GRANTED) { 
     PermissionUtils.requestPermission((AppCompatActivity) getActivity(), MY_LOCATION_REQUEST_PERMISSION, Manifest.permission.ACCESS_FINE_LOCATION, false); 
    } else { 
     final Location lastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (lastLocation != null) { 
      mLatitude = lastLocation.getLatitude(); 
      mLongitude = lastLocation.getLongitude(); 
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(mLatitude, mLongitude), 17); 
      if (mMap != null) { 
       mMap.moveCamera(cameraUpdate); 
       mClusterManager.clearItems(); 
       addItems(); 
      } 
     } else { 
      if (mMap != null) { 
       mMap.setMyLocationEnabled(false); 
      } 
      final ImageButton myLocation = (ImageButton) getView().findViewById(R.id.my_location); //TODO migliorare 
      myLocation.setVisibility(View.GONE); 
     } 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

    Log.i("GOOGLE API CLIENT", "Connection suspended"); 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
public void onCameraChange(CameraPosition cameraPosition) { 
    if (BuildConfig.DEBUG) { 
     Log.d(TAG, "onCameraChange"); 
    } 
    LatLng center = cameraPosition.target; 
    Snackbar.make(getView(), center.latitude + " - " + center.longitude, Snackbar.LENGTH_SHORT); 
} 

} 

这里的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="me.grinworld.grinpark.MapFragment"> 

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/map_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:layout_margin="8dp" 
    android:background="@drawable/map_bar" 
    android:elevation="3dp" 
    android:padding="3dp"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:background="@drawable/map_bar_search"> 

     <ImageView 
      android:id="@+id/fav_icon" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginStart="5dp" 
      android:layout_marginEnd="5dp" 
      android:layout_centerVertical="true" 
      android:layout_alignParentEnd="true" 
      android:src="@drawable/ic_favorite_idle"/> 
     <fragment 
      android:id="@+id/place_autocomplete_fragment" 
      android:name="com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_toStartOf="@id/fav_icon" 
      android:layout_gravity="center" /> 

    </RelativeLayout> 

</android.support.v7.widget.Toolbar> 

+0

也许是因为你没有'show()'小吃店。 'SnackBar.make()。show();' – Aryan

+0

@Aryan不,不管方法里面有什么,我调试它并检查该方法从未被调用。谢谢 – leodev

回答

0

解决。 我刚刚注意到,因为我使用的是ClusterManager,所以我设置了onCameraChangeListener这个管理器,这显然覆盖了我的自定义方法。

相关问题