Google Maps Android API v2 - 检测地图上的触摸 [英] Google Maps Android API v2 - detect touch on map
问题描述
我找不到有关如何在新的 Google Maps API v2 上拦截地图触摸的示例.
I can't find an example on how to intercept the map touch on the new Google Maps API v2.
我需要知道用户何时触摸地图以停止线程(以我当前位置为中心的地图).
I need to know when the user touches the map in order to stop a thread (the centering of the map around my current location).
推荐答案
@ape 在这里写了一个关于如何拦截地图点击的答案,但我需要拦截触摸,然后他在评论中建议了以下链接它的答案,如何处理 onTouch 事件用于 Google Map API v2 中的地图?.
@ape wrote an answer here on how to intercept the map clicks, but I need to intercept the touches, and then he suggested the following link in a comment of its answer, How to handle onTouch event for map in Google Map API v2?.
该解决方案似乎是一种可能的解决方法,但建议的代码不完整.为此,我重新编写并测试了它,现在它可以工作了.
That solution seems to be a possible workaround, but the suggested code was incomplete. For this reason I rewrote and tested it, and now it works.
这是工作代码:
我创建了类 MySupportMapFragment.java
I created the class MySupportMapFragment.java
import com.google.android.gms.maps.SupportMapFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MySupportMapFragment extends SupportMapFragment {
public View mOriginalContentView;
public TouchableWrapper mTouchView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
mOriginalContentView = super.onCreateView(inflater, parent, savedInstanceState);
mTouchView = new TouchableWrapper(getActivity());
mTouchView.addView(mOriginalContentView);
return mTouchView;
}
@Override
public View getView() {
return mOriginalContentView;
}
}
我什至创建了类 TouchableWrapper.java:
I even created the class TouchableWrapper.java:
import android.content.Context;
import android.view.MotionEvent;
import android.widget.FrameLayout;
public class TouchableWrapper extends FrameLayout {
public TouchableWrapper(Context context) {
super(context);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
MainActivity.mMapIsTouched = true;
break;
case MotionEvent.ACTION_UP:
MainActivity.mMapIsTouched = false;
break;
}
return super.dispatchTouchEvent(event);
}
}
在布局中我这样声明:
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="@+id/buttonBar"
class="com.myFactory.myApp.MySupportMapFragment"
/>
为了在主 Activity 中进行测试,我只写了以下内容:
Just for test in the main Activity I wrote only the following:
public class MainActivity extends FragmentActivity {
public static boolean mMapIsTouched = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
这篇关于Google Maps Android API v2 - 检测地图上的触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!