我试图设置一个监听器以捕捉相机移动,但它不起作用。如果我设置好了,那么由托管SupportMapFragment的Fragment实现的onCameraChange方法也不会被调用。 This similar question没有帮助我。
这里是片段的代码:
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 :(得分:0)
<强>解决。强> 我刚才注意到,因为我使用的是ClusterManager,所以我将这个管理器设置为onCameraChangeListener,这显然超出了我的自定义方法。