我在搜索谷歌地图中心时使用双击和缩放进行缩放。我找到了Here的一个解决方案,这仅适用于双击缩放解决方案。
TouchableWrapper
public class TouchableWrapper extends FrameLayout {
GestureDetectorCompat mGestureDetector;
public TouchableWrapper(Context context) {
super(context);
mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}
private final GestureDetector.SimpleOnGestureListener mGestureListener
= new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(MotionEvent e) {
Log.e("GestureDetector", "Executed");
//Notify the event bus (I am using Otto eventbus of course) that you have just received a double-tap event on the map, inside the event bus event listener
EventBus_Singleton.getInstance().post(new EventBus_Poster("double_tapped_map"));
return true;
}
};
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
mGestureDetector.onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
}
}
我已将Otto用于事件触发器。
public class EventBus_Singleton {
private static final Bus BUS = new Bus();
public static Bus getInstance() {
return BUS;
}
public EventBus_Singleton() {
}
public void post(String s) {
BUS.post(s);
}
}
EventBus_Poster
public class EventBus_Poster {
public final String string;
EventBus_Poster(String string) {
this.string = string;
}
}
在“我的地图活动”中,我使用事件接收方法在同一位置缩放地图。
@Subscribe
public void EventBus_singleton(EventBus_Poster event) {
Log.e("EventBus_singleton", "Executed");
if (event != null && event.string.equals("double_tapped_map")) {
if (mapFragment != null) {
mapFragment.getMap().getUiSettings().setZoomGesturesEnabled(false);
mapFragment.getMap().getUiSettings().setRotateGesturesEnabled(false);
mapFragment.getMap().setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition cameraPosition) {
LatLng lng = cameraPosition.target;
Log.e("LatLng", "" + lng);
}
});
mapFragment.getMap().animateCamera(CameraUpdateFactory.zoomIn(), 400, null);
}
}
}
我曾尝试使用ScaleGestureDetector.SimpleOnScaleGestureListener
来进行缩放缩放,但它不起作用,有时甚至是滞后。任何人都可以帮我在谷歌地图上进行缩放吗?
答案 0 :(得分:6)
嗯,我发现在尝试这样做时,成功的缩放涉及速度和比例条件。测量速度以确定您是否真的想要缩放,具体取决于您将两个手指移开或移近彼此的速度,您可以在谷歌地图内部和优步的应用程序中尝试这一点,您实际上可以继续移动手指远离或靠近,但速度太慢,以至于不会在地图上触发放大/缩小事件!
因此,在尝试确定完整的捏合手势时,肯定会涉及速度。第二当然你也需要比例尺,并且我已经打印出比例值,当你开始捏缩放时,它总是总是小于 1.0 <始终当你开始捏合放大时,大于1.0。
这两者的组合肯定会让你获得更好的结果,先看速度我会说
private final ScaleGestureDetector.OnScaleGestureListener mScaleGestureListener
= new ScaleGestureDetector.SimpleOnScaleGestureListener() {
/**
* This is the active focal point in terms of the viewport. Could be a local
* variable but kept here to minimize per-frame allocations.
*/
float startingSpan;
float startFocusX;
float startFocusY;
@Override
public boolean onScaleBegin(ScaleGestureDetector scaleGestureDetector) {
startingSpan = scaleGestureDetector.getCurrentSpan();
startFocusX = scaleGestureDetector.getFocusX();
startFocusY = scaleGestureDetector.getFocusY();
return true;
}
@Override
public boolean onScale(ScaleGestureDetector scaleGestureDetector) {
float scale = scaleGestureDetector.getCurrentSpan() / startingSpan;
mVelocityTracker.computeCurrentVelocity(1000);
Log.d("VELOCITY", "X vel : " + mVelocityTracker.getXVelocity());
Log.d("VELOCITY", "Y vel : " + mVelocityTracker.getYVelocity());
if (scale <= 1.0) {
EventBus_Singleton.getInstance().post(new EventBus_Poster("pinched_map", "out"));
} else {
EventBus_Singleton.getInstance().post(new EventBus_Poster("pinched_map", "in"));
}
return true;
}
};