我有一个与TouchableWrapper 不同的更简单的解决方案,它适用于play-services-maps:10.0.1 的最新版本。此解决方案仅使用地图事件,不使用自定义视图。不使用已弃用的函数,并且可能支持多个版本。
首先,您需要一个标志变量来存储地图是由动画还是由用户输入移动(此代码假定每个不是由动画触发的相机移动都是由用户触发的)
GoogleMap googleMap;
boolean movedByApi = false;
您的片段或活动必须实现GoogleMap.OnMapReadyCallback、GoogleMap.CancelableCallback
public class ActivityMap extends Activity implements OnMapReadyCallback, GoogleMap.CancelableCallback{
...
}
这会迫使您实现onMapReady、onFinish、onCancel 方法。并且onMapReady 中的 googleMap 对象必须为相机移动设置一个事件监听器
@Override
public void onMapReady(GoogleMap mMap) {
//instantiate the map
googleMap = mMap;
[...] // <- set up your map
googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
@Override
public void onCameraMove() {
if (movedByApi) {
Toast.makeText(ActivityMap.this, "Moved by animation", Toast.LENGTH_SHORT).show();
[...] // <-- do something whe you want to handle api camera movement
} else {
Toast.makeText(ActivityMap.this, "Moved by user", Toast.LENGTH_SHORT).show();
[...] // <-- do something whe you want to handle user camera movement
}
}
});
}
@Override
public void onFinish() {
//is called when the animation is finished
movedByApi = false;
}
@Override
public void onCancel() {
//is called when the animation is canceled (the user drags the map or the api changes to a ne position)
movedByApi = false;
}
最后,如果你创建一个移动地图的通用函数会更好
public void moveMapPosition(CameraUpdate cu, boolean animated){
//activate the flag notifying that the map is being moved by the api
movedByApi = true;
//if its not animated, just do instant move
if (!animated) {
googleMap.moveCamera(cu);
//after the instant move, clear the flag
movedByApi = false;
}
else
//if its animated, animate the camera
googleMap.animateCamera(cu, this);
}
或者只是每次移动地图时,在动画之前激活标志
movedByApi = true;
googleMap.animateCamera(cu, this);
我希望这会有所帮助!